Avatar of aerol
aerol
 asked on

Display Java Dialog before loading widgets

I have a fairly simple JDialog. There are some buttons and a JTable on the Dialog. I have a fair amount of data to load into the JTable and it takes 10-15 seconds. All is working well but the Dialog has not painted while the data is being retrieved and placed in the JTable. I would like for the Dialog to have been drawn and THEN I can load the data and I can bring up a WAIT cursor to let the user that something is happening. How can I delay the JTable load until after the Dialog is fully displayed??

Thanks!
Editors IDEsJava

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
CEHJ

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
dnunes_br

You can use such class to do your wait cursor feature:


public class WaitCursorManager{
    private final static MouseAdapter mouseAdapter =
        new MouseAdapter() {};
 
      private WaitCursorManager() {}
 
      /** Sets cursor for specified component to Wait cursor */
      public static void startWaitCursor(JComponent component) {
        RootPaneContainer root =
          ((RootPaneContainer) component.getTopLevelAncestor());
        root.getGlassPane().setCursor(new Cursor(Cursor.WAIT_CURSOR));
        root.getGlassPane().addMouseListener(mouseAdapter);
        root.getGlassPane().setVisible(true);
      }
 
      /** Sets cursor for specified component to normal cursor */
      public static void stopWaitCursor(JComponent component) {
        RootPaneContainer root =
          ((RootPaneContainer) component.getTopLevelAncestor());
        root.getGlassPane().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        root.getGlassPane().removeMouseListener(mouseAdapter);
        root.getGlassPane().setVisible(false);
      }
}

Open in new window

dnunes_br

One idea that I do is use a progress bar on your main window to show the progress of the opening window combined with the wait cursor. In you JDialog code, have a method that loads all the data you need, and afterwards use your jdialog.setVisible(true);
CEHJ

If you do it as i mentioned, you won't need a progress bar, as the user will see the table filling. Here's an example that downloads the UK lottery results:
import java.awt.event.*;
 
import java.io.*;
 
import java.net.*;
 
import java.util.*;
 
import javax.swing.*;
import javax.swing.table.*;
 
 
/**
 * DOCUMENT ME!
 *
 * @author $author$
 * @version $Revision$
 */
public class Lottery extends JFrame {
    private static int NUM_HEADERS = 10;
    private JTable table;
    private DefaultTableModel tableModel;
 
    public void setGui() {
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	tableModel = new DefaultTableModel(new Vector(), makeColumnHeaders());
	table = new JTable(tableModel);
	table.addMouseListener(new RowSorter());
 
	JScrollPane sp = new JScrollPane(table);
	getContentPane().add(sp);
    }
 
    private Vector makeColumnHeaders() {
	String headers = "Draw Date, Ball 1, Ball 2, Ball 3, Ball 4, Ball 5, Ball 6,  Bonus Ball, Ball Set, Machine";
 
	return new Vector(Arrays.asList(headers.split("\\s*,\\s*")));
    }
 
    public void start() {
	new TableFiller().execute();
    }
 
    public static void main(String[] args) {
	Lottery reader = new Lottery();
	reader.setGui();
	reader.setSize(600, 300);
	reader.setVisible(true);
	reader.start();
    }
 
    private class RowSorter extends MouseAdapter {
	public void mouseClicked(MouseEvent e) {
	    int[] selection = table.getSelectedRows();
	    Set numbers = new TreeSet();
	    for (int i = 0; i < selection.length; i++) {
		for(int col = 1;col <= 6;col++) {
		    int row = selection[i];
		    if (row == 0) {
			continue;
		    }
		    String val = tableModel.getValueAt(row, col).toString();
		    val = String.format("%02d", Integer.parseInt(val));
		    numbers.add(val);
		}
	    }
	    JOptionPane.showMessageDialog(Lottery.this, numbers);
	}
    }
 
    private class TableFiller extends SwingWorker {
	public Object doInBackground() {
	    Vector allRows = new Vector();
	    String line = null;
 
	    try {
		int rowCount = 0;
		BufferedReader in = new BufferedReader(new InputStreamReader(
			    new URL(
				"http://www.national-lottery.co.uk/player/files/Lotto.csv").openStream()));
 
		while ((line = in.readLine()) != null) {
		    String[] tokens = line.split("\\s*,\\s*");
		    rowCount++;
 
		    if (tokens.length == NUM_HEADERS) {
			Vector row = new Vector(Arrays.asList(tokens));
			//System.out.println(row);
			//allRows.add(row);
			publish(row);
		    }
		}
		System.out.printf("%d row(s) found\n", rowCount);
 
		in.close();
	    } catch (IOException e) {
		e.printStackTrace();
	    }
 
	    return allRows;
	}
 
	public void process(List x) {
	    Vector row = (Vector) x.get(0);
	    tableModel.addRow(row);
	}
    }
}

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
aerol

ASKER
Yes. I should have thought of threading. Worked like a charm. Thanks!
CEHJ

:-)