addGroupglcreateSequentialGroup addComponentenabledLbl...

This preview shows page 166 - 170 out of 251 pages.

.addGroup(gl.createSequentialGroup() .addComponent(enabledLbl) .addComponent(pressedLbl) .addComponent(armedLbl)) ); pack(); setTitle("Custom button model"); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE);
} private class OkButtonModel extends DefaultButtonModel { @Override public void setEnabled(boolean b) { if (b) { enabledLbl.setText("Enabled: true"); } else { enabledLbl.setText("Enabled: false"); } super.setEnabled(b); } @Override public void setArmed(boolean b) { if (b) { armedLbl.setText("Armed: true"); } else { armedLbl.setText("Armed: false"); } super.setArmed(b); } @Override public void setPressed(boolean b) { if (b) { pressedLbl.setText("Pressed: true"); } else { pressedLbl.setText("Pressed: false"); } super.setPressed(b); } } private class CheckBoxAction extends AbstractAction { public CheckBoxAction() { super("Disabled"); } @Override public void actionPerformed(ActionEvent e) { if (okbtn.isEnabled()) { okbtn.setEnabled(false); } else { okbtn.setEnabled(true); } } } public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() { @Override public void run() { ButtonModel2 ex = new ButtonModel2(); ex.setVisible(true); } }); } } This example does the same thing as the previous one. The difference is that we do not use a change listener and we use a custom button model. ButtonModel model = new OkButtonModel(); okbtn.setModel(model); We set the custom model for the button. private class OkButtonModel extends DefaultButtonModel { ... } We create a custom button model and override the necessary methods. @Override public void setEnabled(boolean b) { if (b) { enabledLbl.setText("Enabled: true"); } else { enabledLbl.setText("Enabled: false"); } super.setEnabled(b); } We override the setEnabled() method and add some functionality there. We must not forget to call the parent method as well to proceed with the processing. JList models Several components have two models; JList is one of them. It has the following models: the ListModel and the ListSelectionModel . The ListModel handles data and the ListSelectionModel works with the selection state of the list. The following example uses both models.
package com.zetcode; import java.awt.Container; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.DefaultListModel; import javax.swing.GroupLayout; import static javax.swing.GroupLayout.Alignment.CENTER; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.ListSelectionModel; public class ListModels extends JFrame { private DefaultListModel model; private JList list; private JButton remallbtn; private JButton addbtn; private JButton renbtn; private JButton delbtn; public ListModels() { initUI(); } private void createList() { model = new DefaultListModel(); model.addElement("Amelie"); model.addElement("Aguirre, der Zorn Gottes"); model.addElement("Fargo"); model.addElement("Exorcist"); model.addElement("Schindler's list"); list = new JList(model); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.addMouseListener(new MouseAdapter() { @Override

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture