Question : Some Java problems with JToolBar, button sizes, JPane, constants etc.

I add here the code for Colorizer.java. It is supposed to be a practice on setting up a GUI. There is no intenton on making code for a real paint program. In doing this self-made practice, I got stuck on the following points:

(-) I did not manage to get all the buttons to be the same size. Tried 50x50, but I am not sure if that is the correct size. It didn't work anyway.
(-) The buttonrow is placed wrongly in the centre. It is supposed to be placed straight under the menuline un the top left corner.
(-) The drawingpanel in the mainframe has wrong size. It should fill out the rest of the window.
(-) Using the constant VERTICAL does not seem to work. I thought I had imported the class libraries correctly.
(-) When I drag off the JToolBar from the mainframe, and then close it, I get the following error:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: canno
t add to layout: constraints must be a GridBagConstraint
        at java.awt.GridBagLayout.addLayoutComponent(Unknown Source)
        at java.awt.Container.addImpl(Unknown Source)
        at java.awt.Container.add(Unknown Source)
        at javax.swing.plaf.basic.BasicToolBarUI$FrameListener.windowClosing(Unk
nown Source)
        at java.awt.Window.processWindowEvent(Unknown Source)
        at javax.swing.JDialog.processWindowEvent(Unknown Source)
        at java.awt.Window.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(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.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

After the JToolBar is closed, a new one pops up where it is supposed to be placed from the start(see description above).

Hope you gues can help me.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
import javax.swing.*; //JFrame, JButton, JPanel, JTextField
import javax.swing.SwingConstants; //Gets the constants, like VERTICAL and HORIZONTAL
import java.awt.*; //Color, GridBagLayout, GridBagConstraints, BorderLayout
import java.awt.event.*; //ActionListener and the rest
import java.awt.Dimension;

class Mainframe extends JFrame {
	EditColorsDialog editColorsDiag = new EditColorsDialog(this);
	
	public Mainframe(String title) {
		setTitle(title);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		setLayout(new GridBagLayout());
		
		buildMenuLine();
		buildButtonLine();
		
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 1;
		c.gridy = 1;
		c.gridheight = 6;
		
		DrawingPanel dPanel = new DrawingPanel();
		dPanel.setBorder(BorderFactory.createLineBorder(Color.black));
		add(dPanel, c);
	}
	private void buildMenuLine() {
		JMenu mainMenu = new JMenu("File");
		JMenuItem menuItem = new JMenuItem("Save");
		MenuListener listener = new MenuListener();
		mainMenu.add(menuItem);
		menuItem.addActionListener(listener);
		
		menuItem = new JMenuItem("Load");
		mainMenu.add(menuItem);
		menuItem.addActionListener(listener);
		
		menuItem = new JMenuItem("Close");
		mainMenu.add(menuItem);
		menuItem.addActionListener(listener);
		
		menuItem = new JMenuItem("Exit");
		mainMenu.add(menuItem);
		menuItem.addActionListener(listener);
		
		JMenu optionsMenu = new JMenu("Options");
		menuItem = new JMenuItem("Edit Colors");
		optionsMenu.add(menuItem);
		menuItem.addActionListener(listener);
		
		menuItem = new JMenuItem("About");
		optionsMenu.add(menuItem);
		menuItem.addActionListener(listener);
		
		JMenuBar menuLine = new JMenuBar();
		menuLine.add(mainMenu);
		menuLine.add(optionsMenu);
		
		setJMenuBar(menuLine);
	}
	private void buildButtonLine() {
		JToolBar buttonRow = new JToolBar("Colors", 1); //VERTICAL doesn't work, using 1
		ButtonListener listener = new ButtonListener();
		int buttonx = 50;
		int buttony = 50;
		Dimension d = new Dimension(buttonx, buttony);
		
		JButton button = new JButton("Yellow");
		button.setPreferredSize(d);
		buttonRow.add(button);
		button.addActionListener(listener);
		
		button = new JButton("Red");
		button.setPreferredSize(d);
		buttonRow.add(button);
		button.addActionListener(listener);
		
		button = new JButton("Blue");
		button.setPreferredSize(d);
		buttonRow.add(button);
		button.addActionListener(listener);
		
		button = new JButton("Orange");
		button.setPreferredSize(d);
		buttonRow.add(button);
		button.addActionListener(listener);
		
		button = new JButton("Green");
		button.setPreferredSize(d);
		buttonRow.add(button);
		button.addActionListener(listener);
		
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.VERTICAL;
		c.gridx = 0;
		c.gridy = 1;
		c.gridheight = 6;

		add(buttonRow, c);
	}
	private class MenuListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			String itemName = event.getActionCommand();
			System.out.println("You pushed the menu item named \"" + itemName +"\"..");
			if (itemName == "Exit") {
				System.out.println("Exiting..");
				dispose();
				System.exit(0);
			} else if (itemName == "Edit Colors") {
				editColorsDiag.showDialog();
			}
		}
	}
	private class ButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			String buttonName = event.getActionCommand();
			System.out.println("You pushed the button named \"" + buttonName +"\"..");
		}
	}
	private class DrawingPanel extends JPanel {
		public DrawingPanel() {
			add(new JTextField("Drawing Window"));
			add(new JTextField("- Testing"));
		}
	}
}
class EditColorsDialog extends JDialog {
	JButton exitButton = new JButton("Close");
	ButtonListener listener = new ButtonListener();
	
