I have a handler class which implements ActionListner, FocusListner, and ItemListner. I have instantiated a gui object from a ConfrenceGUI class:
public ConferenceGUI()
{
//Create a new JPanel and set its latyout mgr
conference = new JPanel();
setLayout(new BorderLayout());
//create a RegPanel panel
regPanel = new RegPanel();
//create new WorkshopPanel workshopPanel
workshopPanel = new WorkshopPanel();
//build a buttonpanel
buildButtonPanel();
//Create a title panel
titlePanel = new JPanel(new FlowLayout());
//create and set a font object
titlePanel.setFont(new Font ("sansserif", Font.BOLD, 18));
//add a label
titlePanel.add(new Label("Select Registration Options"));
//Add the instantiated subpanels to the main conference gui panel
add(titlePanel, BorderLayout.NORTH);
add(regPanel, BorderLayout.WEST);
add(workshopPanel, BorderLayout.EAST);
add(buttonPanel, BorderLayout.SOUTH);
//add an item listener to the combo box
ConferenceHandler handler = new ConferenceHandler(this);
regPanel.regComboBox.addItemListener(handler);
//add a focus listener to the name field
ConferenceHandler fhandler = new ConferenceHandler(this);
regPanel.regTextField.addFocusListener(fhandler);
}
I am trying to take information from the listeners (including two button listeners from a separate method in my ConferenceGUI class, which I did not include).
Here is a snipped of code from my handler:
public void itemStateChanged(ItemEvent e)
{
String name = gui.regPanel.regTextField.getText();
if (e.getSource() == gui.regPanel.regComboBox)
{
if (gui.regPanel.getRegType() == "Please select a type")
JOptionPane.showMessageDialog(null, "Please select a registraion type",
"Type Error", JOptionPane.ERROR_MESSAGE);
else gui.textArea.setText(name+" is a " +
gui.regPanel.getRegType()+ " registration");
}
and for the buttons:
public void actionPerformed (ActionEvent e)
{
String name = gui.regPanel.regTextField.getText();
DecimalFormat $ = new DecimalFormat("$#,##0.00");
if (e.getSource() == gui.calcButton)//if the calculate buttton is pressed
{
//dislplay error box if user selects index 0
if (gui.regPanel.getRegType() == "Please select a type")
JOptionPane.showMessageDialog(null, "Please select a registraion type",
"Type Error",JOptionPane.ERROR_MESSAGE);
//prints to textarea if registrant will be attending keynote or not
if (gui.regPanel.regCheckBox.isSelected())
gui.textArea.setText("Keynote address will be attended");
else
gui.textArea.setText("Keynote address will not be attended");
//prints to textarea which workshops registrant will be attending
gui.textArea.setText(name+" is registered in these workshops:" +
gui.workshopPanel.getWorkshopList());
//prints total registration fees to textarea
gui.textArea.setText("Total charges for" + name + " are " + $.format(calcTotalCharges()));
}
else if (e.getSource() == gui.clearButton)//if the clear button is pressed
{
//clear the textarea
gui.textArea.setText("");
//reset the list
gui.workshopPanel.workshopList.setSelectedIndex(0);
//reset the combobox to index 0
gui.regPanel.regComboBox.setSelectedIndex(0);
}
}
Problem will be obvious to all of you, but as I am just starting out, I can't figure out why I can't write any text into the textArea of my GUI. Apologies for the amount of code, but I wanted to try to be thorough.
Here is where the textarea comes from (which is a sperarate method contained in my ConferenceGUI class:
private void buildButtonPanel()
{
//create the buttonpanel
buttonPanel = new JPanel(new FlowLayout());
//create the buttons
calcButton = new JButton("Calculate Charges");
clearButton = new JButton ("Clear");
//add listeners to the buttons
ConferenceHandler ahandler = new ConferenceHandler(this);
calcButton.addActionListener(ahandler);
clearButton.addActionListener(ahandler);
//create a text area
JTextArea textArea = new JTextArea(5,30);
textArea.setLineWrap(true); textArea.setWrapStyleWord(true);
//add everything to the buttonpanel
buttonPanel.add(calcButton); buttonPanel.add(clearButton); buttonPanel.add(new JScrollPane(textArea));
}
There are three other classes RegPanel, and WorkshopPanel, both of which create a couple of panels for the ConferenceGUI, which is in turn instantiated by an applet (gui).
I'm not sure I understand your question (what doesn't work? What do you expect and what happens?), but what's sure is that calling setText()
multiple times on a JTextArea is not a good idea : setText
replaces the whole content of the text area. You should use append()
to add multiple lines of text to the text area.
EDITED :
Now that you showed us how you built the text area, the problem is clearer : you instantiate a local variable textArea in the buildButtonPanel
method, but the instance variable in your GUI points to another text area (or null).
EDITED again :
Your code is too complex and not complete enough to correct it, but the situation looks like this one :
public class Bug extends JPanel {
private JTextArea textArea = new JTextArea(); // first text area
private void build() {
JTextArea textArea = new JTextArea(); // second text area. Inaccessible outside of this method
this.add(new JScrollPane(textArea));
}
public void actionPerformed(ActionEvent e) {
this.textArea.setText("foo"); // here, we modify the first text area, but it hasn't been added to the GUI, so it's invisible
}
}
To fix it, you have to change it to this :
public class NoBug extends JPanel {
private JTextArea textArea = new JTextArea(); // unique text area
private void build() {
this.add(new JScrollPane(this.textArea));
}
public void actionPerformed(ActionEvent e) {
this.textArea.setText("foo"); // here, we modify the unique text area, which has been added to the GUI in the build() method }
}