I'm trying to learn some basic AWT usage to do a very simple UI with a checkbox group. I've been using the book Java The Complete Reference - Tenth edition" The example I'm using is straight out of the book. The frame displays and so does the text string but the checkbox group is not being displayed (I'm using eclipse on windows 7 64 bit. Java version is 12.0.1)
I've tried it in eclipse and from the command line with the same results.
Here is the source code for the example:
// Demonstrate AWT Checkbox Group
import java.awt.*;
import java.awt.event.*;
public class CBGroup extends Frame implements ItemListener {
String msg = "";
Checkbox windows, android, solaris, mac;
CheckboxGroup cbg;
public CBGroup() {
// Use a flow layout
setLayout (new FlowLayout());
// Create a checkbox group
cbg = new CheckboxGroup();
// Create the checkboxes and include them in the group
windows = new Checkbox("windows", cbg, true);
android = new Checkbox("android", cbg, false);
solaris = new Checkbox("solaris", cbg, false);
mac = new Checkbox("mac", cbg, false);
// Add item listeners
windows.addItemListener(this);
android.addItemListener(this);
solaris.addItemListener(this);
mac.addItemListener(this);
addWindowListener(new WindowAdapter () {
public void windowClosing (WindowEvent we) {
System.exit(0);
}
});
}
public void itemStateChanged (ItemEvent ie) {
repaint();
}
// Display current state of the check boxes
public void paint (Graphics g) {
msg = "Current selection: ";
msg += cbg.getSelectedCheckbox().getLabel();
g.drawString(msg, 20, 120);
}
public static void main(String[] args) {
CBGroup appwin = new CBGroup();
appwin.setSize(new Dimension (240, 180));
appwin.setTitle("CBGroup");
appwin.setVisible(true);
}
}
I expect a window frame to be displayed with a checkbox group showing windows, solaris, mac and android has choices and with windows already selected as the default. Underneath that should be a text string that says "Current selection: windows". The text string shows up and the window frame looks good and works properly but the checkbox group does not show up. Again, this code is straight from the book I mentioned. I'm guessing it maybe has something to do with the flow layout part but not much control over that.
(Before you continue with your project, take a look at what is the difference between Swing and AWT. I recommend you to move to Swing.)
You are not able to see the checkboxes because you do not add them to the frame. Use Frame.add(Component c)
method in order to achieve that.
Now about the custom painting, I would not prefer it here since it is only a text. You could add a Label or something instead of going with custom painting. Also, when you override paint
method, always start by calling super.paint(Graphics g)
(the same "rule" applies to Swing - paintComponent
method).
Finally, all AWT (and Swing) applications must run on their own thread. Use EventQueue#invokeLater
method for AWT and SwingUtilities#invokeLater
for Swing. (Do they actually differ?)
Your code with all the implementations i mentioned:
public class CBGroup extends Frame implements ItemListener {
String msg = "";
Checkbox windows, android, solaris, mac;
CheckboxGroup cbg;
public CBGroup() {
super("");
// Use a flow layout
setLayout(new FlowLayout());
// Create a checkbox group
cbg = new CheckboxGroup();
// Create the checkboxes and include them in the group
windows = new Checkbox("windows", cbg, true);
android = new Checkbox("android", cbg, false);
solaris = new Checkbox("solaris", cbg, false);
mac = new Checkbox("mac", cbg, false);
add(windows);
add(android);
add(solaris);
add(mac);
// Add item listeners
windows.addItemListener(this);
android.addItemListener(this);
solaris.addItemListener(this);
mac.addItemListener(this);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
@Override
public void itemStateChanged(ItemEvent ie) {
repaint();
}
// // Display current state of the check boxes
@Override
public void paint(Graphics g) {
super.paint(g);
msg = "Current selection: ";
msg += cbg.getSelectedCheckbox().getLabel();
g.drawString(msg, 20, 120);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
CBGroup appwin = new CBGroup();
appwin.setSize(new Dimension(240, 180));
appwin.setTitle("CBGroup");
appwin.setVisible(true);
});
}
}