	public EditColorsDialog(JFrame parent) {
		super(parent, "Edit Colors", true);
		add(new JLabel("Pane for editing colors"), BorderLayout.NORTH);
		add(new JLabel(),BorderLayout.CENTER);
		add(exitButton,BorderLayout.SOUTH);
		exitButton.addActionListener(listener);
		setSize(200,300);
		setResizable(false);
	}
	public void showDialog() {
		setVisible(true);
	}
	private class ButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			String buttonName = event.getActionCommand();
			if ( buttonName == "Close" ) {
				setVisible(false);
			}
		}
	}
}

class Colorizer {
	public static void main(String[] args) {
		Mainframe newFrame = new Mainframe("Colorizer");
		newFrame.setVisible(true);
		newFrame.setSize(300,400);
		newFrame.setResizable(false);
	}
}

Answer : Some Java problems with JToolBar, button sizes, JPane, constants etc.

Sorry, here's the code :)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToolBar;

class Mainframe extends JFrame {
	private static final long serialVersionUID = 1581133201499203821L;

	GridBagConstraints moduleGBConstraints = new GridBagConstraints();

	EditColorsDialog editColorsDiag = new EditColorsDialog(this);

	public Mainframe(String title) {
		setTitle(title);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		setLayout(new GridBagLayout());

		buildMenuLine();
		buildButtonLine();

		DrawingPanel dPanel = new DrawingPanel();
		dPanel.setBorder(BorderFactory.createLineBorder(Color.black));
		moduleGBConstraints.gridx++;
		moduleGBConstraints.gridheight = 1;
		moduleGBConstraints.weightx = 0.7;
		moduleGBConstraints.weighty = 0;
		moduleGBConstraints.gridheight = 6;
		add(dPanel, moduleGBConstraints);
	}

	private void buildMenuLine() {
		JMenu mainMenu = new JMenu("File");
		JMenuItem menuItem = new JMenuItem("Save");
		MenuListener listener = new MenuListener();
		mainMenu.add(menuItem);
		menuItem.addActionListener(listener);

		menuItem = new JMenuItem("Load");
		mainMenu.add(menuItem);
		menuItem.addActionListener(listener);

		menuItem = new JMenuItem("Close");
		mainMenu.add(menuItem);
		menuItem.addActionListener(listener);

		menuItem = new JMenuItem("Exit");
		mainMenu.add(menuItem);
		menuItem.addActionListener(listener);

		JMenu optionsMenu = new JMenu("Options");
		menuItem = new JMenuItem("Edit Colors");
		optionsMenu.add(menuItem);
		menuItem.addActionListener(listener);

		menuItem = new JMenuItem("About");
		optionsMenu.add(menuItem);
		menuItem.addActionListener(listener);

		JMenuBar menuLine = new JMenuBar();
		menuLine.add(mainMenu);
		menuLine.add(optionsMenu);

		setJMenuBar(menuLine);
	}

