Gridbag layout.
To: comp.lang.java.gui
I've come up with the following code for my JApplet, which seems to
work. With one exception, and I don't know whether its anything to do
GridBagLayout. What I'm trying to do is create a Panel surrounded by
buttons, like this:
http://www.paullee.com/layout.jpg
The following code nearly does what I want:
private void initialisegridbaglayout( Container pane )
{
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton button;
button = new JButton("");
button.setPreferredSize(new Dimension( 420, 30));
c.gridx = 1;
c.gridy = 0;
pane.add(button, c);
button = new JButton("");
button.setPreferredSize(new Dimension( 30, 356));
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);
displayPanel = new DataDisplayPanel( this );
displayPanel.setPreferredSize(new Dimension( 420, 356 ));
displayPanel.setBackground( Color.BLACK);
c.gridx = 1;
c.gridy = 1;
pane.add(display, c);
button = new JButton("");
button.setPreferredSize(new Dimension( 30, 356));
c.gridx = 2;
c.gridy = 1;
pane.add(button, c);
button = new JButton("");
button.setPreferredSize(new Dimension( 420, 30));
c.gridx = 1; //aligned with button 2
c.gridy = 2; //third row
pane.add(button, c);
}
The problem lies in the central square, which is a display panel,
subclassed from JPanel. At the moment, it doesn't do anything, it just
has a load
of MouseListeners to watch out for mouse clicks, and the following:
public void paintComponent( Graphics g )
{
super.paintComponent( g );
}
Now, using the above GridBad code, the buttons are drawn at the
perimeter of the Panel, but the panel itself isn't black. I've also
tried
drawing simply shapes by adding code to its paintComponent method
above, but to no avail. Even if I replace
displayPanel = new DataDisplayPanel( this );
by
JPanel displayPanel = new JPanel();
I still get nothing.
But this stand-alone application:
import java.awt.*;
import javax.swing.*;
public class DemoGridBag extends JFrame
{
public static void main(String[] args)
{
JFrame f = new JFrame("Gridbag Demo");
Container pane = f.getContentPane();
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JButton button;
button = new JButton("1");
button.setPreferredSize(new Dimension( 420, 30));
//c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 0;
pane.add(button, c);
button = new JButton("2");
button.setPreferredSize(new Dimension( 30, 356));
//c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);
JPanel display = new JPanel();
display.setPreferredSize(new Dimension( 420, 356 ));
display.setBackground( Color.BLACK );
//c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 1;
c.gridy = 1;
pane.add(display, c);
button = new JButton("4");
button.setPreferredSize(new Dimension( 30, 356));
//c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 2;
c.gridy = 1;
pane.add(button, c);
button = new JButton("5");
button.setPreferredSize(new Dimension( 420, 30));
//c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0; //reset to default
c.gridx = 1; //aligned with button 2
c.gridwidth = 1; //1 columns wide
c.gridy = 2; //third row
pane.add(button, c);
f.setSize(400, 400);
f.pack();
f.setVisible(true);
}
}
does show a black central JPanel to which I can draw. What is wrong
with my Applet method?
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24