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.

Recommended Answers

All 7 Replies

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...");
      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?

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);

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.