Get the in-focus tab after closing a tab

后端 未结 1 1650
野性不改
野性不改 2021-01-29 05:46

In a JTabbedPane, I associated a custom-made Data object to each added tab. I also have a corresponding Metadata object that shows up in another panel when the tab is selected.

相关标签:
1条回答
  • 2021-01-29 06:26

    Devil is in the detail, which you provided none.

    I did a quick test and discovered that, ChangeListener is called before ContainerListener, which is a real pain, but, it was always reporting the correct index.

    So, what you need to do is marry the two together, so that, both will update the meta data pane when they are called.

    import java.awt.EventQueue;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ContainerEvent;
    import java.awt.event.ContainerListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JTabbedPane tabbedPane = new JTabbedPane();
                    tabbedPane.addTab("One", new TabPane(tabbedPane));
                    tabbedPane.addTab("Two", new TabPane(tabbedPane));
                    tabbedPane.addTab("Three", new TabPane(tabbedPane));
                    tabbedPane.addTab("Four", new TabPane(tabbedPane));
    
                    tabbedPane.addContainerListener(new ContainerListener() {
                        @Override
                        public void componentAdded(ContainerEvent e) {
                        }
    
                        @Override
                        public void componentRemoved(ContainerEvent e) {
                            System.out.println("Removed " + e.getChild());
    
                        }
                    });
    
                    tabbedPane.addChangeListener(new ChangeListener() {
                        @Override
                        public void stateChanged(ChangeEvent e) {
                            System.out.println(tabbedPane.getSelectedIndex());
                        }
                    });
    
                    JFrame frame = new JFrame();
                    frame.add(tabbedPane);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TabPane extends JPanel {
            private JTabbedPane parent;
    
            public TabPane(JTabbedPane parent) {
                this.parent = parent;
                setLayout(new GridBagLayout());
                JButton btn = new JButton("Close");
                add(btn);
    
                btn.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        parent.remove(TabPane.this);
                    }
                });
            }
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题