Selectively hide panels - java

When I start my program I have 6 panels. Then I have some radio buttons and depending on the choice some panels hide. In this example I want to hide all panels except 1 and 2. When I click the radio button instead of hiding the rest of the panels and leave the first 2 panels in the current position, it moves them.
private void MonocButtonItemStateChanged(java.awt.event.ItemEvent evt) {
Panel3.setVisible(false);
Panel4.setVisible(false);
Panel5.setVisible(false);
Panel6.setVisible(false);
}

Propably the panel that contains the show/hide panels has FlowLayout manager. When components get invisible the container lays out from scratch the components again.
To get around with this you can use an AbsoluteLayout or make the panels invisible in a diferent way so you cannot see them but they are occuping space.

Looks like switching to Absolute Layout fixed the problem! Solved!

Related

How do I get jframe buttons to show up next to each other? [duplicate]

I'm making a calculator same as provided in windows 10 for practice purpose, but I am unable to remove the space between the JButtons. I'm using Netbeans designer view to do this. I have tried by doing margin as 0 even doing -2 of both the buttons but whenever I resize the button and drag it to the other one, the other button goes away automatically.
Here is the screen shot what I want to do:
Here is the design view:
Give the JPanel that holds the JButtons a GridLayout, one with proper rows and columns (call the constructor that uses 2 parameters, again for rows and columns) but that uses no more parameters -- so the layout's horizontal and vertical gap is set to the default size of 0. GridLayout API
Add your JButtons to this JPanel.
Be sure to pack() the JFrame (or other top-level window) after adding components
And calling setVisible(true) after packing
That's really all there is to this.

stop the manually set location of component from changing back according to the used layout after resizing the frame

I have a Jframe that contains some buttons and a main Jpanel that contains some other Jpanels, the layout of the frame is free design and the layout of the main panel is grid bag layout.
What my program does is: moving panels that are inside the main panel from a column to another after clicking on a button.
To do that, I change the location of the panels manually using jpanel.setLocation(x,y).
The program works just fine, but when I resize the frame the panels return to their original locations as specified by the grid bag layout used. I don't want that to happen in my program.
I figured that if I use the null layout, I can solve this problem and get the result that I want.
But I want to do it using the layouts that I specified earlier. Is there a way to do it?
Thank you in advance.

Java Swings GUI, changing the display based on which button is clicked. What is the ideal way to implement this?

I'm pretty new to programming Java based GUI applications. Here's what I have in mind, I want to divide the window into two areas.
The first are contains buttons (or a list), and based on which button was clicked, or which item was selected, the second area changes. (finite number of buttons)
Something like this:
I can think of many ways to do this, but I am not sure what is the best practice. Do I have several invisible panels and make only 1 panel visible at a time, or do I change the ordering (bring panel x to front), or is there some other way?
Appreciate any help I receive!! Thanks in advance!
Although this is a primarily opinion-based answer I'd go with a Nested Layout approach:
Main panel with BorderLayout. Or you can use the frame's content pane which already has BorderLayout as default layout manager.
Left panel with BoxLayout (or GridBagLayout).
Right panel with CardLayout.
Note: Buttons in the left panel should switch right panels cards.
See Lesson: Layoing Out Components within a Container tutorial.
For complex GUIs you have also third-party layout managers available, listed in this answer:
MiG Layout
DesignGridLayout
FormLayout

Drawing borders on a java swing component

I have a UI requirement in java swing wherein I need to achieve the below:
The 2 buttons on the top are placed in a JPanel. I need to draw a line through the center of that panel, upto the beginning of the 2 buttons. The panel below is a container of panels arranged in a card layout. As and when the button is clicked, the card is switched showing another panel.
So in all respects this looks like a JTabbedPane, with one difference, the tabs are buttons arranged in the center of the tabbed pane. I need this difference for the UI I am building.
As of now, the buttons and card layout panel, looks like the below
As you can see, the buttons and panels appear and look separate, instead it would be nice if they are made to appear like they represent one unit.
As you can see, the buttons and panels appear and look separate, instead it would be nice if they are made to appear like they represent one unit.
Put the Border around the outer panel. That is use a panel with a BorderLayout. This panel can have a LineBorder. Then you add your button panel to the NORTH and the panel with the CardLayout to the CENTER.
The line won't be drawn through the buttons but the buttons and panel will appear like they represent on unit.

Reusing same JPanel in Java swing

I have two JPanels that needs to add a JPanel into it, however only the last added JPanel does it show the JPanel. As shown below:
holderPanel1.add(dataPanel);
holderPanel2.add(dataPanel);
only holderPanel2 shows the dataPanel, but holderPanel1 does not show.
UI Components (such as JPanel) are the underline representation of things you see on the screen (location, parent, sub-components etc.), so each panel that you see on the screen has to have a separate underline representation, so you cannot add a panel to two different panels, you need to create two separate panels.

Categories