File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
*
The moose likes Beginning Java and the fly likes Getting String text from a Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Getting String text from a "private" combobox" Watch "Getting String text from a "private" combobox" New topic
Author

Getting String text from a "private" combobox

Esben Christensen
Greenhorn

Joined: Apr 22, 2009
Posts: 8
Hello, I´m having a problem with my coding, what I want to do is to get the loop to update a String with the information of a combobox which is private (I´m using NetBeans and I can´t get it to make the combobox public). Here´s my Main:



And here´s the code for the combobox:


I Hope you understand what I mean. Thanks in advance, any help is very apreciated.

[edit]Insert newlines in the code CR[/edit]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40079
    
  28
Welcome to JavaRanch
Thank you for finding the code button.

I think what you are doing is writing Panelet which is the name of the class, rather than panel which is the identifier for the instance.
But you are setting up the Panelet object in the main method, so it is out of scope when you reach the Main class.

Not certain about the rest of it, but . . .
I suspect you need to move all the code to set up the GUI into the Main class (probably the constructor), and move the code to start the Thread into the main method.

If the JComboBox has private access in the Panelet class, you should provide getter methods which provide access to the selected item in the JComboBox.

Rather than struggling with NetBeans (although it is correct to insist on private access to a field) you should consider a decent text editor (if on Windows try jEdit. NotePad2 or NotePad++, not NotePad) and using the command line.
Esben Christensen
Greenhorn

Joined: Apr 22, 2009
Posts: 8
Thank you for your quick answer but it´s for an exam project and we have to make it through NetBeans, can you clarify how you would do that getter methods to provide access to the selected item in the JComboBox?

Thanks in advance
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40079
    
  28
The usual way. Set up a getSelectedComboboxItemAsString() method which returns the myCombobBox.getSelectedItem().etc().

If it is for an exam, I oughtn't to say any more, or it could lose you marks.
Esben Christensen
Greenhorn

Joined: Apr 22, 2009
Posts: 8
I´ve made it work as long as I´m not using the loop but I need it to do it every time the loop runs through that´s my major problem, as for the exam part we are allowed to get help for our projects as long as we are open about it.

Any idea how to make it work in a loop? I´ve tried so many things but can´t make it work.

Thanks in advance
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40079
    
  28
What have you tried? Where did you put the loop?
Esben Christensen
Greenhorn

Joined: Apr 22, 2009
Posts: 8
I've tried with this in the Panelet class which contains my GUI:


In my Main loop


Mainly things like that moving the sentences around but never getting it to do it.




Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40079
    
  28
I don't think either of those solutions will work.

Back to square 1. How are you going to get the details of the selected Object from the combo box? To help: API documentation. Then how are you going to return that information from the Panelet class?

Remember: fields ought to have private access, so you shouldn't use a . before the name of the field outside its own class.
Esben Christensen
Greenhorn

Joined: Apr 22, 2009
Posts: 8
Thanks for trying to help but I really can´t figure out how to get the SelectedItem out of the JComboBox and into a String in a loop. without the loop I made it work with the following in the class Panelet:


but again I really need it to work through the loop.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40079
    
  28
Where does the ComboBoxActionPerformed method come from? Is it something NetBeans has added for itself? It doesn't appear to be part of the standard Sun API.
Is ComboBox a class or an object? It is confusing if you don't follow the usual capitalisation conventions.
Esben Christensen
Greenhorn

Joined: Apr 22, 2009
Posts: 8
The ComboBoxActionPerformed method comes from NetBeans indeed and by ComboBox i Mean the ComboBoxActionPerformed. Sorry for the confusion
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40079
    
  28
Still can't understand how the ComboBoxActionPerformed method is called, but it appears to work. . . . What have you done about loops?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting String text from a "private" combobox