Could you help me understand what is going on here. I consulted Javadoc: JFrame has setLayout method. So, what sharing error springs out is a mystery to me.
public class View extends JFrame {
public View(){
// LayoutManager for the whole frame.
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
}
}
Result
Exception in thread "main" java.awt.AWTError: BoxLayout can't be shared
at javax.swing.BoxLayout.checkContainer(BoxLayout.java:465)
at javax.swing.BoxLayout.invalidateLayout(BoxLayout.java:249)
at java.awt.Container.invalidate(Container.java:1583)
at java.awt.Component.invalidateIfValid(Component.java:2957)
at java.awt.Container.setLayout(Container.java:1484)
at javax.swing.JFrame.setLayout(JFrame.java:605)
at View.<init>(View.java:16)
at Init.main(Init.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Try this one on JFrame#getContentPane()
this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.X_AXIS));
Read more How to Use BoxLayout
All the components are added in JFrame's content pane.
Read more Adding Components to the Content Pane
Here is the pictorial representation how JFrame looks like
EDIT
From comments:
Well, not clear anyway. I analyze it like this: BoxLayout class needs to know it target. JFrame has setLayoutt method and needs to know its layout.
this.setLayout(manager) internally calls getContentPane().setLayout(manager);
The below line
this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
is converted to below line that is not correct.
this.getContentPane().setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
For more detail have a look at Source code
Related
Im' working on a computer science project, and I'm having some issues with borderlayout:
that is my configuration (simplified)
public class MainPanel extends JPanel{
JSplitPane Pcenter;
JPanel PEnd;
public MainPanel(Container dad){
JTable myTable=new JTable(), anotherTable=new JTable();
JSplitPane left=new JSplitPane();
left.setTopComponent(new JScrollPane (myTable));
left.setBottomComponeny(new JScrollPane(anotherTable));
left.setOrientation(JSplitPane.VERTICAL_SPLIT);
this.setLayout(new BorderLayout());
Pcenter=new JSplitPane();
PEnd=new JPanel();
pend.add(new JButton("Store"));
Pcenter.setLeftComponent(left);
Pcenter.setRightComponent(new JScrollpane(new JLabel("right")));
this.add(Pcenter,BorderLayout.CENTER);
this.add(PPnd,BorderLayout.PAGE_END);
//EDIT:
PEnd.add(new JLabel("goofy"));
}
}
Now, my project is different, but this is my configuration
as I run the main (a jframe whith a JTabbed with this attached as a tab) it shows me only the the center and not the end one. But if I attach PEnd at the beginning it's sown as it have to.
EDIT 14-3-14
I dug into my code and I've seen that te problem is generated by the myTable's scrollpane so removing it will make PEnd shown but mytable represents itself in an horrible way
PEnd, the panel you are adding at the PAGE_END has nothing added to it so there is nothing to show, and has size 0.
Maybe with the line
pend.add(new JButton("Store"));
you meant
PEnd.add(new JButton("Store"));
As a side note, your variable names should start in lower case to follow standard Java style, to differenciate them from classes.
I created frame with button and when it is pressed all content is removed and replaced by new one. But I can not display label, here is my code:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
getContentPane().removeAll();
jLabel2 = new javax.swing.JLabel();
jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("Hello World!");
jLabel2.setLocation(80, 80);
jLabel2.setVisible(true);
getContentPane().add(jLabel2);
getContentPane().repaint();
pack();
}
What am I doing wrong? :(
instead of repaint() try validate().
Instead of trying to remove all and add new components, use a CardLayout, which will "layer" panels and let you navigate between them. See How to use CardLayout and you can see a simple example here
You can also see how to use CardLayout with Netbeans GUI Builder here
You should use validate() instead of repaint. The rest of your source looks fine.
The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.
When I run the code it just opens an empty window
I also important whatever is necessary
relevant parts of the code:
public class Game extends JFrame implements ActionListener,KeyListener{
private JLabel background;
....
public Game(){
background=new JLabel(new ImageIcon("/graphics/board.gif"));
...
this.add(background);
this.setSize(800,600);
this.setVisible(true);...
I tried adding the JLabel to a JPanel and then add it to the frame but it still shows nothing in the window
Originally the code was:
JLabel background = new JLabel("/graphics/board.gif");
This would not set the image at the path described, Suggest that the following method is used (this could be simplified to just use a different JLabel constructor but steps shown for clarity)
Create and load the image and then set the icon for the Label As follows
ImageIcon icon = new ImageIcon("/graphics/board.gif");
JLabel background = new JLabel();
background.setIcon(icon);
Link to ImageIcon Java Doc
It is important to set in the layout the order in which the elements are displayed , maybe you have something that is displayed over the label..
I'm guessing you have a directory structure something like:
-c:\java
- source (for source and class files)
- graphic (for your images)
background=new JLabel(new ImageIcon("/graphics/board.gif"));
Don't specify the leading "/" in the file name. That tells Java to look at the root of the C drive, not at the directory where your class is executing from.
Also, don't use:
this.setSize(800,600);
The image does not stretch to fill the size of the frame. Intead you should be using:
this.pack();
so the frame will be the size of the image.
I am trying to create a child frame to exist inside my applet and it should be bound to a JPanel. I found this and that on the internet but nothing that worked. I think something went wrong during the process and the darn thing is hidden or something. Can someone please give me some help on this issue.
My source code follows...
public class EnableFrame {
public void init() {
EnableFrame theframe = new EnableFrame();
theframe.setSize(550, 300);
theframe.setVisible(true);
}
public EnableFrame() {
JPanel containall = new JPanel();
JInternalFrame iframe = new JInternalFrame("New Frame",true,true);
iframe.setBounds(10,10,150,150);
iframe.getContentPane().add(containall);
iframe.show(true);
}
}
Thanks in advance
-Roland
A JInternal is normally associated with a JDesktopPane.
I order for the internal frame to appear on the screen, you must have added the frame to an appropriate container, such as a JDesktopPane
You may find How to Use Internal Frames of some use.
my view only the comment
even is possible there could be caused with some side_effect for mouse and focus event betweens heavyweight (J)Applet and lightweight JInternalFrames that complicated this idea, and heavyweight (J)Applet can jumping toFront()
you'd don't do that and to use JDesktopPane from JFrame rather than for (J)Applet
Any work-arounds?
myContainer.getContentPane().addContainerListener(new ContainerListener(){
#Override public void componentAdded(ContainerEvent e) {
System.out.println(e.getChild().getClass().getName().toString());
}});
myContainer.add(new JPanel());
myContainer.setJMenuBar(new JMenuBar());
The JMenuBar is not part of the content pane.
See the section from the Swing tutorial on Using Top Level Containers for information about the structure of the frame and its components.
The actual code in the setJMenuBar() method is:
getRootPane().setMenuBar(menubar);
So I would guess the ContainerListener would need to be added to the root pane.
Edit:
From the tutorial link you can see that the frame structure looks like:
root pane
layered pane
menu bar
content pane
from the code I posted it looks like the menubar is added to the root pane which is why I suggested adding the container listener to the root pane. However it appears you need to add the ContainerListener to the layered pane:
getLayeredPane().addContainerListener(new ContainerAdapter()...