Results 1 to 6 of 6
Thread: Hidden JText Field
- 05-28-2012, 05:29 AM #1
Member
- Join Date
- May 2012
- Posts
- 15
- Rep Power
- 0
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?
- 05-28-2012, 07:58 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
- 05-28-2012, 05:09 PM #3
Member
- Join Date
- May 2012
- Posts
- 15
- Rep Power
- 0
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); } }
- 05-29-2012, 01:43 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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
will do.Java Code:this.add(areaLbl)
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)
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.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); } }); } }
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.
- 05-29-2012, 04:04 AM #5
Member
- Join Date
- May 2012
- Posts
- 15
- Rep Power
- 0
Re: Hidden JText Field
Thank you for your help, the example anxwered my question perfectly!
Last edited by jo15765; 05-29-2012 at 04:27 AM.
- 05-29-2012, 04:18 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Similar Threads
-
problem displaying calculation in JText field
By smallmos1 in forum New To JavaReplies: 1Last Post: 02-10-2011, 08:15 PM -
front end display of field description when clicking the field name
By neils in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 10-29-2010, 11:47 AM -
Jtree in Jsplitpane + Jtext
By raomore in forum AWT / SwingReplies: 1Last Post: 10-20-2010, 10:42 AM -
JText area help?
By Jcbconway in forum AWT / SwingReplies: 1Last Post: 09-16-2010, 01:20 AM -
how from an Access Currency field I populate a hidden field
By lse123 in forum Java ServletReplies: 4Last Post: 01-17-2010, 11:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks