Solved

Enableing Text Fileds

Posted on 2001-06-10
12
309 Views
Last Modified: 2010-05-18
Hello,
I have a check box and two text fields. I want the text fields to be grayed out until the check box is checked. When the box is unchecked I want the text fields to again be grayed out (as well as clear themselves from any data in them). How can this be done?

Notes:
To make this easier to integrate here are some details about the prorgam:
serverTextField - name of textfield 1
portTextField - name of textfield 2
altserverCheckBox - name of checkbox

will be compiled and run on JDK/JRE - 1.3 (so pretty much anything goes) (doubt that you would know any, but no new 1.4 stuff)
0
Comment
Question by:m_morgan
  • 6
  • 5
12 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
John,

You need to add a listener to your checkbox that can perform the required action when the checkbox is (un)checked. I think you can just use an ActionListener and in the actionPerformed do something like:

  textfield.setEnabled(checkbox.isSelected());

0
 

Author Comment

by:m_morgan
Comment Utility
that compiles fine. But the boxes aren't grayed out and checking or unchecking the check box does nothing. Here's the code: (It's all in the same class)

  JCheckBox altserverCheckBox = new JCheckBox("Use an Alternate Server and/or port");
  JTextField serverTextField = new JTextField();
  JTextField portTextField = new JTextField();

    altserverCheckBox.setBounds(new Rectangle(21, 390, 210, 23));
    altserverCheckBox.setToolTipText("Alternate Server / Port: Click here to use a specific server or port.");
    altserverCheckBox.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(ActionEvent e)
      {
        altsvrBox_actionPerformed(e);
      }
    });

    serverTextField.setBounds(new Rectangle(21, 430, 275, 23));
    serverTextField.setEditable(false);
    portTextField.setBounds(new Rectangle(310, 430, 63, 23));
    portTextField.setEditable(false);

  void altsvrBox_actionPerformed(ActionEvent e)
  {
    serverTextField.setEnabled(altserverCheckBox.isSelected());
  }
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Strange??
Is the action listener getting called? Try adding a System.out.println() statement in your action listener to check.

You'll need to handle the initial state explicitly. ie. if your checkbox is initially unset, then you should disable the text field when you create it.
0
 

Author Comment

by:m_morgan
Comment Utility
does the code above disable it? I though that:
serverTextField.setEditable(false);
greys out text fields.
0
 

Author Comment

by:m_morgan
Comment Utility
I added a println like you said and when I checked and unchecked the check box it printed the text so it is getting called.
0
 
LVL 92

Accepted Solution

by:
objects earned 75 total points
Comment Utility
> serverTextField.setEditable(false);
> greys out text fields.

Nope, need to use setEnabled(false);

> it is getting called

Next step, print the value of
altserverCheckBox.isSelected()

I'm knocking off for the day, will check how you've gone in the morning.

Another avenue to try is adding an ItemListener or ChangeListener to the checkbox instead of ActionListener.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:m_morgan
Comment Utility
could you tell me more about ChangeListeners and ActionListeners?

BTW - setEnabled(false) didn't work either.
0
 

Author Comment

by:m_morgan
Comment Utility
I'd still like to know more about ChangeListners and ItemListners, but I got it working with an ActionListner. The only problem is that when the textfield is not enabled it does not gray out (it is uneditable but is still white, confusing the user and giving the appearance of an error) When doing this sort of thing in C++ it would automatically gray out for me, do I need to use a seperate statement to change the background color of the text filed to gray to give this illusion?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Have a look at Sun's Java tutorial it explains the various listener types better than I can.
I think the appearance of the disabled text field depends on the L&F. WHich L&F?
0
 

Author Comment

by:m_morgan
Comment Utility
the L&F is the default system L&F. In this case, Windows. I think it would grey out. Any suggestions?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
If the text field has a label associated with it, you could disable the Label. I know they grey out :)
0
 

Expert Comment

