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 / April 2008

Tip: Looking for answers? Try searching our database.

Newbie: Update of window

Thread view: 
frodefi@gmail.com - 03 Apr 2008 14:57 GMT
Hi!

I am new to Java and Swing. I have tried to search the net (including
comp.lang.java.gui FAQ), but I cannot figure out what is wrong with
the following test of swing. The window only updates when I am
changing the size of the window. How do I get the program to update
the window all by itself?

The program genereates a random number of lines, and is adding a
random number of lines when pushing a button.

Thanks in advance for any help!

public class Test implements ActionListener {

   Box box;
   int lines;
   Random generator = new Random();

   Test() {
       JFrame jfrm = new JFrame("Random Lines Test");
       jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       box = Box.createVerticalBox();
       int random = generator.nextInt(9)+1;
       for (int i = 1; i <= random; i++) {
           box.add(new JLabel("Line number " + i));
       }
       lines = random;
       JButton jbRandom = new JButton("Add a random (1-9) number of
lines");
       jbRandom.addActionListener(this);
       jfrm.add(box);
       jfrm.add(jbRandom, BorderLayout.SOUTH);
       jfrm.pack();
       jfrm.setExtendedState(Frame.MAXIMIZED_BOTH);
       jfrm.setVisible(true);
   }

   public void actionPerformed(ActionEvent ae) {
       int random = generator.nextInt(9)+1;
       for (int i = lines; i < lines+random; i++) {
           box.add(new JLabel("Line number " + i));
       }
       lines+=random;
   }

   public static void main(String[] args) {
       SwingUtilities.invokeLater(new Runnable() {
           public void run() {
               new Test();
           }
       });
   }

}
Andrea Francia - 03 Apr 2008 15:39 GMT
> Hi!
>
[quoted text clipped - 51 lines]
>
> }

Use a JList instead of a Box will solve.

If you want to use a Box then call jfrm.pack() at the  end of
actionPerformed() method.

Signature

Andrea Francia
http://www.andreafrancia.it/

Knute Johnson - 03 Apr 2008 17:17 GMT
> Hi!
>
[quoted text clipped - 51 lines]
>
> }

When you add or remove components from an already visible Frame you must
call validate() on the Frame to cause the LayoutManager to re-layout the
components.  Then they will be visible.

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class Test implements ActionListener {
    JFrame jfrm;
    Box box;
    int lines;
    Random generator = new Random();

    Test() {
        jfrm = new JFrame("Random Lines Test");
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        box = Box.createVerticalBox();
        int random = generator.nextInt(9)+1;
        for (int i = 1; i <= random; i++) {
            box.add(new JLabel("Line number " + i));
        }
        lines = random;
        JButton jbRandom = new JButton("Add a random (1-9) number of
lines");
        jbRandom.addActionListener(this);
        jfrm.add(box);
        jfrm.add(jbRandom, BorderLayout.SOUTH);
        jfrm.pack();
        jfrm.setExtendedState(Frame.MAXIMIZED_BOTH);
        jfrm.setVisible(true);
    }

    public void actionPerformed(ActionEvent ae) {
        int random = generator.nextInt(9)+1;
        for (int i = lines; i < lines+random; i++) {
            box.add(new JLabel("Line number " + i));
        }
        lines+=random;
        jfrm.validate();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Test();
            }
        });
    }
}

Signature

Knute Johnson
email s/nospam/linux/

     ------->>>>>>http://www.NewsDem

all@singletask.com - 03 Apr 2008 23:11 GMT
jfrm.pack(); did not help...
but jfrm.validate(); did the trick!

Thanks for your help! :-)

On Apr 3, 6:17 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> frod...@gmail.com wrote:
> > Hi!
[quoted text clipped - 116 lines]
>
> - Show quoted text -
frodefi@gmail.com - 04 Apr 2008 00:11 GMT
jfrm.pack(); did not help, perhpas I did something wrong... But JList
definately looks like the thing for me.

Anyway, jfrm.validate() did the trick for this test-case.

Thanks for your help! :-)

On Apr 3, 6:17 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> frod...@gmail.com wrote:
> > Hi!
[quoted text clipped - 116 lines]
>
> - Show quoted text -


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.