i have small problem while disabling/enabling buttons in Swing Application. It's very basic problem, but I'm new to this concept.
I have a requirement like, having 2 buttons to perform 2 individual operations.
1)While Running initially B1 & B2 Should be Visible/Enabled.
2)If B1 Clicked, then B2 should be disabled/Invisible. After B1 work Completed, then Both B1 & B2 Should be Visible/Enabled.
3)If B2 Clicked, then B1 should be disabled/Invisible. After B2 work Completed, then Both B1 & B2 Should be Visible/Enabled.
But, my problem, is, after processing B1/B2 SetEnable/SetVisible is working.
It should be disabled before start and enabled after completing task
Can anyone help in this case?
Below is my Code...
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class FinCSV implements ActionListener {
JFrame frame = new JFrame("ICON");
JPanel panel = new JPanel(new GridLayout(2, 1, 4, 4));
JButton primaryButton = new JButton(" Primary Process");
JButton secondaryButton = new JButton(" Secondary Process");
public static void main(String[] args)
throws Exception {
FinCSV msql = new FinCSV();
msql.initGUI();
}
public void initGUI() {
this.primaryButton.setActionCommand("Primary");
this.secondaryButton.setActionCommand("Secondary");
this.primaryButton.addActionListener(this);
this.secondaryButton.addActionListener(this);
this.primaryButton.setFont(new Font("Serif", Font.BOLD, 22));
this.secondaryButton.setFont(new Font("Serif", Font.BOLD, 22));
panel.add(this.primaryButton);
panel.add(this.secondaryButton);
frame.setContentPane(panel);
frame.setSize(650, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Primary")) {
secondaryButton.setVisible(false);
for (int i = 0; i < 10000; i++) {
System.out.println("[ " + (i + 1) + " ] Inside Primary Process");
}
secondaryButton.setVisible(true);
} else if (e.getActionCommand().equals("Secondary")) {
primaryButton.setVisible(false);
for (int i = 0; i < 10000; i++) {
System.out.println("[ " + (i + 1) + " ] Inside Secondary Process");
}
primaryButton.setVisible(true);
}
}
}