Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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...

Java
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);
   // secondaryButton.setEnabled(false);
        for (int i = 0; i < 10000; i++) {
            System.out.println("[ " + (i + 1) + " ] Inside Primary Process");
        }
          secondaryButton.setVisible(true);
   // secondaryButton.setEnabled(true);
    } else if (e.getActionCommand().equals("Secondary")) {
        primaryButton.setVisible(false);
   // primaryButton.setEnabled(false);
        for (int i = 0; i < 10000; i++) {
            System.out.println("[ " + (i + 1) + " ] Inside Secondary Process");
        }
        primaryButton.setVisible(true);
   // primaryButton.setEnabled(true);
    }
}
}
Posted
Updated 10-Aug-14 23:12pm
v3

1 solution

I updated the markup.

You are using:

Java
primaryButton.setVisible(true);


to make the buttons working/not working. That is not good practice, cause the user is going to miss the controlls - and the dynamic change in your UI will probably easily mess up everything.

Please use


Java
primaryButton.setEnable(true); // enable
primaryButton.setEnable(false); // disable


that should work better.
YOu might have to redraw your UI after changing the state of the buttons.
 
Share this answer
 
Comments
Uganraj 11-Aug-14 2:55am    
Hi TorstenH,

Thank you for answer,

But, i already tried the SetEnabled() function.
Its wroking after the execution of for loop in above code.
It should disable before(for loop) execution, and enabled after(for loop) execution.

Can you modify the above code accordingly, if possible.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900