• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JComboBox not entirely working

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, here is where I initialize the nameLabel array:



Here is a snippet of my creating of the JComboBox...



Next, here is where I try to get the information from it:



The weird thing is, it works except for when the user selects "Mr." When that happens, it prints "null" in the textArea. Why is this? Thanks!
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alex Bruhart:
... The weird thing is, it works except for when the user selects "Mr." When that happens, it prints "null" in the textArea. Why is this? Thanks!


I think you mean it works except for when the user does not select anything, and just leaves the default of "Mr." showing in the comboBox.

It looks like preName is only assigned a new value if the actionPerformed method is invoked, which only happens if the user changes the selection in the comboBox. If they leave the default of "Mr.", then actionPerformed doesn't run, and preName still has its default null value.

(Note: If the user selects a different value first, but then goes back and selects "Mr," then it works because the method runs on the change.)

So you need to make sure that preName gets a value even if actionPerformed doesn't run.
 
Alex Bruhart
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That would be the reason. Thanks again, I really appreciate it.
 
Alex Bruhart
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another question, this time involving a JScrollPane.

I keep trying to use a JScrollPane, but it doesn't work.
This is the code where I make it.



What's wrong? Is it because I call a method that involves adding something to displayTA later?
[ June 16, 2008: Message edited by: Alex Bruhart ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alex Bruhart:
...
pane.add(new JScrollPane(displayTA));
displayTA = new JTextArea();
displayTA.setFont(new Font("Courier",Font.PLAIN,12));
displayTA.setBackground(new Color(255, 0, 25));
displayTA.setLocation(250,50);
displayTA.setSize(200,250);
displayTA.setLineWrap(bool);
displayTA.setWrapStyleWord(bool);
pane.add(displayTA);
...


You're close, just a bit out of order.

First, prepare your JTextArea. Then add the JTextArea to a JScrollPane. Then add the JScrollPane to the frame. Do not add the JTextArea directly to the frame, because it will not be inside a JScrollPane.

So in your code snippet, remove that last line (pane.add(displayTA);) , and move the first line (pane.add(new JScrollPane(displayTA));) to the end.
[ June 16, 2008: Message edited by: marc weber ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic