Tab in JTabbedPane does not reflect changes on button press

后端 未结 1 1370
温柔的废话
温柔的废话 2021-01-23 04:37

Within a tab of my GUI, the user is allowed to edit an employee\'s name. The name also serves as the tab\'s label, so when the change is confirmed the tab should be updated to r

1条回答
  •  轮回少年
    2021-01-23 05:14

    You may be looking for the setTitleAt() method.

    Addendum: For comparison, here's an sscce that allows multiple edits.

    TabEdit

    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import javax.swing.AbstractAction;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    import javax.swing.JTextField;
    
    /**
     * @see http://stackoverflow.com/a/11007109/230513
     */
    public class TabEdit extends JPanel {
    
        private static final int MAX = 5;
        private static final String NAME = "Tab ";
        private final JTabbedPane pane = new JTabbedPane();
    
        public TabEdit() {
            for (int i = 0; i < MAX; i++) {
                pane.add(NAME + String.valueOf(i), new TabContent(i));
            }
            this.add(pane);
        }
    
        private class TabContent extends JPanel {
    
            private TabContent(final int i) {
                final JTextField jtf = new JTextField(
                    "Please edit the name of " + NAME + String.valueOf(i));
                this.add(jtf);
                jtf.addActionListener(new AbstractAction() {
    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        pane.setTitleAt(i, jtf.getText());
                    }
                });
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 120);
            }
        }
    
        private void display() {
            JFrame f = new JFrame("TabEdit");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(this);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new TabEdit().display();
                }
            });
        }
    }
    

    0 讨论(0)
提交回复
热议问题