Using BoxLayout without container target? - java

I'm trying to use BoxLayout instead of my GridLayout code:
setLayout (new GridLayout (5, 2));
except I noticed that when you do BoxLayout you need to do something like:
setLayout (new BoxLayout(container, BoxLayout.Y_AXIS));
However, I don't call the JFrame something, like how it would be called:
JFrame label = new JFrame ();
it basically just works in the constructor.. I'm obviously a beginner in Java but do have some understanding. Because I don't call the JFrame and simply write:
add (controlPanel);
add (outputPanel);
What do I put into the target part of the BoxLayout? I do want it to be on the Y axis but I'm not sure what to put in the field before it instead of the "container" thanks :)

Simply use JFrame's content pane as target.
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

Related

JPanel button is not at the correct place

i made a custom JFrame for the desktop application and i added a JPanel on the very top of the app to serve as a subtitute of the title box. the problem is when i added a button it located right in the middle of the JPanel instead of the usual left top. AND it would not move even if i set it at a different location.
here is the code:
JFrame f = new JFrame("Hello");
f.setResizable(true);
JPanel pa = new JPanel();
JButton btn = new JButton("Exit");
btn.setBackground(Color.white);
btn.setText("Button");
btn.setSize(300, 80);
btn.setLocation(50, 0);
pa.setBackground(Color.red);
pa.setPreferredSize(new Dimension(width,60));
pa.add(btn);
f.setBackground(Color.white);
f.setUndecorated(true);
f.getContentPane().add(pa, BorderLayout.NORTH);
f.setSize(new Dimension(width,height));
f.setLocation(200, 200);
f.setVisible(true);
You use a BorderLayout in the frame. You can do the same thing in the panel.
pa.setLayout(new BorderLayout());
pa.add(btn, BorderLayout.WEST);
In general, setLocation tends to fight against the layout manager, so you usually don't want to use it unless you're going to position everything by hand.
that is one way to do it, but BorderLayout way is not very good way because i also want to add another button next to it.
Then what this might need is a FlowLayout using FlowLayout.LEADING as the alignment.
But as general tips:
Provide ASCII art or a simple drawing of the intended layout of the GUI (showing all components) at minimum size, and if resizable, with more width and height - to show how the extra space should be used.
For better help sooner, post a
Minimal, Complete, and Verifiable example or Short, Self Contained, Correct Example of your attempt.

JFrame doesn't show the Jpanel's right

I'm using a JFrame with the size of 800x600.
what i'm trying to do is make this:
the black Panel has 2 other panels inside of him with the size of 300x300 each.
the result is that the black panel is to the left (as suposed) and the red panel in in the centre with a gap on top between the frame and the panel. also, if i remove the black panel the right panel is filling the whole frame...
this is the code:
//create the left part of the screen
JPanel leftPanels = new JPanel();
leftPanels.setLayout(new GridLayout(2,1));
leftPanels.setSize(new Dimension(300,600));
// just to illustrate the 2 panels inside of the black panel.
//leftPanels.add(new JPanel());
//leftPanels.add(new JPanel());
//create the right part
JPanel rightPanel = new JPanel();
rightPanel.setSize(new Dimension(500,600));
rightPanel.setBackground(Color.red);
this.add(leftPanels);
this.add(rightPanel);
this.validate();
this.repaint();
is there an easy way to fix this?
I also tried a Gridlayout on the JFrame but that gives me 2 panels of 400X600 each
First, use FlowLayout like this
setLayout(new FlowLayout(FlowLayout.LEFT));
Secondly, kindly use setPreferedSize rather than setSize for the JPanels
leftPanels.setPreferredSize(new Dimension(300,600));
I don't know what is cashRegister, but it looks like you are not adding the rightPanel to JFrame so make sure you add it.
Try to set the layout of the frame to null. Then use setBounds to position the panel.
If you are trying to set the panel relatively one from another set the frame layout to null
this.getContentPane().setLayout(null);
Then you will be able to place them absolutely.
For more info : Doing Without a Layout Manager (Absolute Positioning)

How to stop resizing of JPanel in JFrame

