单击JTabbedPane中禁用的选项卡上的事件

时间:2012-07-30 12:27:42

标签: java swing tabs

我有JTabbedPane,里面有5个标签。禁用位置2,3和5处的选项卡。我想在点击此禁用选项卡时显示JDialog。我尝试添加鼠标监听器并更改监听器,但没有任何方法可以帮助我。使用鼠标单击方法,我可以单击该选项卡,但无法识别单击哪个选项卡。如何在JTabbedPane中禁用的选项卡上获取点击事件?

以下是我的尝试:

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Test {
    public static void main(String[] args) throws InterruptedException, InvocationTargetException {
        SwingUtilities.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Test tab pane click");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JTabbedPane tabbedPane = new JTabbedPane();
                JPanel panel1 = new JPanel();
                panel1.add(new JLabel("HI1"));
                tabbedPane.addTab("Tab 1", panel1);
                JPanel panel2 = new JPanel();
                panel2.add(new JLabel("HI2"));
                tabbedPane.addTab("Tab 2", panel2);
                JPanel panel3 = new JPanel();
                panel3.add(new JLabel("HI3"));
                tabbedPane.addTab("Tab 3", panel3);
                JPanel panel4 = new JPanel();
                panel4.add(new JLabel("HI4"));
                tabbedPane.addTab("Tab 4", panel4);
                JPanel panel5 = new JPanel();
                panel3.add(new JLabel("HI5"));
                tabbedPane.addTab("Tab 5", panel5);
                frame.setLayout(new BorderLayout());
                frame.add(tabbedPane, BorderLayout.CENTER);
                frame.setSize(500, 200);
                frame.setVisible(true);

                // disable 2nd, 3rd and 5th tab.
                tabbedPane.setEnabledAt(1, false);
                tabbedPane.setEnabledAt(2, false);
                tabbedPane.setEnabledAt(4, false);

                tabbedPane.addChangeListener(new ChangeListener() {
                    @Override
                    public void stateChanged(ChangeEvent e) {
                        System.out.println("I'm not called for disabled tabs.");
                    }
                });
                tabbedPane.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        System.out.println("I'm called for disabled tabs, but not able to know which tab is clicked.");
                    }
                });
            }
        });
    }
}

1 个答案:

答案 0 :(得分:4)

With mouse click method I can get that tab is clicked but not able to identify particular which tab is clicked.

您可以使用JTabbedPane的{​​{1}}方法(indexAtLocation(int, int)MouseEventgetX()),它会告诉您包含标签的索引给定的位置。