Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Basically, instead of "adding" a tab, you need to "insert" one, something like...</p> <pre><code> Component tabComp = ... tab.insertTab("Title", null, tabComp, null, index); </code></pre> <p>For example, where <code>index</code> is the location where you want the new tab to be added. If you just want to maintain the "+" at the end, the something like <code>tab.getTabCount() - 1</code> should do the trick</p> <p>If you want to add the tab next to the last active tab, then you need to get a little more adventurous...</p> <p>The problem is, when the <code>stateChanged</code> event is triggered, the selection has already changed, so doing <code>tab.getSelectedIndex()</code> will always return the "+" tab.</p> <p>What you need to know is the tab that was selected BEFORE it.</p> <p>What you could do is inspect the selected tab when the state changed, if it's not the "+" tab, you will want to record the selected index value. </p> <p>When the "+" tab becomes active, you would simply use the "last selected index" value your have being tracking as a base for inserting the new tab...</p> <p>For example...</p> <pre><code>import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTabbedPane; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class TestTabSelection { public static void main(String[] args) { new TestTabSelection(); } public TestTabSelection() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } final JTabbedPane tab = new JTabbedPane(); tab.addTab("A", new JPanel()); tab.addTab("+", new JPanel()); tab.getModel().addChangeListener(new ChangeListener() { private int lastSelected; private boolean ignore = false; @Override public void stateChanged(ChangeEvent e) { if (!ignore) { ignore = true; try { int selected = tab.getSelectedIndex(); String title = tab.getTitleAt(selected); if ("+".equals(title)) { JPanel pane = new JPanel(); tab.insertTab("Tab" + (tab.getTabCount() - 1), null, pane, null, lastSelected + 1); tab.setSelectedComponent(pane); } else { lastSelected = selected; } } finally { ignore = false; } } } }); final JButton btn = new JButton("Add"); btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println(tab.getTabCount()); } }); JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(tab); frame.add(btn, BorderLayout.SOUTH); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload