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

Why number of days between 2 dates are not coming up correctly? [duplicate]
Get GPS when Android Intent is Started
Understanding junit.framework.AssertionFailedError in Java/Spring web application
Decrypting byte array from Java in C++ using RSA with java generated keys
Saving Android App Condition
Can I bind value from <option> to object field as integer in JSP?
How can I specify alternative above element if the above is missing?
How to load beans in inherited XML config, before beans in Configuration classes (integration tests in Spring)
How to get the exact touch position of a image?
How to store object inside Iterator<object>?
How can I ensure Spring constructs a bean once
Stream closed when using htpp client
JSON Replacing '.' with
running android studio project with google glass
“Missing return statement” error occurring despite its presence [closed]
Java How to create objects of class without name

Categories

HOME
pandas
testng
pypi
gremlin
homebrew
platform-builder
google-docs
setup-deployment
yarn
leon
packages
serverspec
directx
gnupg
kentor-authservices
zend-framework3
phaser
dosgi
angular-ui
ab-testing
code-review
dbext
jquery-ajaxq
intentfilter
ehcache
zurb-foundation-6
crosstab
numerical-methods
underflow
spring-mybatis
wtx
galsim
data-manipulation
windows-dev-center
http-digest
dartium
jquery-bootgrid
qwerty
x11-forwarding
lto
c11
leading-zero
dism
eigenvalue
sencha-touch-2.3
segment
angular2-meteor
smartcontracts
datastax-startup
togetherjs
r-forge
bind9
quartz-composer
bonobo
dstu2-fhir
transmitfile
google-web-starter-kit
jwplayer7
blackberry-10
skype4py
netmq
separator
cartesian-product
cyclomatic-complexity
simple-framework
security-testing
sailfish-os
ami
system32
key-management
inmobi
python-green
dia
tween
camanjs
tablelayout
fpml
preferences
issuu
tld
valuechangelistener
comexception
jboss-weld
transcoding
openlaszlo
flexmojos
commoncrypto
google-cloud-save
angularjs-controller
mt
cisco-jtapi
broken-links
getmessage
nsmanagedobject
removeclass
vdsp
infobox
servicehost
imac
visitor-statistic
site.master
sortable-tables
filtered-index
thunderbird-lightning
user-friendly
substrings
xetex
privilege
putchar
w3wp
ugc

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