how to reposition JButton on resizing the window - java

I have kept a JButton in the bottom middle part of my JFrame. Now whenever I resize the window the JButton should be repositioned (in the new centre) depending on new resized window. Can anyone tell me how to accomplish this. Thanks in advance.

Components in Swing are rendered according to the frame's layout manager. The default layout manager is BorderLayout, which divides the frame into five logical parts: North (up), South (down), East (right), West (left) and Center (everything in between). Components are centered by default.
You can create subframes for each part, and give them the a similar layout manager, or a completely different layout manager. in your case you'd want to create a new subframe at the South position of the main frame, and put your button in one of its North, Center or South positions.
Hope this helps...

1) Create a JPanel usign a FlowLayout with "center alignment" and add your JButton to the panel.
2) Add this panel to the "SOUTH" of the content pane which uses a BorderLayout by default.

i would take a look at LayoutManagers
BorderLayout would do the trick
http://java.sun.com/docs/books/tutorial/uiswing/layout/border.html

Related

How to add auto-resize components?

I'm wondering how I can add (what LayoutManager should I use) to make the app automatically adjust the components to the current size when full screen mode is enabled or even when the frame is stretched.
Currently this is what the application looks like, when you enable full screen mode the components are static.
UPDATE: I'm pasting pictures so you can get an idea of what I wrote.
Component layout in IntelliJ: layout
The current design in the small window looks ok: current design
Appearance if we enlarge the window (window zooms in, elements are static): full screen
So I'd like the components to automatically resize to the current window when fullscreen mode is enabled.
Okay. I suggest the following design:
your mainPanel, that you assigned as contentPane for your frame gets a Borderlayout.
In the North, you put a panel with a label or just a label, with Allignment X to the left.
the West gets a panel I suggest we call WestPanel and the East gets another one, which I call EastPanel.
WestPanel gets a Boxlayout or a Gridlayout. If you use Gridlayout, you can use 2 Grids wide and for each line one grid depth. You then can add all the labels in the left column and the textfields on the right.
Or you can make panels, with Borderlayout, labels to the West, textfields to the east, and pile them with emptyBorders ontop of each other. I suggest using a method for that.
The EastPabel gets a Tabbed Pane inside of it. Each tab can have its own layout manager, depending on what you want it to be in the end.
Does this help you?

Add JComponents in the middle of the JPanel

I would like to have a trade-off between FlowLayout and GridBagLayout. In a nutshell, I've some components to add in my JPanel (I don't know how many of these, it's a creation at runtime) and I would like that these components to be center vertically.
FlowLayout was ideal for my goal, but it adds components at the top of JPanel. So, I decided to use GridBagConstraints, as someone suggested here (Java layout manager vertical center) but my problem with this solution is that GridBagLayout doesn't create a new line automatically, so I've just one row (vertically center, at least!!!) where my components are inserted that goes beyond the screen size. Could anyone give me some tricks about that???
I would like that these components to be center vertically. FlowLayout was ideal for my goal, but it adds components at the top of JPanel
You are never forced to use a single panel or layout manager. You can use nested panels. For example:
JPanel centered = new JPanel( choose your layout manager );
Box vertical = Box.createVerticalBox();
vertical.add( Box.createVerticalGlue() );
vertical.add( centered );
vertical.add( Box.createVerticalGlue() );
frame.add( vertical );
The glue in the vertical panel will take up equal amounts of extra space which leaves the "centered" panel vertically centered. So you can add components to the centered panel using whatever layout you want.

Make buttons unresizable

So I was trying to google how to set a default size to JButtons so that they don't grow as the JFrame is resized. I didn't see a setDefaultSize method but the closest one I could find that does a similar job is setMaximumSize(). However, it doesn't seem to work in my situation and I'm guessing it's because I'm using Grid Layout for positioning my buttons in the frame, here's a small piece of my code:
rightPanel.add(ButtonA);
rightPanel.add(ButtonB);
rightPanel.add(ButtonC);
outerPanel.add(leftPanel);
outerPanel.add(rightPanel);
getContentPane().add(outerPanel);
Here's a picture of what happens:
I would also like to have my buttons in the middle of the right panel when I'm resizing (just like they are now but a lot smaller). Any idea of how I can fix this? I'm assuming that I have to use another layout or something.
Thanks
EDIT: I modified my code to use BoxLayout but it does not seem to put the buttons in the middle. The X Alignment is working but Y Alignment is not doing anything:
ButtonA.setAlignmentX(CENTER_ALIGNMENT);
ButtonA.setAlignmentY(CENTER_ALIGNMENT);
ButtonB.setAlignmentX(CENTER_ALIGNMENT);
ButtonB.setAlignmentY(CENTER_ALIGNMENT);
ButtonC.setAlignmentX(CENTER_ALIGNMENT);
ButtonC.setAlignmentY(CENTER_ALIGNMENT);
JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
rightPanel.add(ButtonA);
rightPanel.add(ButtonB);
rightPanel.add(ButtonC);
outerPanel.add(leftPanel);
outerPanel.add(rightPanel);
getContentPane().add(outerPanel);
EDIT2: Fixed with vertical glue.
A GridLayout will always resize the components to fill the space available.
Try using a vertical BoxLayoutinstead. See the section from the Swing tutorial on How to Use Box Layout for more information and examples.
Encapsulate each JButton in a JPanel with a FlowLayout, and then add those FlowLayout JPanels to the rightPanel instead of the JButtons themselves. This will allow you to keep your evenly spaced buttons, but won't make them expand to take up the entire space that the parent container has available.
If you don't want them evenly spaced, but to be three consecutive buttons one after another top down, you can make the right panel have a BorderLayout, add a sub panel to the north area of the BorderLayout with the original GridLayout that the right panel had, and then add those FlowLayout panels containing the JButtons.

How do I limit vertical JDialog resizing to a single component within the dialog?

I have a JDialog that consists of two JPanels, one above the other. Currently, when I resize the JDialog only the bottom panel resizes in the vertical direction. However, I only want the top panel to resize. The only component that the top panel contains is a JScrollPane, so I want any vertical resizing to result in an increased/decreased view of the top panel's content. What is a good way to do this?
Thanks in advance!
elise
, I only want the top panel to resize
dialog.add(topPanel, BorderLayout.CENTER);
dialog.add(anotherPanel, BorderLayout.SOUTH);
This is a job for the proper LayoutManger. Here is a good link that explains LayoutManagers visually and does it quite well.

Java Swing JPanel Stay on top of JTable

I have a Jpanel that is used for displaying messages. When mouse enters a JLabel it expands to normal size and when mourse leaves it disappear.
There are overlaps between JTable and JPanel when the JPanel expands to normal size. As JTable's height cannot be changed during runtime only way I can think of is let JPanel stay on top of JTable.
I am very new to Java ans swing. Any idea please?
Sound like you should be using a tool tip.
Edit:
Try using a BorderLayout. Add your main panel to the CENTER and your message panel to the SOUTH. When you make the message panel visible you can revalidate() the main panel and the Center panel will shrink in size to take whatever space is not required by the south panel.

Categories