PDA

View Full Version : Java JEditorPane and JSlider question




prostuff1
Nov 29, 2007, 06:43 PM
All the code is included below.

Basically this project is to produce a Tag Cloud. Most of it is working but i am having trouble getting my JEditorPane to "refresh" when i need it to.

I draw the pane in in the main window leaving it blank. I have it formatted for "text/html" so that I can change size and color to make the tag cloud.

I have it so that when the slider position is changed the text in the JEditorPane is "update" to what it should be for that given book.

If you need more explanation of the code let me know

import javax.swing.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;

public class TagCloudGUI extends JFrame
{
private JButton importBooks, importWords, produceTagCloud;
private JEditorPane booksToScreen;
private JSlider theSlider;
private int numberOfFiles=1;
private JTextField bookTitleField=new JTextField();
private Hashtable labelTable = new Hashtable();

//This is stuff Josh is adding to create the tag cloud objects.
TagCloud tracker = new TrackingTagCloud();
List<File> fileList = new ArrayList<File>();

public TagCloudGUI()
{
super( "Tag Cloud");
setSize(600,400);
setLayout(new BorderLayout());

JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new GridLayout(1,3));

Icon bug1 = new ImageIcon( "bug2.png" );
importBooks = new JButton( "Import File(s)", bug1);
importBooks.setEnabled(false);
importBooks.setToolTipText("Select one file at a time to add to the Tag Cloud");
buttonsPanel.add(importBooks);

Icon bug2 = new ImageIcon( "bug2.png" );
importWords = new JButton( "Import Excluded Word(s)", bug2);
importWords.setDefaultCapable(true);
importWords.setToolTipText("Select a list of word(s) to exclude from the Tag Cloud (only one list allowed at a time)");
buttonsPanel.add(importWords);

Icon bug3 = new ImageIcon( "bug2.png" );
produceTagCloud = new JButton( "Produce Tag Cloud", bug3);
produceTagCloud.setEnabled(false);
produceTagCloud.setToolTipText("Produce the Tag Cloud");
buttonsPanel.add(produceTagCloud);

add(buttonsPanel, BorderLayout.NORTH);

//create slider
theSlider = new JSlider(JSlider.VERTICAL, 1, 1, 1);
theSlider.setInverted(true);
theSlider.setSnapToTicks(true);
theSlider.setMajorTickSpacing(1);
theSlider.setPaintTicks(true);
//Create the label table
labelTable.put(new Integer(numberOfFiles), new JLabel("Book " + (numberOfFiles)));

theSlider.setLabelTable(labelTable);
theSlider.setPaintLabels(true);

add(theSlider, BorderLayout.WEST);

booksToScreen = new JEditorPane("text/html", "");
booksToScreen.setEditable(false);
JScrollPane scrollPane = new JScrollPane(booksToScreen);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
add(scrollPane, BorderLayout.CENTER);

bookTitleField.setEditable(false);
bookTitleField.setBackground(Color.lightGray);
add(bookTitleField, BorderLayout.SOUTH);

ButtonHandler handler = new ButtonHandler();
importBooks.addActionListener(handler);
importWords.addActionListener(handler);
produceTagCloud.addActionListener(handler);

SliderHandler slideHandler = new SliderHandler();
theSlider.addChangeListener(slideHandler);
}

public static void main(String[] args)
{
TagCloudGUI application = new TagCloudGUI();
application.setVisible(true);

application.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE );
}

private class ButtonHandler implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
if(event.getSource() == importBooks)
{
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(TagCloudGUI.this);

if (returnVal == JFileChooser.APPROVE_OPTION)
{
File trackerFile = fc.getSelectedFile();

if(!fileList.contains(trackerFile))
{
//add the tracker file to the array of files that have been added
fileList.add(trackerFile);

produceTagCloud.setEnabled(true);

theSlider.setMaximum(numberOfFiles);
labelTable.put(new Integer(numberOfFiles), new JLabel("Book " + (numberOfFiles)));
theSlider.setLabelTable(labelTable);
theSlider.setValue(numberOfFiles);

bookTitleField.setText("Book " + (numberOfFiles++) + ": " + trackerFile.getName());
bookTitleField.setEditable(false);
}
else
{
JFrame frame = new JFrame();
JOptionPane.showMessageDialog(frame,
"The file you are trying to import already exists in this Tag Cloud. Please choose a different file.",
"File already exists",
JOptionPane.PLAIN_MESSAGE);
}
}
}
else if(event.getSource() == importWords)
{
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(TagCloudGUI.this);

if (returnVal == JFileChooser.APPROVE_OPTION)
{
File excludedWordsFile = fc.getSelectedFile();

//add the excluded words file to the tag cloud
tracker.excludeWords(excludedWordsFile);

importBooks.setEnabled(true);
}
}
else if (event.getSource() == produceTagCloud)
{
for (Iterator<File> fileIterator = fileList.iterator(); fileIterator.hasNext();)
{
//add the file to the tag cloud
tracker.addTagFile(fileIterator.next());
}
//Still not sure on how to handle this but i have to basically update the Window
//to reflect the changes to size and color.
}

}
}// end private inner class ButtonHandler

private class SliderHandler implements ChangeListener
{
public void stateChanged(ChangeEvent event)
{
int sliderValue = theSlider.getValue();
bookTitleField.setText("Book " + (sliderValue) + ": " + fileList.get(sliderValue-1).getName());
}
}
}