This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes JComboBox not entirely working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JComboBox not entirely working" Watch "JComboBox not entirely working" New topic
Author

JComboBox not entirely working

Alex Bruhart
Greenhorn

Joined: Apr 20, 2008
Posts: 8
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!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Alex Bruhart
Greenhorn

Joined: Apr 20, 2008
Posts: 8
That would be the reason. Thanks again, I really appreciate it.
Alex Bruhart
Greenhorn

Joined: Apr 20, 2008
Posts: 8
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

Joined: Aug 31, 2004
Posts: 11343

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 ]
 
jQuery in Action, 2nd edition
 
subject: JComboBox not entirely working
 
Similar Threads
using an applet to open another window (JFrame)
JComboBox show just one line.
Problem with display of JComboBox
Java error
If statement problem!!