Eleco
Eleco

Reputation: 3244

Superimposing Swing components

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...

Upvotes: 4

Views: 461

Answers (5)

Rorick
Rorick

Reputation: 8953

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.

Upvotes: 0

walter
walter

Reputation: 31

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.

Upvotes: 0

John Doe
John Doe

Reputation: 867

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

Upvotes: 0

nanda
nanda

Reputation: 24788

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

}

Upvotes: 1

willcodejavaforfood
willcodejavaforfood

Reputation: 44063

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.

Upvotes: 4

Related Questions