Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

problem with swing

  Asked By: Lewis    Date: Jun 19    Category: Java    Views: 515
  

I have one small problem with swing. I have 2 buttons. One visible and one not
visible. If i click on the visible one, I want to set the second one visible
too. I attached the source code. For sure I made one mistake, but where?


public class TestSetVisible extends javax.swing.JFrame {

/** Creates new form TestSetVisible */
public TestSetVisible() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();

addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jPanel1.add(jButton1);

jButton2.setText("jButton2");
jButton2.setEnabled(false);
jPanel1.add(jButton2);

getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

pack();
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
jButton2.setVisible(true);
}

/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new TestSetVisible().show();
}


// Variables declaration - do not modify
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
// End of variables declaration

}

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Haya Yoshida     Answered On: Jun 19

please replace jButton2.setVisible(true) with jButton2.setEnabled(true) at
line number 44.this is only mistake in your code.

 
Answer #2    Answered By: Geneva Morris     Answered On: Jun 19

1. set  the jButton2 visibility to false in the initComponents method.
2. fix the jButton1ActionPerformed method  as follows:

.....jButton1ActionPerformed(ActionEvent ae) {
boolean isVisible = jButton2.isVisible();
jButton2.setVisible(!isVisible);
}

This will toggle the jButton2 on and off.
The other thing that you can do using the JToggleButton instead
of jButton1.

 
Answer #3    Answered By: Fabia Ferrrari     Answered On: Jun 19

jButton2 is disabled!?
You should also enable it.
jButto2.setEnabled(true)?

 
Didn't find what you were looking for? Find more on problem with swing Or get search suggestion and latest updates.




Tagged: