I have a JScrollPane with JTable that is filled from MySQL. I'm using SwingWorker to display JScrollPane while reading data from MySQL database. There is no problem with it. My question is how to add a text "Loading..." to JScrollPanel? I just want to display some message in this UI component while JTable is not filled with data from MySQL database. And I cannot find the way to do that.

Thanks in advance.

Remove the JTable and the JLabel, then remove the JLabel and add the JTable.

Do you mean that I must create JLable and add it to JScrollPane, and then remove it?

Well, I can't see the label text...

table = new JTable();
      jScrollPane1 = new JScrollPane(table, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
      int deltax = (int) (0.65 * SystClasses.Form.panfortab.getPreferredSize().width);
      int deltay = (int) (0.5 * SystClasses.Form.panfortab.getPreferredSize().height);
      jScrollPane1.setPreferredSize(new Dimension(deltax, deltay));

      dummyLab = new JLabel("Connecting to the database. Please wait...");
 SwingWorker worker = new SwingWorker<Void, Void>() {
           public void done() {

If I add the JTable and remove it in the "done" method, then I can't see JTable at all. Where is my error?


jScrollPane1.setViewportView(new JLabel("Connecting to the database. Please wait...")

and when the table is ready


Great! It is exactly what I was looking for.

One question more is how to make top alignment of this message? I've tried the following:


... but it doesn't work.

You need to set the alignment on the label. It's filling the scrollpane by default.


very nice! thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.