1

I have a border layout, and a JPanel on the right. The JPanel has absolute layout, and it doesn't contain anything because I'm using it to draw graphics (overriding paintComponent(Graphics g))

The problem is that the panel doesn't show (it's there but like 1 pixel wide). I tried to setSize on the panel.. but that doesn't work..

How do I do it?

frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
JPanel panelCenter = new JPanel();
panelRight = new ActorDrawer();
frame.getContentPane().add(panelRight, BorderLayout.EAST);
panelRight.setSize(200, 400);
panelRight.setLayout(null);
frame.getContentPane().add(panelCenter, BorderLayout.CENTER);
frame.getContentPane().add(panelBottom, BorderLayout.SOUTH);
table = new JTable(new MyTableModel());
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
panelCenter.add(scrollPane);
kleopatra
  • 51,061
  • 28
  • 99
  • 211
luca
  • 12,311
  • 15
  • 70
  • 103

3 Answers3

3

Look into the JPanel.setPreferredSize() method. Also, I think the JFrame.setBounds() call is not what you are looking for. Here's an example:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Dimension;
import java.awt.BorderLayout;

public class Sandbox extends JFrame
{
    class MyPanel extends JPanel
    {
        public void paintComponent(Graphics g)
        {
            g.setColor(Color.RED);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());
        }
    }

    public static void main(String[] args)
    {
        Sandbox s = new Sandbox();
    }

    public Sandbox()
    {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());

        MyPanel panelRight = new MyPanel();
        panelRight.setPreferredSize(new Dimension(150, 300));
        this.getContentPane().add(panelRight, BorderLayout.EAST);

        JPanel panelCenter = new JPanel();
        this.getContentPane().add(panelCenter, BorderLayout.CENTER);

        JScrollPane scrollPane = new JScrollPane(new JTable());
        panelCenter.add(scrollPane);

        JPanel panelBottom = new JPanel();
        this.getContentPane().add(panelBottom, BorderLayout.SOUTH);

        this.setSize(400, 400);
        this.setVisible(true);
    }
}
ctrlc-root
  • 1,049
  • 1
  • 15
  • 22
  • no - never ever call any of the setXXSize methods. Instead, let the component return reasonable sizing hints. http://stackoverflow.com/questions/7229226/avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swing – kleopatra Oct 13 '11 at 06:47
  • @kleopatra when using a JPanel without children and doing direct painting into it, I believe you have to do setXxxSize()... – jfpoilpret Oct 13 '11 at 14:09
  • @jfpoilpret good point, though that would be a very rare exception: most of the time, a drawing has a "natural" size as well. Wouldn't be frequent enough to confuse our young colleagues :-) – kleopatra Oct 13 '11 at 14:35
1

BorderLayout honors the preferred size of the east component, and stretches its height if necessary. Have you tried setting the preferred size of the panel (or overriding the getPreferredSize method to return the size you prefer)?

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

here comes the solution!!! I was doing the same stuff.

JPanel panel = new JPanel();          
JLabel strech_label = new JLabel("As long as you need ,  Dummy Text");
strech_label.setPreferredSize( new Dimension( 111, 0 ) );

panel.setLayout(new BorderLayout());

panel.add(new MyDraw(), BorderLayout.CENTER);
panel.add(strech_label, BorderLayout.SOUTH);

    frame1.setLayout(new BorderLayout());
    frame1.add(panel,BorderLayout.WEST);

So basically the JLabel is either located in the South or North and it is going to strech the MyDraw forcefully.

Tower Jimmy
  • 567
  • 1
  • 4
  • 15