Hi,
I have added a scrollpane to my jtextArea. Then I have implemented an
UpdateListener interface to handle events fired from my Model class ( I
am using the MVC). The problem I am facing is:
I add the scrollpane to the jtextarea and then I add the scrollpane to
the jframe. I don't know how to update the jtextarea.
This is the code for adding the jtextarea with scrollpane to the jframe:
result = new JTextArea();
result.setEditable(false);
// Add scroll bar when a lot window cannot show all results.
scroll = new JScrollPane(result,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
gridbag.setConstraints(scroll, resultConstraints);
this.add(scroll);
This is the code for my handling an update that occured:
public void updateOccurred(UpdateEvent event) {
result.append( event.getStatus()+newline );
System.out.println("Updated result!");
}
What am I doing wrong?
Cheers,
//Mikael
The whole class is here:
========================
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Lab2View extends JFrame implements UpdateListener {
private JTextField numerator, denominator;
private JTextArea result;
private Exercise2Controller controller;
private JLabel numeratorLabel, denominatorLabel, resultLabel;
private final static String newline = "\n";
private JScrollPane scroll;
public Lab2View() {
super("Exercise 2");
GridBagLayout gridbag = new GridBagLayout();
this.setLayout(gridbag);
// Add Panel with numerator text
GridBagConstraints numeratorTextConstraints = new GridBagConstraints();
numeratorTextConstraints.gridx = 0;
numeratorTextConstraints.gridy = 0;
numeratorTextConstraints.gridwidth = 2;
numeratorTextConstraints.gridheight = 1;
numeratorTextConstraints.fill = GridBagConstraints.HORIZONTAL;
numeratorLabel = new JLabel("Enter numerator ");
numeratorLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
gridbag.setConstraints(numeratorLabel, numeratorTextConstraints);
this.add(numeratorLabel);
// Add numeratorField
GridBagConstraints numeratorConstraints = new GridBagConstraints();
numeratorConstraints.gridx = 2;
numeratorConstraints.gridy = 0;
numeratorConstraints.gridwidth = 4;
numeratorConstraints.gridheight = 1;
numerator = new JTextField(12);
gridbag.setConstraints(numerator, numeratorConstraints);
this.add(numerator);
// Add Panel with denominator text
GridBagConstraints denominatorTextConstraints = new GridBagConstraints();
denominatorTextConstraints.gridx = 0;
denominatorTextConstraints.gridy = 1;
denominatorTextConstraints.gridwidth = 2;
denominatorTextConstraints.gridheight = 1;
denominatorTextConstraints.fill = GridBagConstraints.HORIZONTAL;
denominatorLabel = new JLabel("Enter denominator and press Enter ");
denominatorLabel.setAlignmentX(JLabel.RIGHT_ALIGNMENT);
gridbag.setConstraints(denominatorLabel, denominatorTextConstraints);
this.add(denominatorLabel);
// Add denominatorField
GridBagConstraints denominatorConstraints = new GridBagConstraints();
denominatorConstraints.gridx = 2;
denominatorConstraints.gridy = 1;
denominatorConstraints.gridwidth = 4;
denominatorConstraints.gridheight = 1;
denominator = new JTextField(12);
gridbag.setConstraints(denominator, denominatorConstraints);
this.add(denominator);
// //listen to action and send input to contoller.
denominator.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
// send data to the controller
controller.input(numerator.getText(), denominator.getText());
}
}); // end addActionListener
// Add result text
GridBagConstraints resultTextConstraints = new GridBagConstraints();
resultTextConstraints.gridx = 0;
resultTextConstraints.gridy = 2;
resultTextConstraints.gridwidth = 2;
resultTextConstraints.gridheight = 1;
resultTextConstraints.fill = GridBagConstraints.HORIZONTAL;
resultLabel = new JLabel("RESULT:");
resultLabel.setAlignmentX(JLabel.RIGHT_ALIGNMENT);
gridbag.setConstraints(resultLabel, resultTextConstraints);
this.add(resultLabel);
// Add result text
GridBagConstraints resultConstraints = new GridBagConstraints();
resultConstraints.gridx = 2;
resultConstraints.gridy = 2;
resultConstraints.gridwidth = 4;
resultConstraints.gridheight = 1;
resultConstraints.fill = GridBagConstraints.HORIZONTAL;
result = new JTextArea();
result.setEditable(false);
// Add scroll bar when a lot window cannot show all results.
scroll = new JScrollPane(result,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
gridbag.setConstraints(scroll, resultConstraints);
this.add(scroll);
}
// set the controller to be used
public void setController(Exercise2Controller controller) {
this.controller = controller;
}
public void updateOccurred(UpdateEvent event) {
result.append( event.getStatus()+newline );
System.out.println("Updated result!");
}
}
pit.grinja@gmx.de - 17 Nov 2005 19:21 GMT
Hi mikael
Registering your Lab2View instance as an UpdateListener for your
"result" JTextArea should do it.
BTW: It was a little difficult to figure out that you have not done the
above, since I had to dig around a little. > 80 % of the code deals
with placing components in a GridbagLayout, and that has nothing to do
with an apparently deaf listener.
HTH
Piet
Benoit Peltier - 11 Jan 2006 16:45 GMT
Hello Mikeal
I'm probably not reading your code right but, if you want a scrolled
JTextArea here is the way I think it should be done :
// contruct a scrollable JTextArea
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane( textArea );
// then to add this container (scrollPane) to the JFrame
frame.getContentPane().add( scrollPane, BorderLayout.CENTER );
The only GUI bug that I had is constructing the scrollable JTextArea and
assigning it pragmatically. The JTextArea does not always refresh itself. I
must add these lines :
// example on a button click
public void actionPerformed( ActionEvent event ) {
frame.getContentPane().add( scrollPane );
// now to refresh the darn JTextArea
textArea.repaint();
textArea.revalidate();
}
Hope this helps ...
Ben
> Hi,
>
[quoted text clipped - 153 lines]
>
> }