WilliamShatner
WilliamShatner

Reputation: 926

How do you update a JDialog with new information?

I have a Gui which allows the user to click a button and view the contents of a text file. The issue is that the file can be rather large (100,000+ rows of data) and can take upwards of 15 seconds to read and display in a table.

An example of the format of the text file:

*/
    Account ID     : 8 digit number
    Money Charged  : Integer < $10
    Values separated by \t 

/*


Account Id    Money Charged
----------    --------------
731298        3
359412        5
624937        1

So when the button is clicked, it reads each line and puts each id into a map and increases the charges of the accounts.

Once it is finished it will place a JTable inside a JDialog with the information. However, as soon as the button is clicked, the user could be confused as to what the program is doing as I've experienced upwards of 10-15 seconds of delay before the JDialog & JTable appears.

So is there a way to make the JDialog appear with a string letting the user know that it is creating the table? (I thought it would be done using one of the JDialog methods such as repaint() or validate but those didn't seem to do the trick.

Here is the order I'm thinking it should be done:

//Psuedo-code
actionPerformed {
    create frame
    display label that lets user know its creating table
    read text file
    create map and place values
    remove label
    update dialog with table // unsure what to do to update it properly
}

So my overall question is simple: Is it possible to inform the user that it is currently calculating the data (via JLabel in the JDialog) and once the data is read, display the JTable (by updating the JDialog)?

If needed, I can provide source. Not sure it's really needed though

Edit

public void actionPerformed(ActionEvent event) {
    JFrame frame = new JFrame();
    JDialog dialog = new JDialog(frame, "Account Charges", true);
    JLabel label = new JLabel("Currently calculating the charges")

    dialog.getContentPane().add(label);       //add waiting label
    dialog.setVisible(true);              
    readData(myFile);                         //read file and create table
    dialog.getContentPane().remove(label);    //waiting label no longer needed
    dialog.getContentPane().add(myJtable);    //update dialog with the table
}

Upvotes: 1

Views: 4315

Answers (3)

mKorbel
mKorbel

Reputation: 109823

1) I have a Gui which allows the user to click a button and view the contents of a text file.

  • use CardLayout, I'd to suggest to create another Container if you really needed to block or provide input mask for input / amend / delete of value that is already displayed or exist

2) The issue is that the file can be rather large (100,000+ rows of data) and can take upwards of 15 seconds to read and display in a table.

  • I don't know any man except the internal revision, that could be want to check or working with 100k rows, use SwingWorker fills FileIO to the TableModel, and create paginations for JTable

3) //Psuedo-code

actionPerformed {

create frame - not JFrame re_use existing JDialog, sure better could be CardLayout

display label that lets user know its creating table - no idea let it be

read text file - use SwingWorker with JProgressBar, to transform data and put that to the TableModel, use batch for update of TableModel f.e. every 50rows, look for paginations for JTable, then you'll display only required numbers of rows, not 100k, this could be crazy :-)

create map and place values - could be useless because you are storing all required informations in the TableModel

remove label - no idea let it be

update dialog with table - Jtable could be updated immediatelly from SwinWorker, Swing GUI could be accesible for all Mouse or KeyBoards inputs, never waiting, nor delay at 10-15sec

}

Upvotes: 5

MarioDS
MarioDS

Reputation: 13073

The simplest way to use your solution is to do label.setVisible(false) when your operation is complete. Of course your lay-outmanagers (or otherwise a good free design) should be made so that the other components resize/reposition properly when the label becomes invisible. Normally it should happen automatically.

There will be other ways to do it, but you'll spend more time making them.

Upvotes: 2

Anthony
Anthony

Reputation: 496

JDialogs are able to hold components inside of them, such as the table you are talking about. You can simply tell the user what you are doing by using JDialog's getContentPane method to add/delete components such as a progress bar and label, so that they are notified that the program is working.

Also, correct me if I'm wrong, but repaint() is usually a method used by java.awt packages, and not so much javax.swing ... only on certain occasions.

-- Another Solution -- You could tell the user what's happening before the dialog even opens up. Maybe have a 'Current Status' label on the GUI or a progress bar. When the file is completely read and is ready to be used, you can open the dialog then and quickly post the values on the table.

Upvotes: 4

Related Questions