我正在使用 JTabbedPane 并希望在用户选择另一个选项卡时进行一些字段验证。可能,根据他们对消息框的回复,让他们在同一个选项卡上更正错误数据。我正在实现 ChangeListener 接口并处理 stateChanged 事件,但是 getSelectedIndex() 返回新选择的选项卡。是否有简单的方法来 1)确定哪个是先前选择的选项卡和 2)在数据验证失败的情况下阻止用户转到新选择的选项卡?无法使用 stateChanged 事件。提前致谢,
问问题
2593 次
2 回答
5
您需要自己跟踪用户所在的选项卡。当用户导航到新选项卡时,您会更新一个 int 字段以指示用户已打开的选项卡。
如果您的验证例程表明用户需要更正前一个选项卡中的某些数据,您可以JTabbedPane.setSelectedIndex(int index)
在侦听器的stateChanged(ChangeEvent e)
-method 中使用 -method 将用户返回到他之前选择的选项卡。
于 2012-06-15T12:01:25.980 回答
3
嗯,你可以尝试这样的事情,它基本上是添加一个 ChangeListener 来监视当前选项卡并保留以前的选项卡选择:
public class JTabbedPaneExample extends JFrame {
private JTabbedPane tabbedPane;
private JPanel panel1, panel2, panel3;
private int currentTabIndex = 0, previousTabIndex = 0;
public JTabbedPaneExample() {
createAndShowUI();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JTabbedPaneExample jTabbedPaneExample = new JTabbedPaneExample();
}
});
}
private void createAndShowUI() {
setTitle("JTabbedPaneExample");
setSize(300, 300);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
addComponentsToContentPane(getContentPane());
addListeners();
//pack();
setVisible(true);
}
private void addComponentsToContentPane(Container contentPane) {
tabbedPane = new JTabbedPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
tabbedPane.insertTab("tab1", null, panel1, null, 0);
tabbedPane.insertTab("tab2", null, panel2, null, 1);
tabbedPane.insertTab("tab3", null, panel3, null, 2);
contentPane.add(tabbedPane);
}
private void addListeners() {
tabbedPane.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent ce) {
previousTabIndex = currentTabIndex;
currentTabIndex = tabbedPane.getSelectedIndex();
System.out.println("Current tab is:" + currentTabIndex);
System.out.println("Previous tab is:" + previousTabIndex);
}
});
}
}
编辑:这是一个较新的版本,如果验证变量设置为“true”,则仅允许选项卡更改,这是通过单击按钮完成的,只是为了显示逻辑:
public class JTabbedPaneExample extends JFrame {
private JTabbedPane tabbedPane;
private JPanel panel1, panel2, panel3;
private int currentTabIndex = 0, previousTabIndex = 0;
private boolean valid = false;
private JButton changeVariableBtn;
public JTabbedPaneExample() {
createAndShowUI();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JTabbedPaneExample jTabbedPaneExample = new JTabbedPaneExample();
}
});
}
private void createAndShowUI() {
setTitle("JTabbedPaneExample");
setSize(300, 300);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
addComponentsToContentPane(getContentPane());
addListeners();
//pack();
setVisible(true);
}
private void addComponentsToContentPane(Container contentPane) {
tabbedPane = new JTabbedPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
changeVariableBtn = new JButton("Set validation to true");
tabbedPane.insertTab("tab1", null, panel1, null, 0);
tabbedPane.insertTab("tab2", null, panel2, null, 1);
tabbedPane.insertTab("tab3", null, panel3, null, 2);
contentPane.add(tabbedPane, BorderLayout.NORTH);
contentPane.add(changeVariableBtn, BorderLayout.CENTER);
}
private void addListeners() {
tabbedPane.addChangeListener(new ChangeListener() {
private boolean automatedStateChange = false;
@Override
public void stateChanged(ChangeEvent ce) {
//this is used so when we allow the user not to go to the new tab by setting the tabb index to the previous one we dont want our changelistener to fire again as if the user were changing the tabs
if (automatedStateChange) {
automatedStateChange = false;
} else {
previousTabIndex = currentTabIndex;
currentTabIndex = tabbedPane.getSelectedIndex();
}
if (valid) {
System.out.println("Current tab is:" + currentTabIndex);
System.out.println("Previous tab is:" + previousTabIndex);
System.out.println("Validation succeeded: " + valid);
changeVariableBtn.doClick();
} else {
System.out.println("You need to enter all valid data first!");
automatedStateChange = true;
tabbedPane.setSelectedIndex(previousTabIndex);
}
}
});
changeVariableBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
if (valid) {
valid = false;
changeVariableBtn.setText("Set validation to true");
} else {
valid = true;
changeVariableBtn.setText("Set validation to false");
}
}
});
}
}
于 2012-06-15T12:19:35.050 回答