by:sontnvn
Comment Utility
Dear,
 Here my entire example code, it was make by VJA but pure java.
package com.gcs.ui;

import java.applet.*;
import java.awt.*;
/**
 * Description:
 * Creation date: (6/18/01 2:16:18 PM)
 * @author: Son Tran Ngoc
 */
public class SecondExample extends javax.swing.JApplet {
      private javax.swing.JPanel ivjJAppletContentPane = null;
      private javax.swing.JRadioButton ivjJRadioButton1 = null;
      private javax.swing.JTextField ivjJTextField1 = null;
      private javax.swing.JTextField ivjJTextField11 = null;
      IvjEventHandler ivjEventHandler = new IvjEventHandler();

class IvjEventHandler implements java.awt.event.ActionListener {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                  if (e.getSource() == SecondExample.this.getJRadioButton1())
                        connEtoC1(e);
            };
      };
/**
 * connEtoC1:  (JRadioButton1.action.actionPerformed(java.awt.event.ActionEvent) --> SecondExample.jRadioButton1_ActionPerformed()V)
 * @param arg1 java.awt.event.ActionEvent
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private void connEtoC1(java.awt.event.ActionEvent arg1) {
      try {
            // user code begin {1}
            // user code end
            this.jRadioButton1_ActionPerformed();
            // user code begin {2}
            // user code end
      } catch (java.lang.Throwable ivjExc) {
            // user code begin {3}
            // user code end
            handleException(ivjExc);
      }
}
/**
 * Returns information about this applet.
 * @return a string of information about this applet
 */
public String getAppletInfo() {
      return "SecondExample\n" +
            "\n" +
            "Description:\n" +
            "Creation date: (6/18/01 2:16:16 PM)\n" +
            "@author: Son Tran Ngoc\n" +
            "";
}
/**
 * Return the JAppletContentPane property value.
 * @return javax.swing.JPanel
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JPanel getJAppletContentPane() {
      if (ivjJAppletContentPane == null) {
            try {
                  ivjJAppletContentPane = new javax.swing.JPanel();
                  ivjJAppletContentPane.setName("JAppletContentPane");
                  ivjJAppletContentPane.setLayout(null);
                  getJAppletContentPane().add(getJRadioButton1(), getJRadioButton1().getName());
                  getJAppletContentPane().add(getJTextField1(), getJTextField1().getName());
                  getJAppletContentPane().add(getJTextField11(), getJTextField11().getName());
                  // user code begin {1}
                  // user code end
            } catch (java.lang.Throwable ivjExc) {
                  // user code begin {2}
                  // user code end
                  handleException(ivjExc);
            }
      }
      return ivjJAppletContentPane;
}
/**
 * Return the JRadioButton1 property value.
 * @return javax.swing.JRadioButton
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JRadioButton getJRadioButton1() {
      if (ivjJRadioButton1 == null) {
            try {
                  ivjJRadioButton1 = new javax.swing.JRadioButton();
                  ivjJRadioButton1.setName("JRadioButton1");
                  ivjJRadioButton1.setText("Enable select");
                  ivjJRadioButton1.setBounds(56, 84, 108, 22);
                  // user code begin {1}
                  // user code end
            } catch (java.lang.Throwable ivjExc) {
                  // user code begin {2}
                  // user code end
                  handleException(ivjExc);
            }
      }
      return ivjJRadioButton1;
}
/**
 * Return the JTextField1 property value.
 * @return javax.swing.JTextField
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JTextField getJTextField1() {
      if (ivjJTextField1 == null) {
            try {
                  ivjJTextField1 = new javax.swing.JTextField();
                  ivjJTextField1.setName("JTextField1");
                  ivjJTextField1.setBounds(201, 55, 97, 20);
                  // user code begin {1}
                  // user code end
            } catch (java.lang.Throwable ivjExc) {
                  // user code begin {2}
                  // user code end
                  handleException(ivjExc);
            }
      }
      return ivjJTextField1;
}
/**
 * Return the JTextField11 property value.
 * @return javax.swing.JTextField
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private javax.swing.JTextField getJTextField11() {
      if (ivjJTextField11 == null) {
            try {
                  ivjJTextField11 = new javax.swing.JTextField();
                  ivjJTextField11.setName("JTextField11");
                  ivjJTextField11.setBounds(203, 117, 97, 20);
                  // user code begin {1}
                  // user code end
            } catch (java.lang.Throwable ivjExc) {
                  // user code begin {2}
                  // user code end
                  handleException(ivjExc);
            }
      }
      return ivjJTextField11;
}
/**
 * Called whenever the part throws an exception.
 * @param exception java.lang.Throwable
 */
