JSlider fitting the width of a JPanel - java

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

Related

Fill JPanel only to the height of inner elements

I'm trying to achieve a design whereby one of my JPanel's heights is only set to the height of it's inner components. Currently it's laid out like so:
JPanel (BoxLayout)
JPanel (CardLayout)
JPanel (BoxLayout)
JPanel (BoxLayout)
JPanel (FlowLayout)
It currently displays as so, I want the top bit to expand the both on the x and y axis pushing the "Your Name" to above the status bar.
I need to use BoxLayouts it seems so that it flows from top to bottom.
Please help!
I want the top bit to expand the both on the x and y axis pushing the "Your Name" to above the status bar.
Then is sounds like you can use a BorderLayout as the top level panel, instead of the BoxLayout.
Since the default layout manager for the content pane of the frame is a BorderLayout you can just add the panels directly to the content pane of the frame:
frame.add(cardLayoutPanel, BorderLayout.CENTER);
frame.add(flowLayoutPanel, BorderLayout.PAGE_END);
When you add a panel to the "PAGE_END", its preferred height is respected.
When you add a panel to the "CENTER" it gets all the extra spaces available in the frame.
Read the Swing tutorial on Layout Managers for more information and working examples.
I need to use BoxLayouts it seems so that it flows from top to bottom.
You don't need to use a BoxLayout (as demonstrated above), but you can still use it if you want. BoxLayout respects the maximum size of a panel. So you could override the getPreferredSize() method of your panel using the flow layout to return the preferred height of the panel.
#Override
public Dimension getMaximumSize()
{
Dimension preferred = super.getPreferredSize();
Dimension maximum = super.getMaximumSize();
maximum.height = preferred.height;
return maximum;
}
Now the height of the flow panel will be respected and all the extra space will go to the other panel.

JPanel Format Problems

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.

Set constant size of JScrollPane

I have a JTabbedPane with a Border Layout.
Here's the code I'm using to add the components:
add(columnNames, BorderLayout.NORTH);
add(scroll, BorderLayout.CENTER);
add(useCtrl, BorderLayout.SOUTH);
setVisible(true);
Question:
Notice the excess whitespace to the right inside the JScrollPane. I don't want that there. I would like for the JScrollPane not to change size at all when changing the size of the JFrame. I have tried setSize() and setPreferredSize(), but the size of the JScrollPane always changes. I've tried using GridLayout, but I get the same result.
Place the JScrollPane in a JPanel with another layout. (e.g. BoxLayout or GridBagLayout). And add the JPanel to the center.
The size of a graphics object is controlled by the layout manager. The BorderLayout will always expand the CENTER object to take up all available space. GridLayout expands all it's children proportionally. If you try a GridBagLayout and set the weightx to 0, that will prevent expansion horizontally.
There are a lot of layout managers available, browse the API for more choices and experiment until you find the resizing behavior you want. Each has a fairly good explanation of how it works in the javadoc.

Resize JPanel within JScrollPane - never smaller than viewport

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.

Java Swing Panel Size

Hi I have been learning Java Swing for creating a chess game to practice my Java programming skills.
I've added a JPanel to the east of the JFrame with BorderLayout and I've used the setPrefferedSize(new Dimension(x,y)) method to set the width and height.
After that I have created 4 JPanel and added them with BoxLayout on the previously created panel.
I have tried to set the size of the 4 panels with the setSize(x,y) and setPreferredSize(new Dimension(x,y)) but it dosent work the 4 panels automaticly changed there size to fit the main JPanel and after adding a JLabel on one of them the size of it increased automaticly .
This is my code:
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
JPanel a = new JPanel();
a.setPreferredSize(new Dimension(50, 50)); //this dosent work
a.add(min);
a.setBackground(Color.red);
this.add;
JPanel b = new JPanel();
b.setBackground(Color.blue);
this.add(b);
JPanel c = new JPanel();
this.add(c);
JPanel d = new JPanel();
d.setBackground(Color.black);
this.add(d);
How can I change the size of each of these panels?
BoxLayout is best for laying out components with varying sizes along a single axis. From the Javadocs:
"BoxLayout attempts to arrange components at their preferred widths (for horizontal layout) or heights (for vertical layout)."
The idea is that they may have different heights (for a horizontal layout) and it will take the maximum height. And, they definitely may have different widths. Also, BoxLayout works with some, er, "interesting" filler pieces like Box.createHorizontalGlue(). These are actually quite useful for flexible, resizeable layouts once you get the hang of it. But, all in all, BoxLayout is for flexible, resizable layout of items with differing sizes.
For simpler cases, especially if you want both preferred width and preferred height to be "respected", use GridLayout as everybody else has suggested.

Categories

Resources