Thanks that was it.
Now I'm getting another error:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal anchor value
at java.awt.GridBagLayout.AdjustForGravity(Unknown Source)
at java.awt.GridBagLayout.adjustForGravity(Unknown Source)
at java.awt.GridBagLayout.ArrangeGrid(Unknown Source)
at java.awt.GridBagLayout.arrangeGrid(Unknown Source)
at java.awt.GridBagLayout.layoutContainer(Unknown Source)
at java.awt.Container.layout(Unknown Source)
at java.awt.Container.doLayout(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validate(Unknown Source)
at java.awt.Window.show(Unknown Source)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at java.awt.Window.setVisible(Unknown Source)
at homenetwork.bkr.training.GridBagLayoutTest$1.run(G ridBagLayoutTest.java:16)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: illegal anchor value
at java.awt.GridBagLayout.AdjustForGravity(Unknown Source)
at java.awt.GridBagLayout.adjustForGravity(Unknown Source)
at java.awt.GridBagLayout.ArrangeGrid(Unknown Source)
at java.awt.GridBagLayout.arrangeGrid(Unknown Source)
at java.awt.GridBagLayout.layoutContainer(Unknown Source)
at java.awt.Container.layout(Unknown Source)
at java.awt.Container.doLayout(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validate(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Updated code:
Project GridBagLayoutTest
|
Code:
|
FontFrame.java
package homenetwork.bkr.training;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class FontFrame extends JFrame {
public FontFrame() {
setTitle("GridBagLayout Test");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
GridBagLayout layout = new GridBagLayout();
setLayout(layout);
ActionListener listener = new FontAction();
//construct components
@SuppressWarnings("unused")
JLabel faceLabel = new JLabel("Face :");
face = new JComboBox(new String[] {"Serif", "SansSerif", "Monospaced", "Dialog", "DialogInput"});
face.addActionListener(listener);
@SuppressWarnings("unused")
JLabel sizeLabel = new JLabel("Size: ");
size = new JComboBox(new String[] {"8","10","12","15", "18", "24", "36", "48"});
size.addActionListener(listener);
bold = new JCheckBox("Bold");
bold.addActionListener(listener);
italic = new JCheckBox("Italic");
italic.addActionListener(listener);
sample = new JTextArea();
sample.setText("The quick brown fox jumps over the lazy dog.");
sample.setEditable(false);
sample.setLineWrap(true);
sample.setBorder(BorderFactory.createEtchedBorder());
//add components to grid, using GBC convenience class
add(faceLabel, new GBC(0,0).setAnchor(GBC.EAST));
add(face, new GBC(1,0).setAnchor(GBC.HORIZONTAL));
add(sizeLabel, new GBC(0,1).setAnchor(GBC.EAST));
add(size, new GBC(1,1).setFill(GBC.HORIZONTAL).setWeight(100,0).setInsets(1,1,100,100)); //assumed (1,1) to (100,100)
add(bold, new GBC(0,2,2,1).setAnchor(GBC.CENTER).setWeight(100,0).setInsets(1,1,100,100));
add(italic, new GBC(0,3,2,1).setAnchor(GBC.CENTER).setWeight(100,100)); //ERROR:java.lang.NullPointerException
add(sample, new GBC(2,0,1,4).setAnchor(GBC.BOTH).setWeight(100,100));
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
private JComboBox face;
private JComboBox size;
private JCheckBox bold;
private JCheckBox italic;
private JTextArea sample;
/**
* An action listener that changes the font of the sample text
*/
private class FontAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String fontFace = (String) face.getSelectedItem();
int fontStyle = (bold.isSelected() ? Font.BOLD : 0) +
(italic.isSelected() ? Font.ITALIC : 0);
int fontSize = Integer.parseInt((String) size.getSelectedItem());
Font font = new Font(fontFace, fontStyle, fontSize);
sample.setFont(font);
sample.repaint();
}
}
}
GridBagLayoutTest.java
package homenetwork.bkr.training;
import java.awt.*;
import javax.swing.*;
public class GridBagLayoutTest {
public static void main (String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
FontFrame frame = new FontFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
} |