Hello!

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.

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...");
      dummyLab.setVisible(true);
      jScrollPane1.add(dummyLab);
...
 SwingWorker worker = new SwingWorker<Void, Void>() {
...
           @Override
           public void done() {
                  jScrollPane1.remove(dummyLab);
...

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

Edited 6 Years Ago by LianaN: n/a

Try

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

and when the table is ready

jScrollPane1.setViewportView(table);

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:

jScrollPane1.setAlignmentX(Component.TOP_ALIGNMENT);
      jScrollPane1.setAlignmentY(Component.TOP_ALIGNMENT);

... but it doesn't work.

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

label.setVerticalAlignment(SwingConstants.TOP);

Edited 6 Years Ago by Ezzaral: n/a

This question has already been answered. Start a new discussion instead.