Needing a component similar to JTabbedPane - java

I'm looking for a component like "JTabbedPane" in which I can design each tab separately and easily but I don't want the little square buttons with tab names in runtime! Instead, I want to activate each tab panel in my code. In fact, I want to have multiple "JPanel"s with same size and location (they have complete overlap) and I set visibility of each them manually in my code but the most importing thing is that I want to design each panel as easy as possible (like clicking on the tab names in design-time).

You could use CardLayout here to create your own overlapping panels as you have described. The visibility of each panel can be programmatically changed.

Related

JavaFX switching between two panes in a UI

I need to give the user of a UI the option to switch between two border panes on clicking a button (or toggle switch or whatever).
The panes have the same job, size and position - it's just a different configuration so I don't want both of them there at the same time.
When designing the UI in SceneBuilder I cannot place them at them same spot and set one visible and one invisible - because SceneBuilder obviously doesn't know that I want to stack them on top of each other.
Is there a way I can include both of them in the UI but only show one at a time?
I'd appreciate any ideas :)!
What you are looking for is the CardLayout. See oracle's documentation on that: https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html
Instead of adding your Components directly to the Component with the BorderLayout, instead add a JPanel which is using the CardLayout. Add the two Components to this JPanel.
Unfortunately I have no way to test this with the SceneBuilder right now, but you'll figgure out, how that works ;)

Java - Placing of few buttons and textboxes using SWT

I need to place few buttons one under another and few textboxes in the same way using SWT.
When I'm doing that, they are next to each other and I cannot change it even using
button1.setLocation(new Point(100,20));
button2.setLocation(new Point(400,10));
Can I add those things to something similiar to SWING's JPanel and move/position it freely as I need? Or maybe another solution? As to let You know - I cannot use SWING here. It has to be SWT. The reason is that I have already a chart made with SWT. The buttons and textboxes should be placed so they won't be covering my chart.
You can dynamically add a new control to the existing layout, but make sure you call the layout() on the parent Composite, where you have set the layout.
If you want to place a SWT control relative to another control, you can use org.eclipse.swt.layout.FormLayout.

Resize gui elements by dragging the corners

I have a swing project and noticed that the elements inside the JFrame is statically located inside the JFrame and I do not have the option of resizing the elements as I want to.
My gui app inside the designer window looks like the following:
I want to resize the button (E.g.) by dragging the corners of the button, but I am not allowed to?
As you can see on the following picture, the dragging is not allowed per pixel, but only per section in the JFrame:
How can I disable the static placement of the elements/Enable the self-dragging of elements inside the designer window?
Most likely you will need to disable the LayoutManager. On Netbeans, Setting this to null would provide you full control over the location and dimension of the child elements, so I am assuming that something similar should work here (although you seem to be using Eclipse, if that is the case, please state what plugin you are using).
It is to be noticed however that usually you want to have a layout manager taking care of how your components are rendered. I would recommend you take a look here for some more information on layout managers prior to removing them completely.
Setting size and position of components in a GUI should be left to the JRE at run-time, using:
The preferred size of the component (with current content, in the current PLAF).
The borders/padding/insets set to the component.
The layout used.
The component spacing defined in the layout constructors.
The layout constraints used when adding components to the layout (especially important to e.g. BorderLayout and the much maligned GridBagLayout, as well as many 3rd party layouts).
Generally, complex effects are created by using a nested layout.

Can a layout manager spawn several JPanels?

