This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JComboBox not entirely working

 
Alex Bruhart
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic