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

how to retrieve image from mysql database using java servlet and show it in HTML img tag? [closed]
Parsing RSS XML feed into a listview
Create new table entry with new id
Weird Error with JSP
IntelliJ IDEA 12 - Android GUI designer doesn't successfully change app theme
Using user-inputted characters in If/Switch statements
Displaying a series of pictures (JPanels with images) in a horizontal sequence
Making A Copy of an Array List without it looping
Can't run webcam in Eclipse
Maven onejar plugin, get a properties file
How to prefer reads on secondaries in MongoDb
insert contact optimization
DES algorithm java code using string as input [closed]
Instantiate an object of a class where the class is given via generics [duplicate]
Neglect all escape characters and code JAVA
Swing Runnable with parameters

Categories

HOME
pdf
urbancode
tesseract
rsync
analysis
spring-jdbc
leon
youtube-api-v3
jgroups
umd
alpha
cross-browser
echarts
pheatmap
session-variables
reveal.js
visual-studio-cordova
zebra-printers
jsprit
netflix
workload-scheduler
tostring
pugjs
solaris-10
sms-gateway
excel-vba-mac
google-pagespeed
scriptcs
wijmo
javacv
tdd
cas
filezilla
assistant
facebook-apps
large-file-upload
tasklet
google-rich-snippets
airconsole
column-family
pim
environment-modules
hexo
qwerty
swisscomdev
c11
hilbert-curve
email-templates
g-code
core-plot
netcdf4
auto-update
parentheses
youcompleteme
python-c-api
jlink
integrity
np-complete
r-forge
qtwebview
spring-cache
eventkit
nested-sets
fody
markojs
pyke
jfugue
xna-4.0
eclipse-clp
mutation-observers
django-unittest
varargs
browser-bugs
ideamart
sailfish-os
processmodel
iiviewdeckcontroller
oberon
document-classification
bitcoinj
sgen
jquery-layout
xceed-datagrid
java.nio.file
stxxl
pyhdf
quartz-graphics
jquery-mobile-dialog
plasma
jelly
wsdl-2.0
free-variable
quickdialog
subscript
gnu-prolog
gnustep
nsobject
table-footer
winbugs14
pyinotify
fluent-interface
createwindow
icanhaz.js
firefox-5
virtual-functions
phonon
sector
multiple-languages
3gp

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