My Jpanels keep sinking when I go to them - java

In my program I go from the log in to the main menu,to different areas of the site.
jPanel2.removeAll();
Items panel = new Items();
jPanel2.add(panel);
jPanel2.repaint();
jPanel2.revalidate();
I thought that I found a way to change jpanels so that I can go from one jpanel to another which in the same screen.
The problem with the above code is that each time I change jpanels, the old jpanel isn't gotten rid of, it is staying there, meaning whenever I change jpanels the jpanel slowly gets lower on the page due to it being put underneath the other jpanels that were created before it.
Is there a way to get rid of the jpanels so that when I go to a new jpanel it doesn't slowly lower down the page?

I think you should use CardLayout to achieve this requirement.
Check this below link.
https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html
or this SO
Changing Panels using the Card layout

Related

Keeping a component in a fixed position within a BoxLayout

I have a JPanel that uses a horizontal Box layout and contains a JLabel that I would like to keep in the exact same position as other components within the JPanel are setVisible(false). Currently, the JLabel moves to the left as other components become invisible.
What's the easiest way to go about this?
EDIT: Pics added
So this is what the JPanel look like with all components visible
When I set the three JTextFields on the right to invisible, the JLabel set to text X moves to the left like this:
But I would like it to stay where it was like this:
EDIT2: I'm actually using Netbeans GUI editor's Free Design for this particular JLabel. I'm sorry for the mistake - I've been using a lot of BoxLayouts recently and I got confused!
Currently, the JLabel moves to the left as other components become invisible.
Yes, layout managers are designed to only work with visible components. I'm not sure if any of the default layout manager will work, but I would look into using the GridBagLayout, since this layout is based on a grid structure so as long as you have components in that grid on another row the label should not shift.
Otherwise, you could dislay the "other components" in a panel using a CardLayout. Then instead of making the components invisible, you swap the panel with an empty panel.
Read the section from the Swing tutorial on How to Use CardLayout for more information and working examples.
Edit:
Based on your picture the easiest solution is to use "glue":
panel.add(Box.createHorizontalGlue);
panel.add(xLabel);
Now the label will always be displayed at the far right of the panel. Read the tutorial on How to Use BoxLayout for more information about "glue".

Using BoxLayout manager with JLabels in java

Hey all I am having some trouble trying to get my jlabels to line up the way I want them to. I am using 3 panels (Title Panel, Display Panel, and Button Panel) Inside of my DisplayPanel I have a JtextField, 3 jlabels and the next thing I want to have happen is for the rest of my JLabels which happen to be ImageIcons to be in a set location inside of my panel, which is in boxLayout.
MasterOffense1 = new JLabel(Mastery1);
MasterOffense1.setLocation(400, 100);
MasterOffense1.setSize(25, 25);
MasterOffense1.setToolTipText("<html>"+"Double-Edged Sword<br> Melee- Deal an additional 2% damage and receive an additional 1% damage<br> Ranged- Deal and additional 1.5% damage and receive an additional 1.5% damage"+"</html>");
DisplayPanel.add(MasterOffense1);
MasterOffense2 = new JLabel(Mastery2);
MasterOffense2.setLocation(400, 130);
MasterOffense2.setSize(25,25);
MasterOffense2.setToolTipText("<html>"+"Fury<br> Rank-1: +1.25% Attack Speed<br> Rank-2: +2.5% Attack Speed<br> Rank-3: +3.75% Attack Speed<br> Rank-Max: +5.00% Attack Speed"+"</html>");
DisplayPanel.add(MasterOffense2);
There is code for the 2 JLabels with ImageIcon inside them and they keep showing up right below each other. I have no idea how to make this happen and I am completely stumped..
Any help would be appreciated. Thanks!
There is code for the 2 JLabels with ImageIcon inside them and they keep showing up right below each other.
Then is sounds like you are using a vertical BoxLayout.
If you want the labels to be displayed horizontally, then you can add the labels to a panel and then add the panel to your "displayPanel".
That is you can nest panels that use different layout managers to get your desired layout.
Also, use standard Java variable names. Variable names should NOT start with an upper case character.

How to hide other panels or panes during coding

