-
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?
-
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.
-
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?
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);
}
}
-
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.
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)
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.
-
Re: Hidden JText Field
Thank you for your help, the example anxwered my question perfectly!
-
Re: Hidden JText Field
Did you look at the Double API docs? There is also ""+doubleVal.