The Artima Developer Community
Sponsored Link

Java Answers Forum
JScrollPane problem

2 replies on 1 page. Most recent reply: Aug 5, 2003 11:01 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Nataly

Posts: 1
Nickname: nata
Registered: Jul, 2003

JScrollPane problem Posted: Jul 30, 2003 11:33 AM
Reply to this message Reply
Advertisement
I have application that drawing some graphics shapes (lines,circels...)

I Have JFrame -> JTabbedPane -> JPanel -> and on this Panel I have JScrollPane.
And when I actually scrolling , some of my drawing is gone ,the part that scrolled out is disappearing.
What can I do? Does ScrollBar has events?It`s looks like I need to use repaint, but where?

Thank you.


David

Posts: 150
Nickname: archangel
Registered: Jul, 2003

Re: JScrollPane problem Posted: Jul 31, 2003 6:24 AM
Reply to this message Reply
Could you post some code?

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: JScrollPane problem Posted: Aug 5, 2003 11:01 AM
Reply to this message Reply
Here is an example of a little program I am using for something I am doing at the moment.
It uses JFrame, JTabbedPane, and JScrollPanes.

Perhaps there is something here to help you.


/*      QuestionEditor.java
 *      August 3, 2003
 */
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.util.*;
 
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
 
/**     QuestionEditor
 */
public class QuestionEditor extends JFrame implements ActionListener, CaretListener{
 
    private int index = 0;
    private File[] files;
    private JTextPane viewer;
    private JTextArea display;
    private String stylesheetFileName = "WebQuestionViewer.xsl";
    public QuestionEditor(){
        super("QuestionEditor");
        init();
    }
    
    public static void main(String[] args){
        new QuestionEditor();
    }
    
    public void init(){
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
 
 
        viewer = new JTextPane();
        viewer.setContentType("text/html");
        viewer.setEditable(false);
        JScrollPane viewerPane = new JScrollPane(viewer);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension viewerSize = new Dimension(screenSize.width, screenSize.height*8/10);
        viewerPane.setPreferredSize(viewerSize);
        
        display = new JTextArea();
        display.setEditable(true);
        display.addCaretListener(this);
        JScrollPane editorPane = new JScrollPane(display);
        editorPane.setPreferredSize(viewerSize);
        
        JPanel controlPanel = new JPanel();
        
        JButton selectFilesButton = new JButton("Select Files");
        selectFilesButton.addActionListener(this);
        controlPanel.add(selectFilesButton);
        
        JButton previousButton = new JButton("Previous");
        previousButton.addActionListener(this);
        controlPanel.add(previousButton);
        
        JButton nextButton = new JButton("Next");
        nextButton.addActionListener(this);
        controlPanel.add(nextButton);
 
        JButton refreshButton = new JButton("Refresh");
        refreshButton.addActionListener(this);
        controlPanel.add(refreshButton);
        
        JButton saveButton = new JButton("Save");
        saveButton.addActionListener(this);
        controlPanel.add(saveButton);
        
        JButton exitButton = new JButton("Exit");
        exitButton.addActionListener(this);
        controlPanel.add(exitButton);
 
        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
        tabbedPane.addTab("View", viewerPane);
        tabbedPane.addTab("Edit", editorPane);
        
        getContentPane().add(tabbedPane, BorderLayout.CENTER);
        getContentPane().add(controlPanel, BorderLayout.SOUTH);
        pack();
        setLocationRelativeTo(null);
        show();
        
    }
    
    public void actionPerformed(ActionEvent actionEvent){
        String actionCommand = actionEvent.getActionCommand();
 
        if (actionCommand.equals("Refresh")) {
            edit();
        }else if (actionCommand.equals("Exit")) {
            System.exit(0);
        }else if (actionCommand.equals("Previous")) {
            index= index -1;
            if (index < 0) index = files.length - 1;
            edit();
        }else if (actionCommand.equals("Next")) {
            index++;
            if (index >= files.length) index = 0;
            edit();
        }else if (actionCommand.equals("Save")) {
            if (files != null){
                try{
                    FileWriter fw = new FileWriter(files[index]);
                    fw.write(display.getText());
                    fw.close();
                }catch (FileNotFoundException fnfe){
                    System.err.println("FileNotFoundException: " + fnfe.getMessage());
                }catch (IOException ioe){
                    System.err.println("IOException: " + ioe.getMessage());
                }
            }
        }else if (actionCommand.equals("Select Files")) {
            files = getFiles();
            if (files != null){
                index= 0;
                edit();
            }
        }
    }  
    
    public void caretUpdate(CaretEvent e){
        viewer.setText(transform(display.getText()));
        viewer.setCaretPosition(0);
    }
    
    private void edit(){
        if (files != null){
            display.setText("");
            display.setBorder(new TitledBorder(files[index].getName()));
            viewer.setBorder(new TitledBorder(files[index].getName()));
 
            try{
                FileReader fr = new FileReader(files[index]);
                BufferedReader br = new BufferedReader(fr);
                String nextLine = "";
                String xml = "";
                while ((nextLine = br.readLine()) != null){
                    xml = xml + nextLine + "\n";
                }
                fr.close();
                br.close();
                display.setText(xml);
                display.setCaretPosition(0);
            }catch (FileNotFoundException fnfe){
                System.err.println("FileNotFoundException: " + fnfe.getMessage());
            }catch (IOException ioe){
                System.err.println("IOException: " + ioe.getMessage());
            }
 
        }else{
            if (files == null)display.append("No files selected.\n");
        }    
    }
    
    private File[] getFiles(){
        display.setText("");
        files = null;
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setCurrentDirectory(new File("k:\\OPS_TRAINING\\Exambank"));
        fileChooser.setMultiSelectionEnabled(true);
        fileChooser.setFileFilter(new XMLFileFilter());
        if (fileChooser.showDialog(this, "Select xml files") == JFileChooser.APPROVE_OPTION ){
            files = fileChooser.getSelectedFiles();
        }        
        return files;
    }
    
    /**	Transform all document into html using an xsl stylesheet.
    *	Output is a string.
    */
    public String transform(String xmlText){
        String htmlstring = "";
        try{
            if (xmlText.length() > 0){
                StreamSource source = new StreamSource(new StringReader(xmlText));
                StringWriter stringwriter = new StringWriter();
                TransformerFactory transformerfactory = TransformerFactory.newInstance();
                StreamSource stylesheet = new StreamSource(new File(stylesheetFileName));
                Transformer transformer = transformerfactory.newTransformer(stylesheet);
                transformer.transform(source, new StreamResult(stringwriter));
                htmlstring = stringwriter.toString();
            }
        }catch (TransformerConfigurationException tce){
                System.err.println("TransformerConfigurationException: " + tce.getMessage());
        }catch (TransformerException te){
                System.err.println("TransformerException: " + te.getMessageAndLocation());
        }catch (Exception exception) {
                System.err.println("Exception: " + exception.getMessage());
        }
        return htmlstring;
    }
 
    
    class XMLFileFilter extends javax.swing.filechooser.FileFilter{
        public boolean accept(File file){
            return (file.isDirectory() || (file.getName().toLowerCase().indexOf(".xml") > 0));
        }
        public String getDescription(){
            return "xml files";
        }
    }
}

Flat View: This topic has 2 replies on 1 page
Topic: Java details question Previous Topic   Next Topic Topic: Trying to write a date program

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use