JPanel button is not at the correct place - java

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.

Related

Glue Equivalent in MigLayout

I am using MigLayout and I would like to create an effect like using BoxLayout's "glue" to create an invisible space that will flexibly fill the area between two components. From the tutorial:
I understand that in MigLayout, gaps are used to create spaces between components. I can create a rigid space with "gap 10". The MiG Layout Cheatsheet says that I can append push to a bound size with a gap to "make that gap "greedy" and take any left over space" (e.g. "gap rel:push"), but that doesn't seem to work in the following code that I've tried:
JFrame frame = new JFrame("Test");
JPanel panel = new JPanel(new MigLayout());
JLabel label1 = new JLabel("Testing");
label1.setBorder(BorderFactory.createLineBorder(Color.black)); //Border to clearly show JLabel boundaries
panel.add(label1, "");
JLabel label2 = new JLabel("Testing Once Again");
label2.setBorder(BorderFactory.createLineBorder(Color.black)); //Border to clearly show JLabel boundaries
panel.add(label2, "gap rel:push");
frame.setContentPane(panel);
frame.pack();
frame.setMinimumSize(new Dimension(400, 100));
frame.setPreferredSize(new Dimension(400, 100));
frame.setVisible(true);
Which displays a window like this:
I would expect for the "gap rel:push" parameter to work like horizontal glue in the picture above and thus push the JLabel on the right all the way to the right edge of the window, but it doesn't.
How can I get the same effect as BoxLayout's glue using MigLayout? Am I misusing the "gap" parameter or is there some other way to accomplish it?
Also, I realize that I could use docking in the example given, but I'd like an answer without that because it won't work in a more complex layout I'm creating.
Save yourself some sanity points and enable debug borders on the MiGLayout: new MigLayout("debug").
It's possible that the gap is indeed pushing the components apart, but the layout isn't using all of the available space. This should be obvious if you turn on debug borders. Try making the layout use all available space: new MigLayout("debug, fill").
Instead of placing a gap between your label and the cell edge, you may want to place a gap between the two columns: new MigLayout("debug, fill", "[]rel:push[]"). The difference should become clear when using a multi-row layout. In this case, you may not need the fill constraint: new MigLayout("debug", "[]rel:push[]").
I don't have a compiler handy to run this against, but I hope I've given you something to work with.

Adding a button anywhere in a JPanel

