JComboBox get current value
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: JComboBox get current value

  1. #1
    Join Date
    Sep 2006
    Location
    Wantagh,NY
    Posts
    151

    JComboBox get current value

    I am trying to get the "Current" value from a combo box. With getSelected Value you actually have to select a value for it to trigger. I have a gui with several tabs. The tabs share a common combo box. When switching from one tab to another, I want to be able to grab the value in the combobox after the tab is swiched.

    The default value of the combo box is "--Choose--". If I am on tab 1, and then switch to tab 2 and do a print of the combo box value, it comes up empty rather than "--Choose--".

    Here is the code I am using to get the value in the combobox currently.

    Code:
    class GoActionListener implements ActionListener {
    		public void actionPerformed(ActionEvent event) {
    			
    		//	musicListBox.remove(logoPanel);
    			String category = (String) categoryComboBox.getSelectedItem();
    			
    			//String cat = (String)
    			
    		    System.out.println(category);
    I want the value of Sys out to be "--Choose--" when the tab is switched.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: JComboBox get current value

    If I am on tab 1, and then switch to tab 2 and do a print of the combo box value, it comes up empty rather than "--Choose--".
    It will unless you select an item.

    You can call getSelectedIndex() and check to see if it returns -1 (nothing selected) and if so call getItemAt(0) to get the first item in the list.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Sep 2006
    Location
    Wantagh,NY
    Posts
    151

    Re: JComboBox get current value

    Yes, you are right, thanks!.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center