JComponents
JComponents are the Swing elements the user interacts with and are central to building a ConfigurablePanel. Here are examples of useful components:
The following section is composed of snippets of codes covering often used cases in Swing. In particular, these snippets are useful when implementing the ConfigurablePanel methods propertyhasChanged() and parameterhasChanged() in which JComponents should be modified.
Note that the action listeners are already implemented as static methods in SwingUIListeners and should be called in the ConfigurablePanel method addComponentListeners().
The list is not exhaustive.
Table of contents
- JTextField
- JToggleButton/JCheckBox
- JButton
- JComboBox
- Jslider
- JSpinner
- ButtonGroup
- JLabel
- Titledborder
JTextField
Set font
JTextField txtf = new JTextField();
txtf.setFont(new Font("Tahoma", Font.BOLD, 12));
Set text color
JTextField txtf = new JTextField();
txtf.setForeground(Color.red);
Action listeners
To have a trigger when the “Enter” key is pressed:
JTextField txtf = new JTextField();
txtf.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String text = txtf.getText();
// Do something
doSomething(text);
}
});
Or trigger when the focus is lost:
JTextField txtf = new JTextField();
txtf.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent ex) {}
@Override
public void focusLost(FocusEvent ex) {
String s = txtf.getText();
// Do something
doSomething(text);
}
});
Note that those actions listeners are not incompatible.
Modifying a JTextField
JTextField txtf = new JTextField("0");
String s = "1";
txtf.setText(s);
JToggleButton/JCheckBox
Set font
JToggleButton tglb = new JToggleButton("On/Off");
tglb.setFont(new Font("Tahoma", Font.BOLD, 12));
Set margin
JToggleButton tglb = new JToggleButton("On/Off");
tglb.setMargin(new Insets(2, 2, 2, 2));
Set text color
JToggleButton tglb = new JToggleButton("On/Off");
tglb.setForeground(Color.red);
Action listener
JToggleButton tglb = new JToggleButton("On/Off");
tglb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
boolean selected = abstractButton.getModel().isSelected();
// Do something
doSomething(selected);
}
});
Modifying a JToggleButton
JToggleButton tglb = new JToggleButton("On/off");
boolean b = false;
tglb.setSelected(b);
Enable/disable
JToggleButton tglb = new JToggleButton("On/off");
boolean b = false;
tglb.setEnable(b);
JButton
Action listener
JButton btn = new JButton("Click me");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doSomething();
}
});
Enable/disable
JButton tglb = new JButton("Click me");
boolean b = false;
tglb.setEnable(b);
JComboBox
Action listener
String[] strarr = {"Value1","Value2","Value3"};
JComboBox<String> cbx = new JComboBox();
cbx.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String val = String.valueOf(cbx.getSelectedItem());
int index = cbx.getSelectedIndex();
// Do something
doSomething(val);
}
});
Modifying a JComboBox
String[] strarr = {"Value1","Value2","Value3"};
JComboBox<String> cbx = new JComboBox();
cbx.setSelectedIndex(1);
cbx.setSelectedItem(strarr[2]);
Note that this will trigger the action listener of the previous section.
JSlider
Action listener
JSlider sld = new JSlider();
sld.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
int val = sld.getValue();
// Do something
doSomething();
}
});
Modifying a JSlider
JSlider sld = new JSlider();
sld.setValue(10);
JSpinner
Action listener
// integer step size
SpinnerNumberModel model = new SpinnerNumberModel(0,-5,5,1);
JSpinner spnr = new JSpinner(model);
spnr.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
int val = (int) spnr.getValue();
// Do something
doSomething();
}
});
Modifying a JSpinner
SpinnerNumberModel model = new SpinnerNumberModel(0,-5,5,1);
JSpinner spnr = new JSpinner(model);
spnr.setValue(2);
ButtonGroup
Action listener
ButtonGroup bg = new ButtonGroup();
// Instantiates the buttons and adds them to the button group
int N = 5;
for(int i=0;i<N;i++){
JToggleButton btn = new JToggleButton("Button"+i);
bg.add(btn);
}
// Adds the action listeners
Enumeration<AbstractButton> enm = bg.getElements();
int counter = 0;
while(enm.hasMoreElements()) {
final int pos = counter;
AbstractButton btn = enm.nextElement();
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
// Do something
doSomething(pos);
}
});
counter++;
}
JLabel
Modifying a JLabel
JLabel lbl = new JLabel("Label");
lbl.setText("New text");
Set font and font size
JLabel lbl = new JLabel("Label");
lbl.setFont(new Font("Tahoma", Font.BOLD, 12));
TitledBorder
Modifying a TitledBorder title and color
// Instiates the JPanel and its titledborder
JPanel pane = new JPanel();
Font f = new Font("Tahoma", Font.BOLD, 12);
Color c = Color.blue;
pane.setBorder(new TitledBorder(null, "Title", TitledBorder.LEFT, TitledBorder.TOP, f, c));
// Modifying title
TitledBorder border = (TitledBorder) pane.getBorder();
String title = "New title";
border.setTitle(title);
// Modifying color
Color c2 = Color.red;
border.setTitleColor(c2);
pane.repaint();