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.
Related
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.
I've created a JPanel and JScrollPane like so:
JPanel panel = new JPanel();
panel.setPreferredSize(5000, 5000);
JScrollPane scrollPane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPanel.getViewport().setBackground(Color.GRAY); //panel is white
I will be using event handlers to dynamically resize the panel. The problem is, sometimes the panel is smaller than the viewport. I'd set the panel's size like this:
panel.setPreferredSize(10, 10); //Just an example
but the panel will never be smaller than the viewport. I tried using
panel.setSize(10, 10);
and i saw a gray flicker (the viewport's background showing through), which indicated that the panel was being sized to what i wanted it to be, but then grew to the viewport's size. How can i stop this?
To control the sizing the view inside a JViewport, let it implement the Scrollable interface. Without, it's always forced to the size of the viewport.
Is there any way to make a JSlider to fit the width of a JPanel when it resizes? I'm now using. JFrame's pack() method and when I resize the panel in which the slider is contained, the slider doesn't change its size.
Thanks in advance!
Actual sizing behaviour is controlled by the LayoutManager used in the panel. The default is FlowLayout which keeps the children at their preferredSize. Change to a LayoutManager which sizes them bigger/smaller than their pref, for a single child f.i. the center of a BorderLayout:
JPanel panel = new JPanel(new BorderLayout());
panel.add(mySlider)
There are others, read the chapter about LayoutManagers in snoracle's online Swing tutorial
I have a simple swing application which consists of a JLabel and three buttons. The three buttons are in their own JPanel which is in a JFrame along with the JLabel. The JPanel uses flowlayout manager to arrange the buttons horizontally and the JFrame uses the BorderLayout manager to arrange the JLabel and JPanel vertically.
My problem is when I launch the application, during the course of use the text on one of the buttons changes which increases its width. However, the window doesn't resize to accomdate this and one of the buttons disappears. I thought about calling pack() again, but the JFrame is a local variable in my constructor, also, I shouldn't have to tell my program to resize, right? I haven't been able to find anything on google or here to help me but there must be a simple solution, what am I missing? Code is below.
playButton = new JButton("Play");
pauseButton = new JButton("Pause");
stopButton = new JButton("Stop");
curTrackLabel = new JLabel("No Track Selected");
JFrame myFrame = new JFrame("MediaPlayer");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setTitle("MediaPlayer");
myFrame.setLocation(400,300);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
myFrame.add(topPanel);
JPanel buttonPanel = new JPanel(new FlowLayout());
buttonPanel.add(playButton);
buttonPanel.add(pauseButton);
buttonPanel.add(stopButton);
topPanel.add(buttonPanel, BorderLayout.CENTER);
topPanel.add(curTrackLabel, BorderLayout.NORTH);
playButton.addActionListener(new playButtonHandler());
pauseButton.addActionListener(new pauseButtonHandler());
stopButton.addActionListener(new stopButtonHandler());
myFrame.pack();
myFrame.setVisible(true);
Maybe try
((JFrame)myButton.getTopLevelAncestor()).pack();
Where myButton is the button whose text is modified during execution.
As with learning any GUI software, experimentation is best. Try messing with BorderLayouts with nested JPanels.
Ultimately, you use JPanel with a BorderLayout (Flow Layout is OK but really when resizing the window, it epically fails). See http://download.oracle.com/javase/tutorial/uiswing/layout/border.html to learn more about BorderLayouts.
Now for your layout scheme it should be something along the lines of:
Top Level Container: JFrame
JFrame contains a JPanel (Call this
JPanel 1) with a BorderLayout.
The three buttons should be in a
SEPARATE jPanel (JPanel 2). JPanel
1 should add the three buttons as
BorderLayout.CENTER. In this way,
the window will resize if the button
changes its width and/or hright.
The JLabel should be added as
BorderLayout.LINE_START.
The tutorial at: http://download.oracle.com/javase/tutorial/uiswing/layout/border.html should help you with this. But in general, use the following:
Use JPanel and nest JPanels as necessary
BorderLayout.CENTER will accomodate size changes---this is the key! (Experiment with this)
JFrame should only be used as a top level container (for more complex GUIs, this is true).
If you require more flexibility, check out JGoodies: http://www.jgoodies.com/ . This is more along the lines of creating forms.
I have been using the printComponent that was shown in another question. It works great for printing a JPanel if I first add that JPanel to a JFrame and draw that JFrame to screen. But if I don't do that before I print, I get a blank page printed. Why is this?
I've used code like the following to create a BufferedImage on a panel that is not visible on the frame:
JPanel panel = new JPanel();
... // add components
panel.setSize(300, 300);
panel.doLayout();
that is because the panel you wish to draw has an initial size of 0,0. Once added to a container with a layout manager and is displayed, then it gets its "normal" size.