2

I would ask something about my project. I have a Java program that connects to a website through a Connection class that takes as parameter an int. This class has only a method that return an ArrayList (it gets informations from a web page , and puts results in an arraylist).

In the Main I have a for loop:

for(int i=0;i<insertUserNumber; i++){} 

Inside this loop I invoke a Connection object that gets as parameter the "i" of loop and when the object returns the ArrayList, I take it, do something with it and show the result inside a JOptionPane.

The problem is that ONLY when i click on OK i see the other JOptionPane with the result of operation did in the loop. I wish to see them at the same time so I could see all data.

From Javadoc

All dialogs are modal. Each showXxxDialog method blocks the caller until the user's interaction is complete.

So how can I resolve the problem?

1)Is using multithreading a good solution or there are better ones ?

2) is there a way where I can refresh automatically data of the JoptionPane without start the program?

Thanks and sorry for my bad english.

Heisenbug
  • 38,762
  • 28
  • 132
  • 190
Strom
  • 129
  • 1
  • 2
  • 8

2 Answers2

3

is there a way where I can refresh automatically data of the JoptionPane without start the program?

Certainly. Show a JPanel in it that uses a CardLayout, as shown here.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
2

For which concern the first part of the question:

you can't show multiple JDialog if you use showXXXDialog method, because they are modal. On the other hand if you create a JDialog object you can show it as much as you want:

JDialog first = new JDialog();
first.setSize(new Dimension(80,80));
JDialog second = new JDialog();
second.setSize(new Dimension(80,80));

first.setVisible(true);
second.setVisible(true);

For what concern changing dynamically the data displayed inside the dialog, yes it's also possible. The first parameter of all showXXXDialog methods is a frame (thus can be a JDialog object too). You can do something like:

JDialog dialog = new JDialog();
JPanel p = new JPanel ();
p.setLayout(new FlowLayout());
JLabel label = new JLabel("FOO");
p.add(label);
dialog.add(p);

JOptionPane.showXXXDialog(dialog,....);

If you change the content of JLabel label everywhere else inside your program, your dialog will be automatically updated.

Heisenbug
  • 38,762
  • 28
  • 132
  • 190
  • In this way i dont have to click OK to see the other "window" right? – Strom Sep 20 '11 at 16:39
  • No, you don't have. But I'm not sure what you are trying to do is a good thing. Having several opened dialog can confuse the user. Anyway if that's what you want I think you can try to play around a bit with this solution. – Heisenbug Sep 20 '11 at 16:41
  • The problem is when users want compare the data. How i could do it without show several opened dialog? I wish show only 1 with both datas but i dont have idea how do it.By the way if you want suggest something i will be happy. Thanks – Strom Sep 20 '11 at 16:58