Results 1 to 6 of 6
  1. #1
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    42
    Rep Power
    0

    Default Hidden JText Field

    I know how to create a JText field...what is throwing a monkey-wrench is the fact that I need to have 2 JText fields visible, and a 3rd that is only visible when the "Check Answers" button is pressed. How do I 1st off set ONE JText field to visible = False (I am assuming this would be the proper coding to use) and set it to Visble = True only when the Check Answers button is pressed?

  2. #2
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Hidden JText Field

    Don't assume: read the API docs or a tutorial. (Not being inclined to practice what I preach - who does? - ) I would assume that Component is the place to look as any component could, logically be made visible or invisible.

    Once you've located the method use it in the button's action handler.

  3. #3
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    42
    Rep Power
    0

    Default Re: Hidden JText Field

    I am attempting to add the code to create the JLabel once the button has been pressed under the buttons action handler as you suggested. However my code isn't creating the JLabel?! This is how I coded it, where did I err?
    Java Code:
    	public void actionPerformed(ActionEvent evt)
    	{
    		if(evt.getSource() == totalBtn)
    		{
    			double area=Double.parseDouble(lenTxt.getText()) * Double.parseDouble(widthTxt.getText());
    			//Trying to use the below code to create the JLabel after the click event
    			JLabel areaLbl = new JLabel("The area is:" + area);
    			this.add(areaLbl);
    			setVisible(true);
    		}
    	}

  4. #4
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Hidden JText Field

    In fact my suggestion was to make it visible within the event handler. The best place to create the label is probably whereever the rest of the gui is created: that way the gui can be laid out the way you want. It's not wrong to create components like that, but it is unnecessarily confusing. For instance it is not clear what

    Java Code:
    this.add(areaLbl)
    will do.

    In the actionPerformed() method you posted you call setVisible(true) on whatever thing it is that the method is a method of. You should be calling the method on the label instance.

    -----

    Maybe this will help. (If not, post some compilable, runnable code)

    Java Code:
    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.JLabel;
    import javax.swing.SwingUtilities;
    
    
    public class Eg extends JFrame {
        
        private JLabel label;
        private JButton button;
        
        public Eg() {
            super("Eg");
            setLayout(new GridLayout(2, 0));
            button = new JButton("Show!");
            button.addActionListener(toggler);
            add(button);
            
                // label constructed here with the rest of the gui
            label = new JLabel("Visible");
            label.setVisible(false);
            add(label);
            
            pack();
        }
        
            // label made visible (or not) here
        private ActionListener toggler = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                if(label.isVisible()) {
                    label.setVisible(false);
                    button.setText("Show!");
                } else {
                    label.setVisible(true);
                    button.setText("Hide!");
                }
            }
        };
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    Eg test = new Eg();
                    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    test.setLocationRelativeTo(null);
                    test.setVisible(true);
                }
            });
        }
    }
    I prefer to have an ActionListener that does a specific job (the toggler) rather than an all purpose actionPerformed() that is "stuck on" to the type of the class. The way I think of it Eg is not intrinsically an action listener (intrinsically it is a frame), so rather than "implements ActionListener" I would use an ActionListener instance variable.

    This code shows Eg as a frame while your code should almost certainly prefer a panel. (And in main() create a frame and put the panel into it.) Panels are reusable by different frames.

  5. #5
    jo15765 is offline Member
    Join Date
    May 2012
    Posts
    42
    Rep Power
    0

    Default Re: Hidden JText Field

    Thank you for your help, the example anxwered my question perfectly!
    Last edited by jo15765; 05-29-2012 at 05:27 AM.

  6. #6
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Hidden JText Field

    Did you look at the Double API docs? There is also ""+doubleVal.

Similar Threads

  1. problem displaying calculation in JText field
    By smallmos1 in forum New To Java
    Replies: 1
    Last Post: 02-10-2011, 09:15 PM
  2. front end display of field description when clicking the field name
    By neils in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 10-29-2010, 12:47 PM
  3. Jtree in Jsplitpane + Jtext
    By raomore in forum AWT / Swing
    Replies: 1
    Last Post: 10-20-2010, 11:42 AM
  4. JText area help?
    By Jcbconway in forum AWT / Swing
    Replies: 1
    Last Post: 09-16-2010, 02:20 AM
  5. Replies: 4
    Last Post: 01-18-2010, 12:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •