0

I have a JFrame class

public class SettingsFrame extends JFrame {
 public FirstSettingsFrame() throws HeadlessException {
    setTitle("Settings");
    setSize(600, 400);
    ...
    JButton searchModels = new JButton("Start");
    searchManyModels.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent actionEvent) {

            JFrame frame = new JFrame();
            frame.setSize(new Dimension(800, 600));
            frame.setLayout(null);
            frame.setVisible(true);

            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(800);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                JButton button = new JButton("Test");
                button.setBounds(i * 10, i * 10, 20, 20);
                frame.add(button);
                System.out.println("i = " + i);
            }
        }
    });

 add(searchModels);
 ...
}

Nothing is added, while loop is processing. All buttons are added only when loop is finished. And i want them to be added while looping. How can i manage this?

EDITED:

And this code

 @Override
        public void actionPerformed(ActionEvent actionEvent) {


            JFrame frame = new JFrame();
            JPanel panel = new JPanel();

            frame.setSize(new Dimension(800, 600));

            frame.add(panel);
            frame.pack();
            frame.setVisible(true);

            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(800);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                JButton button = new JButton("Test");
                button.setBounds(i * 10, i * 10, 20, 20);
                panel.add(button);

                panel.revalidate();
                panel.repaint();
                frame.pack();
                System.out.println("i = " + i);
            }
        }
    });

Only changes frame size, not adding anything while looping, too

EDITED 2:

deleting Thread.sleep and writing loop as for (long i = 0; i < 100000000000l; i++) { doesn't affects at all.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user1439231
  • 21
  • 1
  • 4
  • 2
    `deleting Thread.sleep and writing loop as for (long i = 0; i < 100000000000l; i++) { doesn't affects at all.` - that was not the suggestion. Whether you use Thread.sleep() or block the hog the CPU will a silly loop, you are still blocking the EDT so the GUI can't reapint itself. Spend some time to read the tutorial to understand the basics of how Swing works and don't try to take shortcuts! – camickr Apr 07 '14 at 16:40
  • Yes, it works with timer, but i don't need timer. I put here Thread.sleep only for simulating real long-time staff, what is processing in every iteration(get data from internet, modify, save new data, (here repaint frame!), go to next iteration). So it always takes random time, so timer is useless here. How can i manage with this problem? – user1439231 Apr 07 '14 at 16:50
  • 1
    `get data from internet,` - You can't block the EDT when processing data from the internet. You need to use a separate Thread for the internet processing. Read the tutorial on `Concurrency` like I suggested. You would probably use a `Swing Worker` in this case. – camickr Apr 07 '14 at 16:53
  • 1) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). .. – Andrew Thompson Apr 08 '14 at 02:23
  • .. 2) Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling `Thread.sleep(n)` implement a Swing `Timer` for repeating tasks or a `SwingWorker` for long running tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Apr 08 '14 at 02:24

1 Answers1

4

Don't use a null layout!!!

Swing was designed to be used with layout managers. Then the basic code for adding/removing components from a visible GUI is:

panel.add(...);
panel.revalidate(); // to invoke layout manager 
panel.repaint(); // to paint components. 

Read the section from the Swing tutorial on How to Use Layout Managers for more information.

All buttons are added only when loop is finished. And i want them to be added while looping. How can i manage this?

The Thread.sleep() is blocking the Event Dispatch Thread, so the GUI can't repaint itself until the loop is finished executing. Look at the Table of Contents from the link I gave above and read the section from the tutorial on Concurrency for more information. You should be using a Swing Timer (which is also a topic in the tutorial), not a loop.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • 2
    @user1439231, There is no way you had time to read the tutorial link I provided you in order to understand my suggestion and implement the Swing Timer. Post your [SSCCE](http://www.sscce.org/) showing how you used a `Swing Timer`. – camickr Apr 07 '14 at 16:41