Repository navigation
Expand file tree
/
Copy pathButtonModel.java
More file actions
66 lines (44 loc) · 2.5 KB
/
Copy pathButtonModel.java
File metadata and controls
66 lines (44 loc) · 2.5 KB
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
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Main {
public static void main(String args[]) {
JFrame frame = new JFrame("Extracting the button model");
frame.setSize(500, 500);
JButton checkButton = new JButton("label obtained");
checkButton.setBounds(200, 200, 120, 30);
frame.add(checkButton);
ButtonModel model = checkButton.getModel();
// Add a change listener to the ButtonModel to detect changes in its state
model.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
System.out.println("Button state changed!");
System.out.println("Is pressed? " + model.isPressed());
System.out.println("Is armed? " + model.isArmed());
System.out.println("Is enabled? " + model.isEnabled());
System.out.println("Is selected? " + model.isSelected());
}
});
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
/*
Property Name Value
actionCommand The action command string associated with this button
mnemonic The keyboard mnemonic for this button
armed true if the button was pressed and the mouse is still over the
button
enabled true if the button is selectable
pressed true if the button was pressed but the mouse button hasn’t yet
been released
rollover true if the mouse is over the button
selected true if the button has been toggled on (used for checkboxes and
radio buttons)selected
*/
/*
The ButtonModel is an interface that represents the state of a button (like whether it's pressed, armed, selected, etc.). You can retrieve the ButtonModel of a JButton using its getModel() method.
However, in your provided code, you have fetched the model but did not do anything specific with it. I'll expand on your code by retrieving the ButtonModel and then printing out some information about the button's state:
*/