Java Swing JPanel Stay on top of JTable - java

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.

Related

Java JPanel and JFrame border dragging

I am opening a window with the following:
JFrame clientFrame = new JFrame("Frame");
clientFrame.setLayout(new BorderLayout());
final JPanel client_panel = new JPanel();
client_panel.setLayout(new BorderLayout());
client_panel.add(new Applet());
client_panel.setPreferredSize(new Dimension(765, 555));
clientFrame.getContentPane().add(client_panel, "Center");
clientFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
clientFrame.pack();
clientFrame.setVisible(true);
the frame has a random picture on it.
By default when you drag the bottom border upwards, it will naturally remove from the image from the bottom of the image.
The same if you drag the top border downwards it will again remove incrementally from the bottom of the picture.
How do I swap it around so instead it removes form the top of the picture instead of from the bottom?
clientFrame.getContentPane().add(client_panel, "Center");
Don't use magic values. People don't know where "Center" comes form. Use variables provided by the API:
clientFrame.getContentPane().add(client_panel, BorderLayout.CENTER);
What you are asking is not possible with any layout manager that I am aware of. The problem is that the layout manager only knows about size available to the component. It does not know why the size changed (ie, drag up or down). So the layout manager can only define rules based on the space available.
As a simple test use a JPanel with a BorderLayout. Then create a JLabel with containing an ImageIcon. Add the label to this panel and then add the panel to the content pane of the frame.
If you add the label to the BorderLayout.CENTER, then the image is centered in the space available so you lose part of the top and bottom.
If you add the label to the BorderLayout.PAGE_START then space is always taken from (or given to) the bottom of the component.
If you add the label to the BorderLayout.PAGE_END then space is always taken from (or given to) the top of the component.
If you want to consider the drag up or down of the frame then the solution gets much more complicated because you will need to add a ComponentListener to the frame and handle the componentResized and componentMoved methods. You will then need to track the previous state of the frame and then determine which properties have changed and then you will need to do custom painting of the image based on the property changes or you will need to write a custom layout manager that is aware of the property changes.

Java gridbaglayout using scrollpane with draggable jpanel

Im using a gridbaglayout on a jpanel with a scrollpane, all that works fine. Later in my code i add another jpanel using the constraints(x,y) to the same panel on top of everything else thats already there using the index, this also works fine. This added jpanel uses a listener to let me drag the panel around which also works but my problem is when i scroll or resize the jframe it puts the jpanel back where it was before i dragged it. Why?
This added jpanel uses a listener to let me drag the panel around which also works but my problem is when i scroll or resize the jframe it puts the jpanel back where it was before i dragged it. Why?
Because you are using a layout manager. When the frame is resized the layout manager is invoked and components are assigned a size/location based on the rules of the layout manager.
Check out the Drag Layout. It will allow you to drag components around a panel without resetting the location of the components.

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 to reposition JButton on resizing the window

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

Can you set a permanent size for a JPanel inside of a JFrame?

My current problem is that I have a JFrame with a 2x2 GridLayout. And inside one of the squares, I have a JPanel that is to display a grid. I am having a field day with the java swing library... take a look
Image
Java is automatically expanding each JLabel to fit the screen. I want it to just be those blue squares (water) and the black border and not that gray space. Is there a way I can just set the size of that JPanel permanently so that I don't have to go through changing the size of the JFrame a million times before I get the exact dimension so that the gray space disappears?
I also would like to set the size of those buttons so they are not so huge (BorderLayout is being used for the buttons and TextField)
GridBagLayout is what you really want to use. The GridLayout will force the same size for each component in the layout no matter what size constraints you put on them. GridBagLayout is a lot more powerful and a lot more complicated. Study up on the API page for it. Using GridBagLayout, the components won't fill the whole grid space if you don't want them to and can even stay the size that you ask it to be. To keep a component's size from changing, I would set all three available size constraints:
water.setPreferredSize(new Dimension(20, 20));
water.setMinimumSize(new Dimension(20, 20));
water.setMaximumSize(new Dimension(20, 20));
For your buttons, I would definitely use an inner panel as Bryan mentions. You could use either a GridLayout like he suggests or a FlowLayout if you don't want all the buttons to be the same size. Add all your buttons to that inner panel instead of the main one.
If you want the two checkerboards to stay the same size, then you'll need to have them each contained in their own JPanel. Set each of those parent JPanel's to have a layout type of GridBagLayout. Set the preferedSize for each checkerboard component and then add them to their respective containers. GridBagLayout should by default lay each board out in the center of the parent JPanel. So as the window is resized, the JPanel parent area will get larger or smaller, but the checkerboard components inside will remain the same size.
Alternatively, you could have your blue squares scale to the right size as the window is resized by having each checkboard square be a JPanel with a BorderLayout layout manager and adding the JLabel (with a blue background color) to its BorderLayout.CENTER location.
As for your buttons, try something like this:
JPanel theButtonPanel = new JPanel(new BorderLayout());
JButton button1 = new JButton("Fire");
JButton button2 = new JButton("Pass");
JButton button3 = new JButton("Forfiet");
JPanel innerButtonContainer = new JPanel(new Grid(1, 3, 8, 8));
innerButtonContainer.add(button1);
innerButtonContainer.add(button2);
innerButtonContainer.add(button3);
theButtonPanel.add(innterButtonContainer);
Lastly, consider using a design tool for your Swing user interface. Netbeans has an excellent UI designer built into it. Download Netbeans here.
If you can setResizeable( false ) on the top level frame you can then set your layout manager to null and hard code each location and size via setBounds. This is how I would do it (contingent on resizing of course).
I have had success solving problems like these using TableLayout which is a third party layout manager. You will need to download it and read the tutorial but the key would be to set the justification to CENTER when adding the JButtons to their positions in the layout.

Categories