Hi,
Mình tạo 1 combobox , khi click vào item của combobox -> Hiện lên 1 dialog confirm:
① Nếu click Yes thì thực hiện việc A
② Nếu click No thì value của combobox sẽ KHÔNG BỊ thay đổi, tức là: nếu giá trị của combobox đang là [Item 1] -> click vào Item 2 -> Dialog confirm display -> Click No -> Giá trị selected của combobox vẫn là [Item 1] chứ ko phải Item 2
Cái thứ ② mình thử làm mà chưa dc, mình có up source, các bạn xem giúp nhé!
Mã nguồn PHP:
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class ComboBox {
private static JComboBox cmb = new JComboBox();
public static void main(String[] args) {
JFrame f = new JFrame();
cmb.addItem("Item 1");
cmb.addItem("Item 2");
cmb.addItem("Item 3");
cmb.addItem("Item 4");
cmb.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.DESELECTED) {
String itemBefore = (String) e.getItem();
doSomething(itemBefore);
}
}
});
cmb.setBounds(130, 150, 100, 40);
f.add(cmb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void doSomething(String itemBefore){
int dialogButton = JOptionPane.YES_NO_OPTION;
int dialogResult = JOptionPane.showConfirmDialog (null, "Do you want to continue?","Warning",dialogButton, JOptionPane.QUESTION_MESSAGE);
if(dialogResult == JOptionPane.YES_OPTION){
//Thực hiện việc A
}
else {
//Chọn lại item trước
cmb.setSelectedItem(itemBefore);
// JOptionPane.showMessageDialog(null, "Item Before: " + itemBefore);
}
}
}
View more random threads:
Ngọc Bích xanh bản chất thuộc dòng đá đá hoa (jade). Và cẩm thạch là tên gọi chung của ngọc bích. Vì thực chất chúng thuộc dòng đá đa khoáng được hình thành từ chất Silicat dưới dạng dioxy. Ngọc bích...
Chia sẻ Vòng tay ngọc bích xanh là...