Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #3351

Re: Newbie: Update of win

From "Andrea Francia" <andrea.francia@THRWHITE.remove-dii-this>
Subject Re: Newbie: Update of win
Message-ID <47f4ec21$0$4793$4fafbaef@reader4.news.tin.it> (permalink)
Newsgroups comp.lang.java.gui
References <98e6fb07-96e2-4d48-b15d-306e77019ef0@s37g2000prg.googlegroups.co
Date 2011-04-27 15:44 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.gui
frodefi@gmail.com wrote:
> 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();
>             }
>         });
>     }
> 
> }

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.

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

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | Next | Find similar | Unroll thread


Thread

Re: Newbie: Update of win "Andrea Francia" <andrea.francia@THRWHITE.remove-dii-this> - 2011-04-27 15:44 +0000

csiph-web