I have reminder class extending JPanel, with boxlayout and
setPreferredSize(new Dimension(500,500))
In the mainclass I have JFrame
rem = new Reminder();
frame.setSize(900,900);
rem.setMaximumSize(new Dimension(500,500));
frame.getContentPane().removeAll();
frame.setLayout(new BorderLayout());
frame.add(rem,BorderLayout.CENTRE);
frame.validate();
frame.repaint();
And I want the reminder panel in the centre of the jframe without changing its size(500,500) and frame should not change it size(900,900). When I run the above code, rem panel is totally expanded. How to make the rem panel in centre?
Easy way: use AbsoluteLayout and position the "rem" exactly where you want with a graphical editor.
Then set invariable size for frame:
frame.setResizable(false);
Do not use a BorderLayout. Why do you use a BorderLayout if it is exactly what you don't want to have?
Edit: BoxLayout is one choice, if you really need to position everything absolutely yourself, you can also decide to use no layout manager, e.g. http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html
Also if you wanna use a Borderlayout I'm pretty sure it's a typo in your code:
frame.add(rem,BorderLayout.CENTRE);
should be:
frame.add(rem,BorderLayout.CENTER);
Could it be this small thing that's bothering you?

Why am I not getting a functioning JScrollPane?

I have a JFrame window, and I'd like to add a scrollable JTable towards the middle of it. I have a method, called collectionTableScrollPane() that generates the JScrollPane (and I know this is guaranteed to work).
I then proceed to add it to my mainPanel panel. However, I'd like there to be some forced 30px padding on the left and right of the JScrollPane. Logically, I would create a holding JPanel with a centred FlowLayout, and add Box.createHorizontalStrut(30) either side of the JScrollPane.
JPanel tableHolderPanel = new JPanel(new FlowLayout());
mainPanel.add(tableHolderPanel);
tableHolderPanel.add(Box.createHorizontalStrut(30));
tableHolderPanel.add(collectionTableScrollPane());
tableHolderPanel.add(Box.createHorizontalStrut(30));
However, I'm getting a strange result, where the JScrollPane in the middle of the window (denoted by the arrows) sort of becomes ineffectual.
Does anyone know what the problem is?
Note that the JTable contains four rows, of which only two are visible.
I had some issues in the past when i used a JScrollPane inside a panel with a FlowLayout. The behaviour could be tricky, when the content grow, the horizontal scrollbar may appear or the FlowLayout should add a new line.
In your case, i will replace the FlowLayout by a BorderLayout :
JPanel tableHolderPanel = new JPanel(new BorderLayout());
mainPanel.add(tableHolderPanel);
tableHolderPanel.add(Box.createHorizontalStrut(30), BorderLayout.WEST);
tableHolderPanel.add(collectionTableScrollPane(), BorderLayout.CENTER);
tableHolderPanel.add(Box.createHorizontalStrut(30), BorderLayout.EAST);
As far as I'm aware, Box is suppose to be used with the BoxLayout, this may be causing you some issues. Instead, why not use a EmptyBorder on the tableHolderPane
BoxLayout accepting size that came from JComponents, the same issue with default FlowLayout pre_implemented for JPanel
you have to returns PreferredSize by overrode JPanel nested JScrollPane,
use another LayoutManager, e.g. GridBagLayout or todays MigLayout
use NestedLayout, by using BorderLayout where you put two JLabels (e.i. that returns PreferredSize) to the EAST and WEST area
everything depends if you really to want to create the empty area and if shoud be resiziable or not

Java Simple Question about working with JPanels

Just a quick question here. I have a program in which I need to create a number of JPanels, which will then each contain some objects (usually JLabels).
There are a few operations that I have to do each time i create a new JPanel, and I'm wondering if the order in which I do them has any bearing, or if there is a standard practice.
The operations I do are the following:
Declare the JPanel: JPanel panel = new JPanel(...)
Declare the JLabel: JLabel laberl = new JLabel...
Add the JPanel to some other content pane: frame.getContentPane().add(panel)
Set the bounds of the JPanel: panel.setBounds(...)
Add the JLabel to the JPanel: panel.add(label)
In general order isn't important as long as you add all the components to the panel and the panel is added to the content pane "before" you make the frame visible.
The standard practice is to use a layout manager, so there would be no need to set the bounds of the panel you added to the content pane.
The order doesn't matter. However, after creating and adding everything, you need to call revalidate() on the panel (or pack() on its parent window) so that the layout manager (I presume you're using one!) arranges the components as they should be.
Have a method createPanel() that returns the panel with all its children added.
Panel p = createPanel();
p.setBounds(...); // if you must
frame.getContentPane().add(p);
And then
Panel createPanel() {
Panel p = new Panel();
Label l = new Label("Heading");
p.add(l);
return p;
}
The order of constructing and adding items isn't important, except that when you add children, you should add them in the order you want them in the panel.

Categories