Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2006

Tip: Looking for answers? Try searching our database.

Problem to update JTextArea with ScrollPane

Thread view: 
Petterson Mikael - 17 Nov 2005 15:07 GMT
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]
>
> }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.