0

So for this program, I am trying to have a JToolBar on the left, and this spectrum panel on the right side. I am currently adding using a BorderLayout, but as you can see, the spectrum (in cyan) I add has a black border around it (the panel below). Why does it not fill the right side JPanel?

https://i.stack.imgur.com/POkbi.jpg

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToolBar;

public class Spectrum extends JFrame{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /* 
     * 
     */

    public Spectrum(){
        JPanel main = new JPanel(new BorderLayout());
        JPanel rightside = new JPanel();
        JLabel spectrum = new JLabel("spectrum goes here");
        JToolBar toolbar = new JToolBar(null, JToolBar.VERTICAL);
        JButton button1 = new JButton("Icon 1");
        JButton button2 = new JButton("Icon 2");

        main.setBackground(Color.RED);
        main.setPreferredSize(new Dimension(800, 500));

        rightside.setBackground(Color.black);

        spectrum.setPreferredSize(new Dimension(750,500));
        spectrum.setOpaque(true);
        spectrum.setBackground(Color.cyan);

        toolbar.setPreferredSize(new Dimension(50, 500));
        toolbar.setFloatable(false);

        button1.setOpaque(true);
        button2.setOpaque(true);
        button1.setBackground(Color.blue);
        button2.setBackground(Color.green);

        toolbar.add(button1);
        toolbar.add(button2);

        rightside.add(spectrum);
        main.add(toolbar, BorderLayout.WEST);
        main.add(rightside, BorderLayout.EAST);

        setContentPane(main);

        pack();
        setVisible(true);
    }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
bforcer
  • 147
  • 3
  • 13
  • Add rightside to the center... main.add(rightside, BorderLayout.CENTER); Avoid tampering with the PreferredSizes too much. Swing will lay your components out well anyway. – Costis Aivalis May 20 '13 at 05:26
  • if i remove the preffered size, and try to add rightside (BO.center) it just takes up the whole frame and covers the toolbar – bforcer May 20 '13 at 05:32
  • Comment out just the setPreferredSize of the rightside... //spectrum.setPreferredSize(new Dimension(750,500)); – Costis Aivalis May 20 '13 at 05:37
  • Don't use [`setPreferredSize()`](http://stackoverflow.com/q/7229226/230513) at all. – trashgod May 20 '13 at 09:12

1 Answers1

0

I suggest you to do the following change.

main.add(rightside, BorderLayout.EAST);

to

main.add(rightside, BorderLayout.CENTER);
Jeff Lee
  • 783
  • 9
  • 17