queria q a mensagem do jlabel desse ok apenas quando o combobox estivesse no t2 e o botão fosse apertado, como faço??
package texte;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class tela {
private JFrame frame;
/**
* Launch the application.
*/
JComboBox comboBox = new JComboBox();
JLabel lblNewLabel = new JLabel("Nao sincrtonizado");
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
tela window = new tela();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public tela() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
comboBox.setModel(new DefaultComboBoxModel(new String[] {"t1", "t2", "t3"}));
comboBox.setBounds(10, 11, 142, 42);
frame.getContentPane().add(comboBox);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
comboBox.setSelectedItem("t2");
if(comboBox.setSelectedItem("t2")){
lblNewLabel.setText("OK funcionou");
}
else{
lblNewLabel.setText("Não funcionou!");
}
}
});
btnNewButton.setBounds(26, 160, 126, 64);
frame.getContentPane().add(btnNewButton);
lblNewLabel.setBounds(224, 37, 186, 35);
frame.getContentPane().add(lblNewLabel);
}
}