Overlay Swing Components

Is it even possible to have one component superimposed (or complex) on top of another in Swing ?

I am thinking about a progress indicator in the foreground (which will sit right above the JTable (in the background) as table initialization continues ...

+4
source share
5 answers

Yes, there are several ways to do this.

This can be done by adding it to the glass panel, but this usually blocks your user interface.

I would take a look at JXLayer , which allows you to wrap components for additional drawing tasks. There is also a JBusyComponent that relies on this library, which probably does what you want.

+4
source

In your case, I suggested you create a custom table that extends JTable, and then add a ProgressBar as a child of the custom component.

Very crude 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); } } 
+1
source

I am far from being a guru in Swing programming, so my answer will not be too precise. Answer: yes =). You can take a look at the JFrame panel and place the progress bar in a glass panel or a layered panel.

0
source

Do it yourself, maybe use a double-glazed window:

  • make it visible and draw a progress bar
  • After initializing the table, remove the components from the glass and hide it
0
source

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

0
source

Source: https://habr.com/ru/post/1300383/


All Articles