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ì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é!
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);
}
}
}