Reputation:
How can I disable my btnExecute JButton
when the program is initialized?
After that, when the three JComboBox
selected items are not equal to the "Choose..." item, enable this button.
The second part of this problem is solved, but when starting the program the button is enabled, this should be resolved to only be allowed on the JButton
if the three JComboBox
is not equal to "Choose ..." item.
This is my code:
public class WebStarter_v2 {
private JFrame frame;
private JComboBox<Object> cbProgram;
private JComboBox<Object> cbVersion;
private JComboBox<Object> cbAction;
private JButton btnExecute;
private boolean programSelected = false;
private boolean versionSelected = false;
private boolean actionSelected = false;
private String prototypeValue="XXXXXXXXXX";
private String[] programs = {"Choose...", "Apache", "PHP-FPM", "MySQL"};
private String[] actions = {"Choose...", "start", "restart", "stop"};
//private String[] status = {"standby", "running", "stopped", "error"};
private String[] apacheVersions = {"Choose...", "2.2.31", "2.4.17"};
private String[] phpVersions = {"Choose...", "5.6.16", "7.0.0"};
private String[] mysqlVersions = {"Choose...", "5.7.9"};
public static void main(String[] args) {...}
public WebStarter_v2() {
initialize();
}
private void initialize() {
...
JPanel panel_Actions = new JPanel();
splitPane.setTopComponent(panel_Actions);
...
...
cbProgram = new JComboBox<Object>(new DefaultComboBoxModel<Object>(programs));
cbProgram.setPrototypeDisplayValue(prototypeValue);
cbProgram.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent event) {
if(event.getStateChange() == ItemEvent.SELECTED){
String item = event.getItem().toString();
if ( item.contains("Apache") ){
makeComboBoxModel(cbVersion, apacheVersions);
programSelected = true;
updateButtonState();
}
else if ( item.contains("PHP-FPM") ){
makeComboBoxModel(cbVersion, phpVersions);
programSelected = true;
updateButtonState();
}
else if ( item.contains("MySQL") ){
makeComboBoxModel(cbVersion, mysqlVersions);
programSelected = true;
updateButtonState();
}
else
{
programSelected = false;
//set initial state of cbVersion and cbAction
cbVersion.setModel(new DefaultComboBoxModel<Object>());
cbAction.setModel(new DefaultComboBoxModel<Object>());
updateButtonState();
}
}
}
});
...
...
cbVersion = new JComboBox<Object>();
cbVersion.setPrototypeDisplayValue(prototypeValue);
cbVersion.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent event) {
if(event.getStateChange() == ItemEvent.SELECTED){
String item = event.getItem().toString();
if ( !item.contains("Choose...") )
{
versionSelected = true;
makeComboBoxModel(cbAction, actions);
updateButtonState();
}
else{
versionSelected = false;
cbAction.setModel(new DefaultComboBoxModel<Object>());
updateButtonState();
}
}
}
});
...
...
cbVersion = new JComboBox<Object>();
cbVersion.setPrototypeDisplayValue(prototypeValue);
cbVersion.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent event) {
if(event.getStateChange() == ItemEvent.SELECTED){
String item = event.getItem().toString();
if ( !item.contains("Choose...") )
{
versionSelected = true;
makeComboBoxModel(cbAction, actions);
updateButtonState();
}
else{
versionSelected = false;
cbAction.setModel(new DefaultComboBoxModel<Object>());
updateButtonState();
}
}
}
});
...
...
cbAction = new JComboBox<Object>();
cbAction.setPrototypeDisplayValue(prototypeValue);
cbAction.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent event) {
if(event.getStateChange() == ItemEvent.SELECTED){
String item = event.getItem().toString();
if ( !item.contains("Choose...") ){
actionSelected = true;
updateButtonState();
}
else{
actionSelected = false;
updateButtonState();
}
}
}
});
...
...
btnExecute = new JButton("Execute");
...
...
JPanel panel_Messages = new JPanel();
splitPane.setBottomComponent(panel_Messages);
}
Upvotes: 1
Views: 273
Reputation: 503
Or you can setVisible()
for this:
btnExecute.setVisible(false);
And then in addItemListener
for each JComboBox you should check programSelected
, versionSelected
and ActionSelected
. If all of them are true
, make
btnExecute.setVisible(true);
Upvotes: 1
Reputation: 15310
You want the setEnabled
method of a JButton
:
btnExecute.setEnabled(false);
After that when your user selects the three JComboBox
you can set it back to true
.
Upvotes: 3