private void handleException(java.lang.Throwable exception) {

      /* Uncomment the following lines to print uncaught exceptions to stdout */
      // System.out.println("--------- UNCAUGHT EXCEPTION ---------");
      // exception.printStackTrace(System.out);
}
/**
 * Initializes the applet.
 *
 * @see #start
 * @see #stop
 * @see #destroy
 */
public void init() {
      try {
            setName("SecondExample");
            setSize(426, 240);
            setContentPane(getJAppletContentPane());
            initConnections();
            // user code begin {1}
            // user code end
      } catch (java.lang.Throwable ivjExc) {
            // user code begin {2}
            // user code end
            handleException(ivjExc);
      }
}
/**
 * Initializes connections
 * @exception java.lang.Exception The exception description.
 */
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private void initConnections() throws java.lang.Exception {
      // user code begin {1}
      // user code end
      getJRadioButton1().addActionListener(ivjEventHandler);
}
/**
 * Comment
 */
public void jRadioButton1_ActionPerformed() {
      if (getJRadioButton1().isSelected()){
            getJTextField1().setEnabled(true);
            getJTextField11().setEnabled(true);
            getJTextField1().setBackground(Color.white);
            getJTextField11().setBackground(Color.white);
            System.out.println("No Selected");
            
      }else{
            
            getJTextField1().setText(null);
            getJTextField11().setText(null);
            getJTextField1().setBackground(Color.gray);
            getJTextField11().setBackground(Color.gray);
            getJTextField1().setEnabled(false);
            getJTextField11().setEnabled(false);
            System.out.println("Selected");
      }
      return;
}
/**
 * main entrypoint - starts the part when it is run as an application
 * @param args java.lang.String[]
 */
public static void main(java.lang.String[] args) {
      try {
            javax.swing.JFrame frame = new javax.swing.JFrame();
            SecondExample aSecondExample;
            Class iiCls = Class.forName("com.gcs.ui.SecondExample");
            ClassLoader iiClsLoader = iiCls.getClassLoader();
            aSecondExample = (SecondExample)java.beans.Beans.instantiate(iiClsLoader,"com.gcs.ui.SecondExample");
            frame.getContentPane().add("Center", aSecondExample);
            frame.setSize(aSecondExample.getSize());
            frame.addWindowListener(new java.awt.event.WindowAdapter() {
                  public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                  };
            });
            frame.show();
            java.awt.Insets insets = frame.getInsets();
            frame.setSize(frame.getWidth() + insets.left + insets.right, frame.getHeight() + insets.top + insets.bottom);
            frame.setVisible(true);
      } catch (Throwable exception) {
            System.err.println("Exception occurred in main() of javax.swing.JApplet");
            exception.printStackTrace(System.out);
      }
}
/**
 * Paints the applet.
 * If the applet does not need to be painted (e.g. if it is only a container for other
 * awt components) then this method can be safely removed.
 *
 * @param g  the specified Graphics window
 * @see #update
 */
public void paint(Graphics g) {
      super.paint(g);

      // insert code to paint the applet here
}
}
good luck
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count8 challlenge 13 84
Passing list of object to Oracle Database Procedure 3 40
recursion example 16 66
eclipse formatting 6 38
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now