tags:

views:

96

answers:

5

Is it possible at all to have one component superimposed (or stacked) over another in Swing?

I'm thinking about having a progressbar (in the foreground) which would be sitting right on top of a JTable (in the background) while the table initialisation is ongoing...

A: 

I'm far form being a guru on Swing programming so my answer will be not too precise. Answer is yes =). You can take a look on panes of JFrame and place your progress bar in glass pane or layered pane.

Rorick
+1  A: 

For your case, I suggested you to create a custom Table extending JTable and then adding the ProgressBar as child of the custom component.

A very rough implementation:

public class TableProgress extends JTable {

    public TableProgress() {
        JProgressBar comp = new JProgressBar();
        add(comp);
        comp.setLocation(100, 100);
        comp.setSize(100, 100);
    }

    public static void main(String[] args) {
        JFrame jFrame = new JFrame();
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setLayout(new BorderLayout());
        jFrame.setPreferredSize(new Dimension(500, 500));
        TableProgress comp = new TableProgress();
        jFrame.getContentPane().add(comp, BorderLayout.CENTER);

        jFrame.pack();
        jFrame.setVisible(true);
    }

}
nanda
+4  A: 

Yes, there are a few ways of doing this.

This could be done by adding it to the glass pane but this will normally block your UI.

I would look into JXLayer which allows you to wrap components to perform additional paint jobs. There is also a JBusyComponent which relies on this library that probably does what you want.

willcodejavaforfood
A: 

A do it yourself way, could be to use the glasspane :

  • make it visible and paint a progressbar
  • once the table is initialized, remove components from the glasspane and hide it
John Doe
A: 

I would use JLayeredPanes. Put the table on the bottom panel and add another panel on top for the progress bar. If you want to block table input, make the progress bar panel cover the table and attach a mouse listener to it.

walter