Im trying to insert a JLayeredPane to JPanel. When I insert the JLayeredPane to the frame, the components show and everything is okay, but when I insert the JLayeredPane to the JPanel and then insert the JPanel to the frame It doesn't display the JLayeredPane.
My code:
button1.setBounds(0, 0, 100, 100);
button2.setBounds(50, 50, 100, 100);
layer.add(button1,1);
layer.add(button2,0);
panel.add(layer);
this.add(panel);
this.setVisible(true);
I insert the JLayeredPane to the JPanel and then insert the JPanel to the frame It doesn't display the JLayeredPane.
A JPanel uses a FlowLayout which respects the preferred size of any component added to it.
A JLayeredPane does not use a layout manager and therefore does not have a preferred size so it is not displayed on the panel.
the components show and everything is okay,
The frame which uses a BorderLayout. The default location is the "CENTER" and the center will size the layered pane based on the space available in the frame.
There is no need for the extra overhead of adding the layered pane to the panel and then the panel to the frame.
Read the section from the Swing tutorial on How to Use Layered Panes for more information and working examples.
Related
I want to set the size of the JTextPane according to the size of the panel so that when i add other panels, it changes accordingly. But it just gives a small text pane in the center and when i add some text, it's size changes accordingly.
JPanel panel = new JPanel();
JTextPane txt = new JTextPane();
JScrollPane pane = new JScrollPane();
pane.add(txt);
panel.add(pane,BorderLayout.CENTER);
add(pane);
now the jtextpane just appears at the center of the screen like a small box. I want it to appear according to the size of the panel
JPanel uses FlowLayout by default which sizes components according to their preferred sizes. You can use BorderLayout which will use the maximum area possible.
Also using constraints such as BorderLayout.CENTER has no effect unless the container is actually using BorderLayout. Dont add components to the JScrollPane. This will replaces all components within the view of the component. Instead set the JTextPane as the ViewPortView, for example
JPanel panel = new JPanel(new BorderLayout());
JTextPane txt = new JTextPane();
JScrollPane pane = new JScrollPane(txt);
// pane.add(txt); remove
panel.add(pane, BorderLayout.CENTER);
Read:
How to Use BorderLayout
How to Use Scroll Panes
You added pane twice. Add panel to your base (a JFrame?) instead and remember to actually set your JPanel to use BorderLayout.
I have a 2 JPanels, 1 a button Panel and one a Graphic Panel. I would like the button panel to situated right below the graphic panel but the button panel cuts off the Graphics Panel in the middle. I've been trying the box layout which seems from discussions seems like the best format for what I am trying to do. Can anyone please give me some advice on my formatting problem.
JFrame canvas = new JFrame("Baseball Strike K");
JFrame canvas = new JFrame ("GraphicBoard");
canvas.setVisible(true);
canvas.setSize(1000,1000);
canvas.setDefaultCloseOperation(EXIT_ON_CLOSE);
//create two panels
//add them to contentPane
//set Layout
JPanel buttonPanel = createButtons();
JPanel mainPanel = new Graphic(); //extends JPanel and writes the paint method
mainPanel.setSize(1000, 1000);
Container content = canvas.getContentPane();
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
content.add(mainPanel);
content.add(buttonPanel);
mainPanel.setSize(1000, 1000);
The job of the layout manager is to determine the size of the component, so you would never invoke the setSize() method of a components.
Instead you give hints to the layout manager on what the size should be. You would do this by overriding the getPreferredSize() method to return an appropriate value. Also, I would pick a more reasonable size (1000, 1000) is a little big to display on most screens. If you really want your painting area this large then I would add the paint panel to a JScrollPane and then add the scrollpane to the frame.
Try getting your code to work using a BoxLayout. Then I would suggest a better layout manager would be to use a BorderLayout. Then you add the paint panel to the CENTER and the buttons to the SOUTH. Now as you resize the frame the paint panel will be adjusted in size.
canvas.setVisible(true);
Also, the placement of that line of code is wrong. You should add all your components to the frame first, before making the frame visible.
I have a problem.
I have a JFrame with some JTextFields, JLabels, Jlists & JButtons now the contents of my frame is more than the screen area so I want to attach a JScrollBar to my JFrame but my srollbar does not work. So, can anyone please guide me on how I can scroll my JFrame using the JScrollbar?
Put all the components in one panel (instead of in the JFrame)
Add this panel to a JScrollPane
Add the JScrollPane to your frame.
I should be something like:
JPanel container = new JPanel();
container.add(panel1);
container.add(Panel2);
JScrollPane jsp = new JScrollPane(container);
frame.add(jsp);
It depends on the layout you are using with your JFrame. If you want to add working scrollbar to your panel you should look at the JScrollPane class and the Scrollable interface which must be implemented by scrollable components.
The How to use scroll panes chapter in the swing tutorial may also be an interesting reading: http://download.oracle.com/javase/tutorial/uiswing/components/scrollpane.html
I am failing to display a JComponent inside a JPanel on a JFrame.
The following does not work.
JComponent component = ...
panel.add(component, BorderLayout.CENTER);
frame.add(panel, BorderLayout.CENTER);
But if I add the JComponent to the JFrame[like frame.add(component, BorderLayout.CENTER);], it displays the contents.
Any ideas
A JPanel's default layout is a FlowLayout so you don't have to specify the center of the panel.
Simply do:
panel.add(component);
Alternately, do:
panel.setLayout(new BorderLayout());
panel.add(component, BorderLayout.CENTER);
By default a JComponent doesn't have a preferred size.
By default a JPanel uses a FlowLayout. When you add a component to the panel it will respect the preferred size of the component, which is 0, so you don't see it.
By default the panel which is used as the content pane of a JFrame uses a BorderLayout. So when you add a component to the content pane of the frame the component is automatically resized to fill the space available to the frame,
The solution is to give your component a preferred size, then it can be used on any panel with any layout manager.
I have a JFrame with a JPanel on it.
I want to add another JPanel which is a preconfigured component onto the Jpanel inside my JFrame.
If I do this:
subPanel.setLayout(new BorderLayout());
subPanel.add(preconfiguredPanel,BorderLayout.CENTER);
my Panel will show.
If I do this:
subPanel.add(preconfiguredPanel);
my JPanel will not show. The documentation says when using add(Component) it will use the default Layout FlowLayout. Ok fine, but why won't my component display inside that JPanel when using the default FlowLayout???
Probably because your panel doesn't have a preferred size.
When you add a panel to a BorderLayout the default is to place it in the center, so the panel will automatically be resized to the size of the frame.
When you add the panel to a FlowLayout, the flow layout repsects the size of the panel.
If you need more help then post your SSCCE.