Asif
Asif

Reputation: 5038

Force JDialog to get visible before proccessing data?

I have created a JDialog in NetBeans and a custom constructor as follows:

public AnimatedProgress(java.awt.Frame parent, boolean modal, JTable table) {
    super(parent, modal);
    initComponents();
    setLocationRelativeTo(parent);
    progressLabel.setText("Collecting Table Data. . .");
    Object[][] data = getJTableData(table); // Simple method to collect data and store in Object[][] array
    progressLabel.setText("Processing Data. . .");
    processData(data);
    progressLabel.setText("Data Processed. . .");
}

Now I called this JDialog as:

new AnimatedProgress(this, true, dataTable).setVisible(True);

My problem is, as Java calls the constructor, all the codes in constructor gets executed first and then the dialog appears with final result.

How can I make my JDialog appears first and then process the methods: getTableData() and processData()??

Upvotes: 1

Views: 940

Answers (2)

Eng.Fouad
Eng.Fouad

Reputation: 117647

Here is a sample use of SwingWorker:

public class BackgroundThread extends SwingWorker<Void, Void>
{
    private JTable table;

    public BackgroundThread(JTable table)
    {
        this.table = table;
    }

    @Override
    public Void doInBackground() throws Exception
    {
        /*
            If getJTableData() or processData() are not static,
            pass a reference of your class which has these methods
            and call them via that reference
        */
        Object[][] data = getJTableData(table);
        publish("Processing Data. . .");
        processData(data);
        publish("Data Processed. . .");
        return null;
    }

    @Override
    public void process(List<String> chunks)
    {
        for(String chunk : chunks) progressLabel.setText(chunk);
    }
}

Then change you constructor to this:

public AnimatedProgress(java.awt.Frame parent, boolean modal, JTable table)
{
    super(parent, modal);
    initComponents();
    setLocationRelativeTo(parent);
    setVisible(true);
    new BackgroundThread(table).execute();
}

I didn't test it, but I hope it works.

Upvotes: 4

dexametason
dexametason

Reputation: 1133

The JDialog can't be visible before it is created ==> constructor must be executed fully before visibility change.
I think you should create a myInitialize() method that contains the data fill-ups. First you make the dialog visible by calling the constructor as now and after that you can call your myIntialize() method to fill up the components with the proper data.

Upvotes: 0

Related Questions