Solved

Enableing Text Fileds

Posted on 2001-06-10
12
315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6172822
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
ID: 6172860
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
ID: 6172866
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

Author Comment

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

Author Comment

by:m_morgan
ID: 6172881
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
ID: 6172895
> 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
 

Author Comment

by:m_morgan
ID: 6172929
could you tell me more about ChangeListeners and ActionListeners?

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

Author Comment

by:m_morgan
ID: 6173836
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
ID: 6174841
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
ID: 6175630
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
ID: 6175671
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
ID: 6201261
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb wildfly example 2 107
maven disable workspace resolution 1 72
Java basic valueOf question 1 49
How do i find out all classes which contain nested class 2 13
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

737 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