	private void buildButtonLine() {
		JToolBar buttonRow = new JToolBar("Colors", 1); // VERTICAL doesn't
		// work, using 1
		buttonRow.setLayout(new GridLayout(5, 1, 4, 4));
		ButtonListener listener = new ButtonListener();
		int buttonx = 50;
		int buttony = 30;
		Dimension d = new Dimension(buttonx, buttony);

		buttonRow.setPreferredSize(null);

		JButton button = new JButton("Yellow");
		button.setPreferredSize(d);
		buttonRow.add(button);
		button.addActionListener(listener);

		button = new JButton("Red");

		button.setPreferredSize(d);
		buttonRow.add(button);
		button.addActionListener(listener);

		button = new JButton("Blue");
		button.setPreferredSize(d);
		buttonRow.add(button);
		button.addActionListener(listener);

		button = new JButton("Orange");
		button.setPreferredSize(d);
		buttonRow.add(button);
		button.addActionListener(listener);

		button = new JButton("Green");
		button.setPreferredSize(d);
		buttonRow.add(button);
		button.addActionListener(listener);

		moduleGBConstraints.fill = GridBagConstraints.VERTICAL;
		moduleGBConstraints.anchor = GridBagConstraints.FIRST_LINE_START;
		moduleGBConstraints.gridx = 0;
		moduleGBConstraints.gridy = 1;
		moduleGBConstraints.gridheight = 1;
		moduleGBConstraints.weightx = 0.3;
		moduleGBConstraints.weighty = 0;
		moduleGBConstraints.gridheight = 6;

		add(buttonRow, moduleGBConstraints);
	}

	private class MenuListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			String itemName = event.getActionCommand();
			System.out.println("You pushed the menu item named \"" + itemName
					+ "\"..");
			if (itemName == "Exit") {
				System.out.println("Exiting..");
				dispose();
				System.exit(0);
			} else if (itemName == "Edit Colors") {
				editColorsDiag.showDialog();
			}
		}
	}

	private class ButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			String buttonName = event.getActionCommand();
			System.out.println("You pushed the button named \"" + buttonName
					+ "\"..");
		}
	}

	@SuppressWarnings("serial")
	private class DrawingPanel extends JPanel {
		public DrawingPanel() {
			add(new JTextField("Drawing Window"));
			add(new JTextField("- Testing"));
		}
	}
}

@SuppressWarnings("serial")
class EditColorsDialog extends JDialog {
	JButton exitButton = new JButton("Close");
	ButtonListener listener = new ButtonListener();

	public EditColorsDialog(JFrame parent) {
		super(parent, "Edit Colors", true);
		add(new JLabel("Pane for editing colors"), BorderLayout.NORTH);
		add(new JLabel(), BorderLayout.CENTER);
		add(exitButton, BorderLayout.SOUTH);
		exitButton.addActionListener(listener);
		setSize(200, 300);
		setResizable(false);
	}

	public void showDialog() {
		setVisible(true);
	}

	private class ButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			String buttonName = event.getActionCommand();
			if (buttonName == "Close") {
				setVisible(false);
			}
		}
	}
}

public class Colorizer {
	public static void main(String[] args) {
		Mainframe newFrame = new Mainframe("Colorizer");
		newFrame.setVisible(true);
		newFrame.setSize(300, 400);
		newFrame.setResizable(false);
	}
}
Random Solutions  
 
programming4us programming4us