import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTabbedPane; import javax.swing.JTextField; import javax.swing.WindowConstants; public class Main extends JFrame{ public Main() { super("Change Tab Title"); setLayout(new BorderLayout()); genUI(); pack(); setLocationRelativeTo(null); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setVisible(true); } private void genUI() { JTabbedPane tp = new JTabbedPane(); tp.insertTab("1", null, new TabPane(0, tp), null, 0); tp.insertTab("2", null, new TabPane(1, tp), null, 1); add(tp, BorderLayout.CENTER); } public static void main(String [] arg) { new Main(); } private class TabPane extends JPanel { public TabPane(int pIndex, JTabbedPane pTabbedPane) { super(); setLayout(new BorderLayout()); _parent = pTabbedPane; _index = pIndex; _title = new JTextField(); _title.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e){ _parent.setTitleAt(_index, _title.getText()); } }); add(_title, BorderLayout.CENTER); } private int _index; private JTextField _title; private JTabbedPane _parent; } }
public class TabContent extends JPanel { public TabContent() { super(); } public void alertContentChanged() { listener.firePropertyChange(PROPERTY_ID, true, true); } public final static String PROPERTY_ID = "Tab Content Change"; }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionprivate JTabbedPane tab; /** * Modified tab title */ public void setTitleTab(JTabbedPane tab){ tab = (JTabbedPane)this; int number_tab = tab.getTabCount(); int current_tab = tab.getSelectedIndex(); for(int i=0;i<number_tab;i++){ String title = tab.getTitleAt(i); System.out.println("Title tab : "+title); System.out.println("1° caract : "+title.substring(0, 1)); if (i == current_tab && !title.substring(0, 1).equalsIgnoreCase("*")){ title = "* " + title; tab.setTitleAt(i,title); this.tab = tab; } } }
private JTabbedPane tab; private MyTabbedPane mytabbedpane; mytabbedpane.setTitleTab(tab);
@Getter public JTabbedPane getTab(){ tab = (JTabbedPane)this; return tab; }
private MyTabbedPane mytabbedpane; public FileTab(MyTabbedPane mytabbedpane) { this.mytabbedpane = mytabbedpane; }
public void setTitleTab(){ int current_tab = getSelectedIndex(); if (!getTitleAt(current_tab).substring(0, 1).equalsIgnoreCase("*")){ String title = this.getTitleAt(current_tab); System.out.println("Title tab (getTitleAt) : "+title); title = "* " + title; this.setTitleAt(current_tab,title); } }
@Getter public MyTabbedPane getMainPanel() { return mainPanel; }