Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java GUI > Newbie: Update ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 5 Topic 9623 of 9959
Post > Topic >>

Newbie: Update of window

by frodefi@[EMAIL PROTECTED] Apr 3, 2008 at 06:57 AM

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 pu****ng 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();
            }
        });
    }

}
 




 5 Posts in Topic:
Newbie: Update of window
frodefi@[EMAIL PROTECTED]  2008-04-03 06:57:06 
Re: Newbie: Update of window
Andrea Francia <andrea  2008-04-03 16:39:28 
Re: Newbie: Update of window
Knute Johnson <nospam@  2008-04-03 09:17:12 
Re: Newbie: Update of window
all@[EMAIL PROTECTED]   2008-04-03 15:11:02 
Re: Newbie: Update of window
frodefi@[EMAIL PROTECTED]  2008-04-03 16:11:21 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Oct 10 14:29:31 CDT 2008.