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 calculate between two String method in java
Robolectric with existing android maven project
While Tomcat localhost is running, other War file is not running due to ports already in use
Exception in thread “main” java.lang.NullPointerException . nothing seems to work [duplicate]
Playing ismv file type on native Android player
Writing a calculator by using a method int sum(String s)?
Log4j2: NoClassDefFoundError: Static Nested Class
Why does the code execute the second line when the first if statement is true?
NullPointerException on Edittext
Java: Decrypting an AESEncrypted file does not work
Reading in a file and processing data
Nexus 7 issues with Eclipse
Why jvm generates serialVersionUID?
Java : Writing CSV in String format to CSV in a file
It gives me an error at GPS [closed]
Get maven dependency from child POM

Categories

HOME
clips
gremlin
tizen
smarty
hashmap
webstorm
programming-languages
sharepoint-designer
sql-server-2016
disassembler
synchronization
communication
user-input
fallback
worldwind
amazonsellercentral
kvc
kudan
mmap
user-interaction
react-css-modules
wtx
atlassian-crucible
gsoap
nouislider
oracle-fusion-middleware
devextreme
main
dosbox
xcode-extension
web-mining
multilingual
starteam
android-fingerprint-api
gulp-sourcemaps
core-plot
apple-news
smartcontracts
youcompleteme
zip4j
python-webbrowser
datastax-startup
pebble-watch
rdfs
tactic
font-size
akka-cluster
imanage
specrun
yt-project
fody
paypal-express
clang-static-analyzer
pagedlist
ubuntu-10.04
hill-climbing
whois
dlna
phpcas
reactive-banana
ruby-2.2
sysinternals
pretty-print
javax.mail
website-monitoring
fpml
applescript-objc
googlemock
elliptic-curve
quantlib-swig
quartz-graphics
jplaton
ora-00911
buster.js
gridcontrol
padarn
pvrtc
unc
bubble-chart
pydot
genshi
infobox
psi
oncheckedchanged
xfbml
nhibernate.search
exchange-server-2003
adrotator
project-hosting
zend-decorators
mdac
paul-graham

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