Using Eclipse Juno for Java & WindowBuilder
I have three panels of the same size that lay on top of each other - they have different widgets. During coding, they all display and clutter up what I'm doing.
I can show and hide them in runtime as needed but, I want to display only the one I'm working on while doing drag and drop of widgets. I've tried using different panels and pane types (tabbed, layered...) and selecting opaque but, nothing hides them.
How do I hide the other (panes, panels...etc) during coding?
I have the same issue. Among other reasons, my solution was to create separate classes per view. So my frame would be its own class, it would maybe have a TabbedPanel (or whatever it's called), and then I would have a new class for each tab on that tabbed panel. Each class would extend JPanel so I could plop it right in there. That way not only is your gui design not cluttered up, but your code logic is separated into separate files, where it might belong anyway.
[SOLVED] Answering my own question.
It may not be perfect or the best/correct way, but it works!
WindowBuilder wants to Surround other panels/widgets that are within it's bounds so, you have to trick it by using opaque, Order>forward/backward then setting the desired bounds (all panel sizes and bounds can be equal and will overlay nicely both during widget drag&drop and runtime).
Here's how to do it with a 3-Panel example (NOTE: WindowBuilder is buggy/in-consistent and often I needed to select the items from the gui, not in the Components tree).
Create your first panel. Add your widgets and border to it.
Create your second panel (the one you want to overlay on top of the first one). This second panel MUST not be completely inside the first panel - it MUST extend beyond the edges of the first panel (parts of it can be inside the first panel). This takes a bit of trial because of the 'surround', mentioned above. Use the shift-key to stop the snapping.
Select the top panel in the gui, NOT from the Components tree, and toggle the Opaque property. The top panel (first or second) in the tree is the one you set to opaque and work on.
Add another panel and repeat the process.
Once you get your widgets/etc as you want them, use the property Bounds to set them all the same or as desired. After that, as long as you don't move a panel by dragging it, it will remain un-surrounded by the other panels. If you move by dragging, it may get set to surround...
I've done this a dozen times now and it works consistently.
Below is a shot of 3 panels overlayed, un-surrounded and not opaque, thus showing widget clutter
Below is a shot after the bounds are set (and not surounded). Opaque and order not yet set:
Below is a shot with bounds set and panel 3 moved forward and opaque set:
Below is a shot with bounds set and panel 2 moved forward and opaque set:
... etc, etc... Now you can work on a panel that's ordered to the front and, naturally, use the setVisible in your code...

JScrollPane doesn't show if BorderLayout constraint is CENTER?

Been developing a game for a while, and currently re working the GUI, or at least trying to. Had the massive problem of not being able to resize the frame (without issues), as I didn't understand layout managers very well. A few projects later, and time to come back and do some more on the game, and I hit a problem...
The basic layout of the main frame is, mainPane, containing one gameScrollPane and one controlPanel. The scroll pane is a scroll pane, and the control panel a normal panel. The scroll pane contains the main game panel.
As I wanted the scroll pane to take up most of the screen, with the control panel taking up a small lower area, much the same as many Sim like games, so chose the Border layout for the mainPane. I added the scroll pane and set the constraints CENTER and the control panel added and constriants SOUTH. This didn't show the scroll pane, so I played around trying different constraints, and it seems that only when I set the scroll pane constraint to North, does it display at all.
To demonstrate this, I have created a quick video...
http://screenjel.ly/q5RjczwZjH8
As you can see, when I change the value of NORTH to CENTER and re run, it's like its not there!
Bonus points for anyone who can see a clear second problem which I may start another question for after this issue is solved!
I thank you for your time to read this.
Thanks in advance for any ideas or thoughts :)
Rel
If you'd posted some code to start with then you might have gotten a really quick answer. Luckily, you posted a link in the comments to the other response.
The setContentPane() stuff is weird, especially after doing some things to it that will then get wiped out. However, that's not your problem.
The issue is that you are adding levelMaker and personMover right to mainPane without any constraints. These will then be blowing away anything you set for CENTER... in this case the previously set gameScrollPane.
That's why you see it for NORTH and not for CENTER.
I can't get the video to show. It's been buffering for ages.
My guess would be that the scrollpane is in fact filling the center; it's just your game panel that's not being shown.
Your game panel needs to return reasonable values for getPreferredSize().
Update
Another thing you may want to do is have your game panel implement the Scrollable interface. You can then override getScrollableTracksViewportWidth and ...height to return true so your panel will be forced to the scrollpane's dimensions.

How do I layout a panel on the top of my Dialog so that it has two buttons?

I need to layout a panel on the top of my Dialog so that it has two buttons (Save and Cancel).
I want the save to be on the left and Cancel to be on the right side.
I've created a JPanel using the MigLayout and docked it to the north of the content pane, and can't for the life of me figure out how to add the two buttons to it so that they appear as I want them. Docking them within the panel seems to get rid of all padding in the dialog (which looks terrible).
Any help would be greatly appreciated.
As an aside, you should probably not be dictating which button is on the left or right. That's one of the way cool things about MiGLayout (platform independence, even on things such as where the cancel button should go).
p.add(cancelButton, "tag cancel");
p.add(okButton, "tag ok");
Now the buttons will appear in the correct order, based on platform.
Here's an article with code doing what you are going for. I strongly recommend avoiding trying to force the size of components like buttons (these really should come from the platform look and feel). Also, docking is fine if it makes sense to do so, but I rarely find it to be necessary. Instead of building a totally separate panel for your buttons, just span the row that contains the buttons - much cleaner, and you don't wind up with all of the nested panels.
It's hard to break from the border layout technique of nested panels, but once you get the hang of it, MigLayout is a dream. BTW - I understand that there are times where you might want to build up the button panel in a library - if that's the case, then separate panels may make sense (although you could also have the library add a button row to an existing panel, instead of returning a panel that you then add to the layout).
Doh, always happens as soon as you ask a question, the answer pops out:
JPanel buttonPanel = new JPanel(new MigLayout("fill","[50][50]",""));
buttonPanel.add(saveChangesButton);
buttonPanel.add(cancelButton, "align right");
getContentPane().add(buttonPanel, "dock north");
Note that the content pane is using the MigLayout too.

Categories

Resources