Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please someone help

 
john mattucci
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank u in advance for whomever will take the time to help me
My problem is as follows. I have a JComboBox with a number of choices; and also a JButton which allows the user to clear his choice just made. So for example, the user picked the 2nd item on
the list and he wishes to clear it he presses clear. The problem is this I wish to fire an actionEvent only when an item is picked
not when clear is pressed. Because the way I have written the code when clear is pressed the first item in the JComboBox is picked which is blank, and this fires an actionEvent which I dont want to happen. How do I stop it? I hope this is clear....
 
ravindran shanmugam
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.awt.*;
public class Mycombo extends JFrame
{
public Mycombo()
{
JButton btnClear = new JButton("Clear");
final JComboBox cmbSelect = new JComboBox();
cmbSelect.addItem("");
cmbSelect.addItem("sdfw");
cmbSelect.addItem("mnbj");
cmbSelect.addItem("qerre");
cmbSelect.setEditable(true) ;
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
contentPane.setPreferredSize(new Dimension(400, 150));
contentPane.add(cmbSelect, BorderLayout.CENTER);
contentPane.add(btnClear, BorderLayout.SOUTH);
setContentPane(contentPane);
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
cmbSelect.setSelectedItem("");
System.out.println("Selected value is "+ (cmbSelect.getSelectedItem()).toString()+ "edsrtger");
}
});
}
public static void main(String[] args)
{
JFrame frame = new Mycombo();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.pack();
frame.setVisible(true);
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic