我有一個嵌套了JTabbedPanes的應用程序。每個JTabbedPanes包含其他JTabbedPanes。如何防止在JTabbedPane上留下選項卡
我想在允許用戶離開當前選項卡之前檢查一些內容。
當您運行下面的代碼時,您會看到Months-> February選項卡上有一個ALLOW複選框。
如果允許複選框被選中,那麼我想允許用戶導航離開當前面板。如果不是,那麼他們不能離開,直到它。
我似乎無法找到一種方法來處理請求離開之前下一個組件(可能在另一個JTabbedPane)顯示。
這可以通過轉到Months-> February選項卡,然後選擇顏色選項卡來演示。
任何想法?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
public class VerifyBeforeLeavingTab extends JFrame
{
private static final long serialVersionUID = 1L;
public VerifyBeforeLeavingTab()
{
setSize(700, 700);
JTabbedPane mainTabbedPane = new JTabbedPane(JTabbedPane.TOP);
MyJTabbedPane colorsPane = new MyJTabbedPane(JTabbedPane.TOP, "colors");
JPanel bluePanel = new JPanel();
bluePanel.setBackground(Color.blue);
colorsPane.addTab("Blue", bluePanel);
JPanel redPanel = new JPanel();
redPanel.setBackground(Color.red);
colorsPane.addTab("Red", redPanel);
mainTabbedPane.addTab("Colors", colorsPane);
JTabbedPane monthsPane = new MyJTabbedPane(JTabbedPane.TOP, "months");
JPanel janPanel = new JPanel();
monthsPane.addTab("January", janPanel);
JPanel febPanel = new MyUnleavableJPanel();
monthsPane.addTab("February", febPanel);
mainTabbedPane.addTab("Months", monthsPane);
add(mainTabbedPane, BorderLayout.CENTER);
getContentPane().add(mainTabbedPane);
}
private class MyUnleavableJPanel extends JPanel
{
private static final long serialVersionUID = 1L;
public MyUnleavableJPanel()
{
final JCheckBox chckBoxAllowToLeave = new JCheckBox("Allow to leave");
chckBoxAllowToLeave.setBounds(100, 100, 50, 50);
this.add(chckBoxAllowToLeave);
addHierarchyListener(new HierarchyListener()
{
public void hierarchyChanged(HierarchyEvent e)
{
if ((HierarchyEvent.SHOWING_CHANGED & e.getChangeFlags()) != 0)
{
if (isShowing())
{
System.out.println("Showing an unleavable panel");
}
else
{
// TODO: Do not let them leave this JCheckbox is selected
if (chckBoxAllowToLeave.isSelected())
{
System.out.println("OK to leave");
}
else
{
System.out.println("Not allowed to leave");
}
}
}
}
});
}
}
private class MyJTabbedPane extends JTabbedPane
{
private static final long serialVersionUID = 1L;
public MyJTabbedPane(int i, String name)
{
super(i);
setName(name);
}
@Override
public void setSelectedIndex(int index)
{
System.out.println("Now on '" + getName() + "' tab #" + index);
super.setSelectedIndex(index);
}
}
private static void createAndShowGUI()
{
// Create and set up the window.
VerifyBeforeLeavingTab frame = new VerifyBeforeLeavingTab();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
try
{
createAndShowGUI();
}
catch (Exception e)
{
e.printStackTrace();
System.exit(0);
}
}
});
}
}
另請參閱此[答案](http://stackoverflow.com/a/2016686/230513)。 – trashgod