I have to build a rather large form with many controls. The controls are divided in basic controls/settings and extended controls/settings. The user can decide if he wants to see only the basic or both basic and extended controls.
I've dropped all extended controls onto their own JPanel so that I can easily switch between the two views by showing or hiding this panel.
Currently I'm using GroupLayout and what happens is that the controls on different panels are not aligned:
Label aaa: Text field
Label a: Text field
Label aaaaaa: Text field
----------------------------
Label b: Text field
Label bbb: Text field
Label bb: Text field
Unfortunatly I found now way to "synchronize" the layouts of the two panels (except using AbsoluteLayout and fixed control coordinates)
Is there any way to achive this?
Is my whole design flawed?
EDIT: If it is possible I would like to keep the GroupLayout manager.
As far as I know, no Swing LayoutManager (from JRE or open source) can span several panels.
I am currently working on such a feature (which I called "layouts synchronization") for my DesignGridLayout project, but it is not something easy to implements (I have started about 2 weeks ago and I still don't see exactly if and when I will get to something interesting, but I still have high hope for it;-))
One option you could check would be to add all components to the same panel (with just one GroupLayout then) and hide/show them based on user's selection. Hopefully, GroupLayout will adapt the size to the situation (after calling pack()).
If GroupLayout behaves well, then it would just be a matter of calling pack() each time after user changes his selection to show/hide extended fields.
Else you would have to manually set the size of your panel every time the user changes his selection.
Probably the easiest (good) way to do it is to add all the components to the main panel. Set the subpanels to non-opaque, and add the also to the main panel. The main panel the needs optimised drawing to be switched off.
Another technique is to add a spacer component. To the bottom panel add a component in the same column as the labels which dynamically takes the width component of its various size methods from the top labels. Do the same in reverse to the top panel.
I think there is no way to do it with the standard layout managers. You'll probably have to write your own layout manager, but it shouldn't be too hard if you subclass GroupLayout.
You could use GridLayout instead of GroupLayout which will give you uniform spacing between the columns
If you want to keep them in separate panels with separate layouts:
Iterate over all of the labels that you add, and find the maximum preferred width of each.
Iterate a second time, and set the preferred size to that each label's preferred height, but the maximum width.
This is the explanation of th GridLayout. This will set every component to the size, you expect it. With the GridData object you can specify how the components are ordere.
Examples
(source: sun.com)

Is there a "Group Box" equivalent in Java Swing?

Trying to build a GUI application in Java/Swing. I'm mainly used to "painting" GUIs on the Windows side with tools like VB (or to be more precise, Gupta SQLWindows... wonder how many people know what that is ;-)).
I can't find an equivalent of a Group Box in Swing...
With a group box, you have a square box (usually with a title) around a couple of related widgets. One example is a group box around a few radio buttons (with the title explaining what the radio buttons are about, e.g. Group Box entitled "Sex" with "Male" and "Female" radio buttons).
I've searched around a bit... the only way I found was to add a sub-pane, set the border on the sub-pane and then add all the widgets in the "group" to the sub-pane. Is there a more elegant way to do that?
Create a JPanel, and add your radiobuttons to it. Don't forget to set the layout of the JPanel to something appropriate.
Then call panel.setBorder(BorderFactory.createTitledBorder(name));
Others have already commetned about JPanel and using a TitledBorder, that's fine.
However, when playing with Swing LayoutManagers, you may find it annoying that components in different JPanels cannot align correctly (each panel has its own LayoutManager).
For this reason, it is a good practice (check "JGoodies" on the web for more details) in Swing GUIs to NOT use TitledBorders but rather separate groups of components in a JPanel by a JLabel followed by a horizontal JSeparator.
Ref. "First Aid for Swing"
A Group box is just a set of 'logically grouped widgets'.
This in the swing world is a JPanel.
Add your widgets to a JPanel.
Set its border type to 'Titled Border' and give the title, same as the name of the VB6 'frame'.
Voila. You have your group box.
Here's a quote from the JRadioButton javadocs since you brought up radio buttons.
An implementation of a radio button -- an item that can be selected or deselected, and which displays its state to the user. Used with a ButtonGroup object to create a group of buttons in which only one button at a time can be selected. (Create a ButtonGroup object and use its add method to include the JRadioButton objects in the group.)
Note: The ButtonGroup object is a logical grouping -- not a physical grouping. To create a button panel, you should still create a JPanel or similar container-object and add a Border to it to set it off from surrounding components.
Not AFAIK, at least not with standard swing widgets.
In VB you have a group widget, which is essentially a panel + border.
In Swing you have a JPanel which is the container widget, and you create and set a border object on it only if you need one. One can argue that in a way that is more elegant since you don't pay for something you don't use (e.g., border)
As David Koelle mentioned about setting up border through java code, you can also achieve similar result in designer mode.
I'm responding based on the Uri's comment which explaind what the OP meant by Group Box:
Uri: I think he means the control group you see in many dialog boxes, where you have a square around a bunch of widgets such as radio buttons, for example.
As far as I know, every JComponent can set a border for itself, so you don't need a second panel.

Categories