Here is the issue I am having. I have a JToolBar
that contains 3 JPanel
's and each panel contains some various components. The panel that contains my JProgressBar
is only visible when there is something loading. What happens when it changes from visible to not visible is my other two panels shift by approx 1 pixel. Here is the code:
private JProgressBar progressBar = new JProgressBar(JProgressBar.HORIZONTAL);
private JPanel progressPanel = new JPanel();
private JPanel globalPanel = new JPanel();
private JPanel cameraPanel = new JPanel();
private JLabel cameraLabel = new JLabel("Camera: ");
private JLabel cameraCoords = new JLabel();
private JLabel globalLabel = new JLabel("Global: ");
private JLabel globalCoords = new JLabel();
progressPanel.setLayout(new BoxLayout(progressPanel, BoxLayout.X_AXIS));
progressPanel.setBackground(Color.RED);
globalPanel.setLayout(new BoxLayout(globalPanel, BoxLayout.X_AXIS));
globalPanel.setBackground(Color.BLUE);
cameraPanel.setLayout(new BoxLayout(cameraPanel, BoxLayout.X_AXIS));
cameraPanel.setBackground(Color.GREEN);
progressBar.setFocusable(false);
progressBar.setPreferredSize(new Dimension(100,0));
progressBar.setMaximumSize(new Dimension(150,20));
progressBar.setStringPainted(true);
//progressBar.setAlignmentY(Component.CENTER_ALIGNMENT);
progressPanel.add(progressBar);
//globalLabel.setAlignmentY(Component.CENTER_ALIGNMENT);
//globalCoords.setAlignmentY(Component.CENTER_ALIGNMENT);
globalPanel.add(globalLabel);
globalPanel.add(globalCoords);
//cameraLabel.setAlignmentY(Component.CENTER_ALIGNMENT);
//cameraCoords.setAlignmentY(Component.CENTER_ALIGNMENT);
cameraPanel.add(cameraLabel);
cameraPanel.add(cameraCoords);
this.setBorder(new EmptyBorder(5,5,5,5));
this.setPreferredSize(new Dimension(0,30));
this.add(progressPanel);
this.add(Box.createHorizontalGlue());
this.addSeparator();
this.add(globalPanel);
this.addSeparator();
this.add(cameraPanel);
this.setFloatable(false);
Now when ever I set progressPanel.setVisible(false)
the JLabel
's shift by a pixel. Where I have the alignment commented out is where I tried to get them to align, but this still did not work. What am I doing wrong here?