Without using the Swing GUI on Eclipse, I've been struggling with adding a button to a JFrame anywhere in the frame (so no BorderLayout.CENTER). I can't get past:
JPanel panel = new JPanel();
JButton OKButton = new JButton("OK");
OKButton.addActionListener(new MyAction());
panel.add(OKButton,BorderLayout.CENTER);
So would something like this be completely redesigned or is there something I'm missing?
EDIT: By Anywhere (as I'm planning to add more than one button/label to a frame), I meant perhaps a coordinate on the frame. So other than dead center, (example) 75% from the left and 25% down.
You can use different Panels with different LayoutMangers to arrange the GUI as you like.
Have a look here for some common LayoutManagers:
http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html
Otherwise you could use the null Layout (panel.setLayout(null)) and place all components by setting the position. But I would recommend you the LayoutMangers

Two Different Panels in One Frame - Java

I have a question.
I have one main frame, and to the left i have a sidebar with menus.
My question is, is it possible to make another panel within the main frame,
so that if menu1 is clicked, the related contents should be displayed to the second half of the main frame, and when other menus are pressed then obviously the relevant stuff according to what is selected. its a bit hard to explain, sorry. Has anyone got an idea, whether that is possible in Java with Eclipse?
yes this's pretty possible you have look at CardLayout, this LayoutManager could be provide the simple way how to implement switching betweens JPanel in the JFrame
Yes, you can add 2 JPanels to 1 frame.
JFrame frame = new JFrame();
JPanel pane1 = new JPanel();
JPanel pane2 = new JPanel();
frame.add(pane1, BorderLayout.WEST);
frame.add(pane2, BorderLayout.EAST);

How do I add space to a JPanel, so that JScrollPane doesn't sit on top of my components?

I have a JScrollPane and when I load my application the bar is sitting on top of one of my buttons. What I would like to do is add some space to the side of my button so that the scroll bar draws over the space and not my button.
Example code that I tried:
JPanel eButton = new JPanel(new BorderLayout());
JPanel spaceFiller = new JPanel();
spaceFiller.setSize(30, 10);
eButton.add(editButton, BorderLayout.EAST);
eButton.add(spaceFiller, BorderLayout.WEST);
The problem with this code is that it still overwrites my button and no space is added. What is the best way to make sure that JScrollPane doesn't overlap the components in my JFrame?
Thanks
To ensure that the size of the JPanel is respected you should use setPreferredSize() instead of setSize().
In your sample code, didn't you reverse EAST and WEST? Shouldn't it be like:
eButton.add(editButton, BorderLayout.WEST);
eButton.add(spaceFiller, BorderLayout.EAST);
That would make more sense, sicne the scrollbar will appear on the right side (EAST).
Please note that the solution you suggest, even though it may work (after exchanging EAST and WEST) looks more like a hack than a real solution.

Java GUI layout problems

I'm writing a small Java GUI program, and I'm having some issues with Java not laying things out properly. I haven't done much Java GUI code lately, so I'm having trouble seeing where the problem lies.
final JFreeChart chart = createChart(dataset);
final ChartPanel chartPanel = new ChartPanel(chart, false);
chartPanel.setPreferredSize(new Dimension(500, 270));
JPanel buttonPanel = new JPanel();
buttonPanel.setPreferredSize(new Dimension(500,50));
JButton toggleButton = new JButton("Toggle");
final JTextField minRange = new JTextField("10");
final JTextField maxRange = new JTextField("1000");
JButton setLimits = new JButton("Set Limits");
buttonPanel.add(toggleButton, BorderLayout.NORTH);
buttonPanel.add(minRange, BorderLayout.SOUTH);
buttonPanel.add(maxRange, BorderLayout.SOUTH);
buttonPanel.add(setLimits);
JSplitPane jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, chartPanel, buttonPanel);
jsp.setDividerLocation(0.8);
setContentPane(jsp);
What's happening here is that all of the layout options are completely being ignored. The GUI components are showing up properly, and the divider specifically is ignoring the preferred size of JFreeChart, and squeezing it to about 5% of space at the top of the frame.
In addition to problems with the splitpane not respecting your desired sizes, you are using BorderLayout constants but you haven't specified the layout for the panel (the default is FlowLayout).
This:
JPanel buttonPanel = new JPanel();
Should be:
JPanel buttonPanel = new JPanel(new BorderLayout());
I believe that using a float proportion on JSplitPane only works once the split pane is "realized", otherwise you're getting a proportion of zero because it doesn't know how big its going to be.
also:
buttonPanel.add(minRange, BorderLayout.SOUTH);
buttonPanel.add(maxRange, BorderLayout.SOUTH);
BorderLayout only allows one component to be in each area, so min range will never appear, as maxRange is now "the" south component. if you want both you'll need to put those 2 components in another panel, then add that panel to the south.
Try setting the minimum size too.
See: Java GUI Problems
JSplitPane pays attention to the minimum size, not the preferred size. Try simply changing setPreferredSize to setMinumumSize.
Dan Dyer is correct, you didn't set the Layout.
You could also set it by buttonPanel.setLayout(new BorderLayout())
And John Gardner is correct that you set a component to BorderLayout.SOUTH twice.
Also check out MigLayout if you don't already know about it. Its the least "surprising" layout manager I've ever used. It just works. It takes some learning, but very straight forward once you get over the syntax.
And I would avoid SplitPane if you can...its very finicky
Never call setPreferredSize() - it should be a calculation.
For example, your ButtonPanel is being set to a fixed preferred size.
What if you add I18N support and the user is using a language with very long localizations? What if the user resizes the frame?
Check out my article on layout managers for details on how you should really use them. It's from 1999 but still applies:
http://java.sun.com/developer/onlineTraining/GUI/AWTLayoutMgr/
Enjoy!

Categories