Skip to main content

How can i make an event to a JComboBox which triggers AFTER selection?



I want to make an event which will be triggered after i make the selection to the JComboBox. the problem I'm now facing is that when i added an ActionListener, it was triggered when the user clicked on the box but BEFORE he actually chose the new item, thus the action listener was activated all the time on the previous value which was selected in the box. what i want to do is simply changing the title of an JTextArea according to the selection. I tried doing something like this:







jBox.addActionListener(new ActionListener(){

@Override

public void actionPerformed(ActionEvent e) {

String alt = GetAlgoAreaTitleByChoice();

panel.remove(jArea);

currentBest = setArea("",alt);

currentBest.setBounds(50, 350, 1000, 290);

panel.add(jArea);

}

});







and the method inside:







private String GetArgsAreaTitleByChoice(){

String chi = jBox.getSelectedItem().toString();

if(chi.equals(generalChoice)){

return "Hello";

}

else if(chi.equals(algoChoice)){

return "World";

}

else if(chi.equals(argsChoice)){

return "Hello";

}

return null;

}







I've tried using the SELECTED events now like this:







public void itemStateChanged(ItemEvent e) {

JComboBox cb = (JComboBox)e.getSource();



// Get the affected item

String item = cb.getSelectedItem().toString();



if (e.getStateChange() == ItemEvent.SELECTED) {

panel.remove(jBox);

textArea = setArea("", item);

panel.add(jBox);

}







but it seems to remove the area from the panel without adding it back... why is this happening?


Comments

  1. For listening of events from JComboBox is better implements ItemListener, returns two events SELECTED/DESELECTED

    EDIT

    if you remove/add JComponent(s) on Runtime and in already visible container, then you have to call (as least code lines)

    revalidate();
    repaint();

    ReplyDelete
  2. Here is a simple demonstration with a sample code :

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class Tester {

    public Tester(){

    JComboBox box = new JComboBox();
    box.addItem("One");
    box.addItem("Two");
    box.addItem("Three");


    box.addItemListener(new ItemListener(){
    public void itemStateChanged(ItemEvent e){

    if(e.getStateChange()==ItemEvent.SELECTED){

    e.getItem(); //Do what ever you want :))

    }
    }
    });

    JFrame frame = new JFrame();
    frame.getContentPane().add(box);
    frame.pack();
    frame.setVisible(true);
    }

    public static void main(String [] args) {
    Tester tester = new Tester();
    }
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.