1

I am trying to create a tabbed pane in my Java Swing application, but it's not working.

When setting a JTabbedPane as the contentpane, everything goes fine. As soon as I try to add a tab, I get an ArrayIndexOutOfBoundsException: 0.

The component gets added to the pane nonetheless, it's just that this error is thrown. My code and the error are down below.

Code:

// This all happens in a class which extends JFrame

private JTabbedPane contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            try {
                ConfigBuilderWindow frame = new ConfigBuilderWindow();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public ConfigBuilderWindow() {
    setTitle("Config Builder");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 916, 617);
    contentPane = new JTabbedPane();
    contentPane.setBorder(new LineBorder(Color.BLUE, 4));
    contentPane.setLayout(new BorderLayout(0, 0));

    contentPane.addTab("Test1", new JButton("Test1"));
    contentPane.addTab("Test2", new JButton("Test2"));

    setContentPane(contentPane);
}

Error:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
at javax.swing.plaf.basic.BasicTabbedPaneUI.paintTabArea(Unknown Source)
at javax.swing.plaf.basic.BasicTabbedPaneUI.paint(Unknown Source)
at javax.swing.plaf.metal.MetalTabbedPaneUI.paint(Unknown Source)
at javax.swing.plaf.metal.MetalTabbedPaneUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1200(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

I hope anyone knows what's going on.

Frakcool
  • 10,915
  • 9
  • 50
  • 89
Finn Bon
  • 13
  • 3
  • 2
    Are you starting the GUI on the Swing event thread, using `SwingUtilities.invokeLater(Runnable r)`? – Hovercraft Full Of Eels Mar 31 '17 at 17:18
  • 4
    Welcome to Stack Overflow! Please take the [tour](http://stackoverflow.com/tour), have a look around, and read through the [help center](http://stackoverflow.com/help), in particular [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) and [What topics can I ask about here?](http://stackoverflow.com/help/on-topic). Please provide a [Minimal, Complete, and Verifiable example (MCVE)](http://stackoverflow.com/help/mcve) – Timothy Truckle Mar 31 '17 at 17:20
  • Yes I am. Is it something I shouldn't do? – Finn Bon Mar 31 '17 at 17:21
  • Possible duplicate of [Exception thrown while working with JTabbedPane](http://stackoverflow.com/q/2751502/522444) – Hovercraft Full Of Eels Mar 31 '17 at 17:22
  • 4
    You should start the GUI on the event thread. You'll need to create and post a [mcve], but first check out [these links to similar questions](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=site:stackoverflow.com+java+jtabbedpane+ArrayIndexOutOfBoundsException&*) – Hovercraft Full Of Eels Mar 31 '17 at 17:22
  • I've added more code, I hope this is enough now. I've looked at the similar questions but I believe none of those show any solution to my problem, sadly. – Finn Bon Mar 31 '17 at 17:31
  • 1
    Tip, add @HovercraftFullOfEels or whoever you want to reply (The `@` is important) so they get notified of your reply. Btw 1+ for a nearly complete MCVE – Frakcool Mar 31 '17 at 17:35
  • @Frakcool thanks! :) Also new to this website, haha. – Finn Bon Mar 31 '17 at 17:35

1 Answers1

4

I'm not sure why you're setting a BorderLayout to the contentPane, I recommend you to change the variable name to tabbedPane and remove this line:

tabbedPane.setLayout(new BorderLayout(0, 0));

This fixes the error.

If you want to edit the layout of the tab, then create a JPanel with that layout and pass it as parameter instead of your JButtons.

Another thing to note is not calling setBounds but pack() and setLocation() or setLocationRelativeTo or setLocationByPlatform() methods instead

Now, as an aside note, don't extend JFrame, see The use of multiple JFrames, Good / Bad practice? (The general consensus says it's a bad practice). Also see Extends JFrame vs creating it inside of class

Community
  • 1
  • 1
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • That did indeed fix it, thank you! I'm quite new to Swing and GUI in Java, so I didn't know that could mess it up. – Finn Bon Mar 31 '17 at 17:33
  • @FinnBon: indeed, it totally prevents the JTabbedPane from correctly layout out tabs as it uses its own layout manager for this. But more, thinking logically, why would you want to do this? In the future please post a valid [mcve] with your question, this means that we can copy and paste it and run it without modification. We can't with your code since you've not shown a valid class or imports, the key being to make it as easy as possible for us to answer the question. (1+ to this answer) – Hovercraft Full Of Eels Mar 31 '17 at 18:15