Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: JScrollPane problem
|
Posted: Aug 5, 2003 11:01 AM
|
|
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";
}
}
}
|
|