Results 1 to 8 of 8
  1. #1
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default Question on ActionListener in combo box

    I'm figuring out how to change a combo box using action listener

    Java Code:
    		JComboBox cboUnitID = new JComboBox();
    		cboUnitID.setModel(new DefaultComboBoxModel(new String[] {"PCS", "PAIR"}));
    		cboUnitID.setBounds(196, 327, 244, 20);
    		contentPane.add(cboUnitID);
    
                    JButton btnNew = new JButton("New");
    		btnNew.setBounds(80, 408, 89, 23);
    		contentPane.add(btnNew);
    		btnNew.addMouseListener(new nRec());
    
            public class nRec extends MouseAdapter {
    		  public void mouseClicked(MouseEvent newRecord){
    			  cboUnitID.setSelectedIndex(1);
    		  }
    	}
    .setSelectedIndex(1) seems isn't working as what I want.
    the combo box has 2 items so 1 should work either it starts from 0 or 1
    Is it possible to reset a combo box selected item when a button is pressed ? or else I might miss something there with my combo box.
    Last edited by Levian; 07-05-2011 at 05:50 AM.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    .setSelectedIndex(1) seems isn't working as what I want.
    Well, you haven't explained what you want and what is happening and I'm not a mind reader.

    However, setSelectedIndex(...) should work if your requirement is to change the selection to "PAIR".

    I would guess the problem is that you have a class variable and a local variable called cboUnitId, so the ActionListener is trying to change the wrong variable. Get rid of the local variable.

    Also:

    a) you should not add a MouseListener to the button. You should use an ActionListener.

    b) you should not use a nul layout. You should be using layout managers.

    If you need more help then you need to post a: Short, Self Contained, Correct Example

  3. #3
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default

    I tried to have one button to click to set the combo box to a certain selected option...means when the new button is clicked it'll empty all texfields (worked out) and return the combo box to a default selected item.

    Java Code:
    public class MatModule extends JFrame {
    
    	private static final long serialVersionUID = -5505615683022863857L;
    	public JComboBox cboUnitID;
    
    	public MatModule() throws SQLException {
    
    		JComboBox cboUnitID = new JComboBox();
    		cboUnitID.setModel(new DefaultComboBoxModel(new String[] {"PCS", "PAIR"}));
    		cboUnitID.setBounds(196, 327, 244, 20);
    		contentPane.add(cboUnitID);
    That's all I have about this combo box...as I see I only declare it once.
    At first I used actionlistener but since it's mouse click, isn't it more appropriate to use mouselistener instead ?
    I used absolute layout and still in the learning way on using the layouts, I got a word about that earlier about the layout but since the code is done, I guess I'll just get the problem solved first while learning about the layouts.
    Last edited by Levian; 07-05-2011 at 08:34 AM.

  4. #4
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default

    And here's the error in console...forgot about that

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at modMat.MatModule$nRec.mouseClicked(MatModule.java: 203)
    at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    line 203 as noted in it is :
    cboUnitID.setSelectedIndex(0);

    I tried to change the index to 0 from 1 with little hope that it'll solve the problem and end up as expected the problem persists

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    Don't you read the responses to your question?

    Quote Originally Posted by camickr View Post
    I would guess the problem is that you have a class variable and a local variable called cboUnitId, so the ActionListener is trying to change the wrong variable. Get rid of the local variable.

    If you need more help then you need to post a: Short, Self Contained, Correct Example
    db

  6. #6
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default

    I only have 1 declaration of the combo box

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    Quote Originally Posted by Levian View Post
    I only have 1 declaration of the combo box
    Oh yeah?

    Java Code:
    public class MatModule extends JFrame {
    
    	private static final long serialVersionUID = -5505615683022863857L;
    	public JComboBox cboUnitID; // 1
    
    	public MatModule() throws SQLException {
    
    		JComboBox cboUnitID = new JComboBox(); // 2
    db

  8. #8
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default

    Ah I see, I first thought the 2nd is to instantiate it...my mistake...

    OK, moving the instantiation into the first declaration clear the problem, thanks to both of you.
    And thanks for the SSCCE, I'll keep that in mind.
    Last edited by Levian; 07-05-2011 at 09:35 AM.

Similar Threads

  1. Calbutton actionListener question
    By bequick01 in forum New To Java
    Replies: 0
    Last Post: 05-04-2011, 03:17 PM
  2. dynamically populate the city combo box based on the values of state combo +ajax+jsp
    By sandy1000 in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-29-2010, 10:00 AM
  3. Combo Box
    By efozdel in forum New To Java
    Replies: 1
    Last Post: 07-07-2010, 03:29 PM
  4. help get the value from the combo box
    By archcavalier in forum Enterprise JavaBeans (EJB)
    Replies: 7
    Last Post: 05-23-2010, 06:03 PM
  5. creation of one combo box form another combo box
    By er.tyagigaurav in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-09-2008, 03:14 PM

Posting Permissions

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