954,141 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

adding a JFrame component to JTabbedPane

Hello folks,

I am trying to add a JFrame object into JTabbedPane, but I am getting a java.lang.IllegalArgumentException. Here's my code snipt.

// other codes...
JTabbedPane tabs = new JTabbedPane();
tabs.addTab("Flight Information",flightInfoPane);  <-------------- line 28
// other codes...
private FlightInfoPane flightInfoPane = new FlightInfoPane();

Here I have flightInfoPane class's declaration...

class FlightInfoPane extends JFrame {
        public FlightInfoPane () {
        }
 // rest of the class ....
}


This is the error that I am getting. I am not sure if this operation is allowed.

F:\DCTM_WORK>java FlightReservationDesktop.Reservation
Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
        at java.awt.Container.addImpl(Unknown Source)
        at javax.swing.JTabbedPane.insertTab(Unknown Source)
        at javax.swing.JTabbedPane.addTab(Unknown Source)
        at FlightReservationDesktop.Reservation.<init>(Reservation.java:28)
        at FlightReservationDesktop.Reservation.main(Reservation.java:35)


please advise...


Thanks.

new_2_java
Junior Poster
127 posts since Apr 2007
Reputation Points: 7
Solved Threads: 6
 

A JFrame is a top-level component. It can not be "added" to another. Use a JPanel.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

I agree with Masijade, and I think you could probably just change the JFrame inheritance to JPanel, well depending on your actual coding hehe Anyway, what I wanted to recommend is NetBeans. It’s a very innovate IDE when one is dealing with GUI applications :icon_cool: You’d just create a new Java project and create a new GUI form; then you can drag and drop components just like in Visual Basic, but since it's Java it's better :icon_mrgreen: You certainly don’t have to code within NetBeans; just copy the automatically generated GUI code to the IDE that you’re using.

PoovenM
Junior Poster
151 posts since Aug 2006
Reputation Points: 56
Solved Threads: 11
 

you can not add jrame into jtabbedpane...but you can add jtabbedpane into jframe.
create jpanels and add them into addTab().

test.tracker
Newbie Poster
1 post since Dec 2011
Reputation Points: 10
Solved Threads: 0
Infraction Points: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You