I want to add a JComponent
in my basic JFrame
. My problem is that I cannot set background color or border for my component.
JFrame
JPanel drawPanel = new JPanel(new GridBagLayout());
drawPanel.add(new DrawingBoard());
JScrollPane scrollPane = new JScrollPane(drawPanel);
this.add(scrollPane, BorderLayout.CENTER);
this.setVisible(true);
JComponent
private class DrawingBoard extends JComponent {
...
public DrawingBoard() {
this.setPreferredSize(new Dimension(500, 500));
this.setBackground(Color.WHITE); //doesn't work
this.setBorder(BorderFactory.createEmptyBorder(0,10,10,10)); //doesn't work
this.setOpaque(true);
...
I found a simple solution to my problem. I just added the Jcomponent to a Jpanel and then I added the Jpanel to the Jframe.
JPanel outer = new JPanel(new GridBagLayout());
JPanel drawPanel = new JPanel(new GridBagLayout());
drawPanel.add(new DrawingBoard(500,500));
drawPanel.setBackground(Color.WHITE);
outer.setBorder(BorderFactory.createEmptyBorder(0,10,10,10));
JScrollPane scrollPane = new JScrollPane(drawPanel);
outer.add(scrollPane);
this.add(outer, BorderLayout.CENTER);