Reputation: 822
I have 2 Frames that represent a bargraph. The valueFrame is input textboxes for integers. The barFrame is the bargraphs corresponding to the valueFrame.
I want to take these 2 seperate frames and put them into 1 JPanel side by side. So when the program runs, I see 1 JPanel with 2 frames inside. When I try to add the frames, I get an IllegalArgumentException. Can anyone show me how I could do this correctly?
public static void main(String[] args) {
// TODO code application logic here
ArrayList<Double> data = new ArrayList<Double>();
data.add(new Double(33.0));
data.add(new Double(44.0));
data.add(new Double(22.0));
data.add(new Double(22.0));
Model model = new Model(data);
View1 valueFrame = new View1(model);
View2 barFrame = new View2(model);
model.attach(barFrame);
JPanel mainPanel = new JPanel();
mainPanel.setSize(600,400);
mainPanel.setLayout(new BorderLayout());
mainPanel.add(valueFrame,BorderLayout.WEST);//IllegalArgumentException
mainPanel.add(barFrame,BorderLayout.EAST);//IllegalArgumentException
mainPanel.setVisible(true);
}
}
Exception in thread "main" java.lang.IllegalArgumentException: adding a window to a container
at java.awt.Container.checkNotAWindow(Container.java:488)
at java.awt.Container.addImpl(Container.java:1089)
at java.awt.Container.add(Container.java:971)
at Ales6_7.Ales6_7.main(Ales6_7.java:43)
BUILD SUCCESSFUL (total time: 4 seconds)
Upvotes: 1
Views: 204
Reputation: 391
If you want two frames side-by-side add two layouts to your JFrame
(Looks like your JFrame is already using BorderLayout
so technically you can add these two new layouts to your JFrame
BorderLayout
on respective east and west if you want to get the look you are currently trying to achieve.)
More about layouts http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Upvotes: 1
Reputation: 14413
You can't add a JFrame
to a JPanel
message is self-explanatory what you can do is take a look to JInternalFrame
.
With the JInternalFrame class you can display a JFrame-like window within another window. Usually, you add internal frames to a desktop pane. The desktop pane, in turn, might be used as the content pane of a JFrame. The desktop pane is an instance of JDesktopPane, which is a subclass of JLayeredPane that has added API for managing multiple overlapping internal frames.
Read more: How to use InternalFrames
Upvotes: 3