jscrollbar show other components - java

Good evening everyone;
I java swing i am trying to have a menu like in the picture
(in the picture left side)
However, it appears that it is more difficult than i thought.I had border layout and i put box layout in the middle and jscroll bar for the right side. Inside the box layout i but labels with icons and i try to chance visibility with adjustment level event. However, i could not manage to obtain any results. I made a research on the internet and stackoverflow however, again i could not exactly reach my purpose.
Regards ...

JScrollBars are not typically used alone, they are used in conjunction with JScrollPane.
Basically, you want to add you components to a container of some kind (say a JPanel) with an appropriate layout manager, then set that container as the view within a JScrollPane
See How to Use Scroll Panes for more details

i found an answer inspiring from comments of other users. First of all thank you for having a look to my question.
i used the codes in here;
http://www.java2s.com/Code/Java/Swing-JFC/AsimpleJScrollPanedemonstration.htm
problem is i required to create a container and put the items to container and then take that pane to put jscroolpane.
Regards ...

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 ;)

How to overlap panels in Java Swing?

I have a screen in my application. The layout of the screen is shown in the attached image file.
I have to add upto 5 labels in Panel1111. But, When I try to add labels in Panel1111, the Panel11 resizes and Panel12 shifts downwards to give space to Panel11.
I want to overlap content of Panel1111 on Panel12.
How can I achieve it?
Layout details:
Panel1 : BorderLayout
Panel11: OverLayLayout
Panel111: GridBagLayout
Using JLayeredPane. Go to Oracle Java website, and go through the tutorial: How to Use Layered Panes
Java's Layout Managers by default try to show all information that is inside them.
If you say you want two panels to overlap, this essentially means that the lower one cannot be seen fully, and also not interacted with in the hidden/overlapped part. Then, this part of the panel doesn't make sense any more. So you should probably rethink your GUI.
If you want it to overlap only at certain times, and the user can define when it should overlap and when not, then you'll need to handle that manually by using no Layout Manager at all, but position the elements yourself. Oracle provides some hints how to do that: http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html.
In the end, you might end up writing your own, custom Layout Manager to handle the resizing of the panels.
Note: only the the layout of Panel 1 must be manually managed. The other panels can likely be handled by a LayoutManager again.

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

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)

Categories