java


JTabbedPane ArrayIndexOutOfBoundsException: 0


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.
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

Related Links

Test a condition on a Map element being iterated in Struts JSP
Selenium Java Chrome: Stale exception
Ant JUnit throws ClassNotFoundException
How to reduce thread count in JAVA for mongoDB
Efficiency of accessing local variable vs field?
How to convert string with $ sing in it to int
How to open a location from executeable jar file
Compare two custom objects in java with nullable fields
Java Web application thread getting stuck for DB operation at java.net.SocketInputStream.socketRead0(Native Method)
Library for parsing an HTML table, considering colspans and rowspans
Applying lock on HashMap
Access denied for user 'root'#'localhost' (using password: YES) IN WAMPSERVER
ProcessBuilder failing when calling a system command where Runtime exec works
Java YTApi LiveChatMessage (v 3)
Dynamically add Edit text on my Image and the Trying to Edit
Not getting scrollbars in JTextPane

Categories

HOME
pdf
bluetooth
netbeans
server
ngrx
comparison
session
elm
react-redux
malloc
rubygems
echarts
datastax-java-driver
mouse
podio
synchronization
gorm
nano-server
spring-kafka
dtrace
jsprit
quartz-scheduler
clearcase-ucm
highlight.js
pc
angular-ui
database-replication
code-review
one-hot-encoding
chromium-embedded
blazemeter
h2db
cas
log4js-node
tinymce-4
bcd
contact-form
google-sites-2016
hybridauth
repo
ghost4j
wallpaper
bower-install
host
hockeyapp
preconditions
email-templates
avro4s
g-code
segment
apple-news
termination
python-c-api
mu
migradoc
pintos
nxt
grails-tomcat-plugin
trash
autorest
blackberry-10
actionbardrawertoggle
ipconfig
tform
ruby-2.2
unity5.2.3
sonarqube5.1.2
zend-route
uitouch
phpthumb
expected-exception
braille
concurrent-collections
fmod
prettify
blending
pysimplesoap
newtonscript
nsmanagedobject
work-stealing
boost-filesystem
mercurial-server
stage
cxxtest
suppress
thunderbird-lightning
microsoft-virtualization
nerddinner

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App