Drawing borders on a java swing component - java

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.

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?

Formatting JButtons in JSplitPane

I'm having trouble getting my JButtons to appear where I want. For some reason all my buttons are appearing in the middle of the Panel when I want it to appear flushed with the top of the Panel, centered. I've tried manipulating different layout positioning such as BorderLayout.NORTH and GridBagConstraints.NORTH however I do not know if I utilizing it on the right components.
Currently my code has A Frame holding a JLayeredPane holding a JSplitPane with two JPanels in it so it has gotten quite confusing.
Do you have any suggestions on how I can get the buttons to appear on the top of the Panel?
I currently have the panel using a GridBagLayout
Read the section from the Swing tutorial on How to Use GridBagLayout for a working example and information about the constraints used by the layout manager.
For some reason all my buttons are appearing in the middle of the Panel
Specifically read the section on the weightx/y constraints, which states that the components will clump together in the center unless you use non-zero values.

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.

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.

Problem in Java GUI

Before reading about my problem, first look at this GUI Diagram.
There are three bars at the top as follows:
one having buttons "pictorial view", "textual view" etc.
second having buttons "processes", "organisation" etc.
third having buttons "application to processes" etc.
After that two information bars are there.
After that a canvas window displaying the diagram.
Now, I want a GUI similar to this with the following features (relative to the above diagram):
At first, only first bar appears and below it a white blank canvas having no diagram appears spreading all over the GUI.
When the user clicks on the "pictorial view" button the second bar appears below the first bar and after that the same canvas without diagram spreading over the remaining space
When the user clicks on the "application" button on the second bar, the third bar appears below the second bar, and after that the same empty canvas spreading over the remaing space in the GUI.
I had tried to implement it by first having a "main panel" with BorderLayout. After that as showing in the following figure:
mainPanel(Border Layout)
|
|--topPanel (at NORTH of the mainPanel's Border Layout)
|
|
|--centerPanel (at CENTER of the mainPanel's Border Layout)
topPanel - to contain all the bars (bars should be added dynamically when a user clicks on a button)
centerPanel - to contain the canvas and adjust its size automatically when new bars are added in the topPanel
I would use a BorderLayout for the app, with the toolbars in a northerly pane.
That north pane I'd give a vertical BoxLayout and put the 3 toolbars into 3 successive boxes. That should take care of the geometry.
I'm not sure if making a toolbar invisible will cause it not to occupy space, but that would be the simplest. Alternatively, you could dynamically add()/remove() toolbars from the north pane.
The other alternative is to go with MigLayout where you can add as many components as you need to the "top". By setting "hidemode" parameter you can specify how space will occupied by invisible components.
In general MigLayout is much more flexible for almost any task and can be used instead of any standard layout or combination of layouts

Categories