0

I want a JTextField to appear when user select "Extra" in JComboBox list and hidden otherwise. But the JTextField is not appearing when user is selecting "Extra" but it is appearing if i shrink the window and enlarge it again. After doing like that it is working properly, only for the first time it is not appearing. Why it is behaving like that? any solution for this?

Here is my code:

getCmbJtocsv_C_8().addActionListener(new ActionListener() { 
   public void actionPerformed(ActionEvent e) {
   {

      input3 = (String)adaptor.getCmbJtocsv_C_8().getSelectedItem();
      adaptor.getTxtJtocsv_C_8().setVisible(false);

      if(input3.equals("extra")){
          adaptor.getTxtJtocsv_C_8().setVisible(true);
      }
}

i have changed the string comparison and i'm still getting the same result.

mariz
  • 509
  • 1
  • 7
  • 13
  • here is your problem. input3=="extra". You need to learn how to compare (String) Objects – Stultuske Jul 11 '16 at 12:58
  • i tried input3.equals("extra") too,still getting the same result. – mariz Jul 11 '16 at 13:05
  • 1
    possible reason may be the layout manager you use. full code or runable piece of code could be helpfull for clearly address the problem. – mfidan Jul 11 '16 at 13:20
  • 1
    This question was originally closed as a duplicate to: http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java. Although the comparison is an issue, the question also states that the code works after the frame is resized which would not be the case if the comparison was the only problem. Therefore I have re opened the question and am waiting for the OP to post a proper [SSCCE](http://sscce.org/) so that we can provide more help. – camickr Jul 11 '16 at 14:31

1 Answers1

1

By default Swing components have a size of (0, 0). The size/location of a component is determined by the layout manager when the frame is packed or made visible.

So I would guess that since you start with the text field invisible the size of its parent panel does not include the text field. When you resize the frame the text field appears because its size and the size of the panel is recalculated.

Now the size of the text field has been calculated so you can toggle the visibility as required.

If this doesn't help then post a SSCCE that demonstrates the problem. That is create a frame with only a combo box and text field to demonstrate the problem. In the future all questions should contain a SSCCE since we can't guess the context of your application based on a few lines of code.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • yup,that's the exact problem here.now i changed my layout to card layout,in the first card the panel will be empty and in the second card the textbox will be there.then I'm showing the card which is needed accordingly.now the code works fine,thanks... – mariz Jul 12 '16 at 05:53
  • @mariz, `i changed my layout to card layout` - Always a good idea to let the layout manager do the work. – camickr Jul 12 '16 at 15:00