JScrollPane and JPanel - java

I have a simple JPanel so defined:
JPanel listObject = new JPanel(null);
listObject.setBounds(0, 350, 300, 100);
I would insert this into a JScrollPanel
I tried to define it so:
JScrollPane scroll = new JScrollPane(listObject);
scroll.setBounds(0, 375, 270, 100);
mainPanel.add(scroll);
The problem is that with this code the panel is not show inside the scrollPane. How can i do for that?
Thank you.

Note some problems with your code:
JPanel listObject = new JPanel(null);
You're adding this to a JScrollPane and note that JScrollPanes don't handle null layout-using viewport views well at all. It in fact can completely mess up the scrollpane's ability to scroll.
listObject.setBounds(0, 350, 300, 100);
By calling setBounds(...) you're in effect saying try to put this component at the [0, 350] position of the container that holds it and size it to [300, 100]. Note that the container that you're adding it to is the JScrollPane's viewport. So if the viewport follows this request, and since you're trying to make your JScrollPane and its viewport have a width of 270 with this code here:
scroll.setBounds(0, 375, 270, 100);
You're in effect asking the JScrollPane to add a JPanel way off to the right beyond the bounds of the JScrollPane itself. Ignoring that the viewport is likely not even using a null layout, this really makes little sense.
You will want to get rid of the setBounds(...) business to start with. JScrollPanes use their own layouts, and so there is no need or good effect by trying to set the bounds of a component being added to it. Instead consider overriding the JPanel's getPreferredSize. Also, a component held by a JScrollPane should not use a null layout as this will usually prevent the JScrollPane from working correctly, from knowing how to scroll the container.
The bottom line here is that you will want to avoid null layouts in general as they lead to the creation of rigid GUIs that are very difficult to enhance or debug, that may look OK on one platform but will definitely look bad on all other platforms or screen resolutions. It's a newbie trap to think that null layout is the way to go, one that you'll quickly learn to avoid the more experience you gain.
If you're still stuck after reading the answers, please consider creating and posting your minimal example program, a small compilable and runnable program that shows for us your problem.

Related

Absolute Layout Panel within JScrollPane

I'am using panel with absolute layout (don't ask why) and I need to add elements on it programmatically. I done that part, but now I want to surround panel with JScrollPane so that when user add more items, scroll bar does its job. But surrounding panel with scroll bar doesn't work. What can I do here.
JFrame frame = new JFrame();
frame.setSize(582, 451);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 11, 546, 391);
frame.getContentPane().add(scrollPane);
JPanel panel = new JPanel();
scrollPane.setViewportView(panel);
panel.setLayout(null);
for(int i=0;i<labelList.size();i++){
if(a==4){
a=0;
b++;
}
labelList.get(i).setBounds(10+120*a+10*a, 10+130*b+10*b, 120, 130);
labelList.get(i).setOpaque(true);
labelList.get(i).setBackground(Color.WHITE);
labelList.get(i).setText(Integer.toString(i));
panel.add(labelList.get(i));
a++;
}
You're not going to like my answer, but I feel that we should be compelled to give you the best answer, which is not always what you want to hear. And that is this: use layout managers to do your component layouts. Yes while it might seem to newbies that null layouts are the easiest to use, as you gain more experience you will find that exactly the opposite is true. Null layout use makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. Instead you will want to study and learn the layout managers and then nest JPanels, each using its own layout manager to create pleasing and complex GUI's that look good on all OS's.
As for ease of use, I invite you to create a complex GUI with your null layout, and then later try to add a component in the middle of this GUI. This will require you to manually re-calculate all the positions and sizes of components added to the left or below the new component, which is prone to many errors. The layout managers will all do this automatically for you.
Specifically use of valid layout managers will update your JPanel's preferredSize automatically increase as more components are added and as the JPanel is revalidated (the layouts are told to re-layout their components). Your null JPanel will not do this, and so the JScrollPane will not work. Yes, a work around is for you to manually calculate and set your JPanel's preferredSize, but this is dangerous and not recommended for the same reasons noted above.

Java: Scrollbar doesn't appear, even though `setVerticalScrollBarPolicy` and other options are set

I got an issue with my JScrollpane. I am adding Labels to it, out from a list. The adding is working and I see the labels. The amount of Labels added is unknown to me, so it can appear that the border of the scrollpane wont be enough. Thats the advantage of using a Scrollpane, so that I can actually scroll down if needed. But the scrollbar doesnt appear. I know there are many questions like that asked, but I tried almost every proposed suggestion. I tried setPreferredSize(), setLayout(), scrollPaneApps.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS), but nothing of it worked.
public DisplayProperties() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(null);
contentPane.setPreferredSize(new Dimension(450,300));
JScrollPane scrollPane1 = new JScrollPane();
scrollPane1.setBounds(15, 54, 195, 202);
scrollPane1.setViewportBorder(new LineBorder(new Color(0, 0, 0)));
scrollPane1.setPreferredSize(new Dimension(185,195));
scrollPane1.setLayout(null);
scrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
contentPane.add(scrollPane1);
int b = 0;
for(String s : XMLParser.ApplicationsListGUI)
{
b = b + 20;
JLabel lbl = new JLabel("lbl"+s);
lbl.setText(s);
lbl.setBounds(10,b,100,15);
scrollPane1.add(lbl);
scrollPane1.revalidate();
lbl.setVisible(true);
}
}
So why doesnt this Scrollbar dont appear?
You appear to have two JScrollPanes involved, scrollPaneApps, which you set a vertical scrollbar policy, which you try to add a component to, but which you never add to the GUI, and scrollPane1, which you don't set a policy, never add components to, but do add to the GUI. Sorry, but this is totally crazy. You need to fix this so that your code makes sense:
Add the actual JScrollPane that has its vertical scrollbar policy set to the GUI. If it's not added to the GUI, it makes sense that it will never be seen.
Don't add components directly to the JScrollPane but rather to its JViewport via the setViewportView(...) method. Or you can add a component to the JScrollPane constructor which is little more than syntactic sugar for adding it to the viewport.
Avoid null layouts and absolute positioning (avoid setBounds(...)). Using these will make your GUI's rigid, ugly, and almost impossible to improve upon later. Never set a JScrollPane's layout as null, for if you do, it will stop working. Completely.
It doesn't show since you have no layout for the scrollPaneApps panel. So remove this line and it should show.
scrollPane1.setLayout(null);
Hope it helps.

JScrollPane and sizing issues

I am having quite a problem in regards to a proper way to handle 'packing' a JPanel and allowing a scroll bar.
I left out a bit of the code, but I believe what is provided should suffice.
The issue, is that the JScrollBar either doesn't show up, or can't be interacted with (if I set the scroll bar to always have the vertical bar).
Here is an image depicting this:
![No Scroll Bar Present][1]
The ProjectPanel (extends JPanel) are of fixed size and, as you can see, extend farther than the visible view port. There is NO way of getting the calculated height of the JPanel (ProjectSelector), as the ProjectPanels can also be transitioned as so:
![They need to fill as a grid][2]
If anyone could help provide some insight on how to do this, that would be great. As of now, I would like at all costs not to use an external API, as that would cause more harm than good.
Question:
How can I set the height for the preferred size to be 'flexible', so as I add components it can expand? If that wouldn't be ideal / no possible, how could I properly allow the JScrollPane to show all components of the JPanel efficiently?
if I do not, then the Flow Layout organizes them horizontally
Maybe you should be using a GridLayout.
If not then you can try the Wrap Layout which is a FlowLayout that wraps to a new line as required.
If you are looking for a different layout, why don't you try MiGLayout? It is a very powerful and flexible layout manager.
See a detailed example here.
From that link, rewrite the initUI method to see the behavoir interesting to your problem:
public void initUI() {
this.setPreferredSize(new Dimension(100, 100));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setJMenuBar(initMenuBar());
this.getContentPane().add(new JScrollPane(mainPanel = initMainPanel()));
this.setLocationByPlatform(true);
this.pack();
}

Netbeans null layout causing border around background when resizable unchecked

I'm making a simple JFrame with the GUI editor in netbeans with a background image set as an icon in a label as suggested by the netbeans site, with a label and a button centered. I was having a very hard time centering them without using the null layout and setting the pixels to center them. I have an 800X600 image as the background, and I don't want the window to be resizeable. So I unchecked resizeable in the properties, and on the code tab I have designer size set to 800, 600, generate size is checked, and the form size automatically sets to 816, 638. This then gives me a border around the right and bottom sides of a few pixels. If I change the Form Size to 800, 600, then the background image is cut off by a few pixels. One other thing that I set that may impact that is in the properties=>bounds set to 800, 600, 800, 600.
Any advice on how to get rid of the border without allowing the window to be resizeable as well as any on whether a different layout can help with centering would be greatly appreciated. I did find some information that Grid Bag layout would help, but I wasn't quite able to get it working correctly. I suppose that writing out the code instead of using the GUI editor may also be a better alternative, but I'm pretty new so any advice on that would be great as well.
Don't use null layout when you can center components quite easily if you use the correct layout or combination of layouts. For instance if you want a JLabel next to a JButton and have them centered in a JPanel, put the JLabel and JButton into their own JPanel first (make sure to have this JPanel's opaque property set to false) and then have the containing JPanel use GridBagLayout. If you add one component (the inner JPanel) without GridBagConstraints, the component is centered automatically, even if the containing JPanel is resized. It's almost idiot-proof, whereas null layout is a recipe for difficult hard to maintain code.

Resizing a JPanel inside of a JScrollPane clears the things I've drawn on the JPanel

I've got a JPanel inside a JScrollPane. I draw things in the JPanel, and at some point I might draw past the width of the JScrollPane. In this case, I'd like the horizontal scroll bar to appear, and I'd like to be able to scroll around to view different parts of the JPanel. However, I end up clearing the JScrollPane.
frame = new JFrame();
frame.setBounds(100, 100, 1000, 800);
localScrollPane = new JScrollPane();
localScrollPane.setBounds(768, 6, 226, 350);
frame.getContentPane().add(localScrollPane);
localView = new JPanel();
localScrollPane.setViewportView(localView);
drawSomeThings(localView.getGraphics());
// wait for user input
int newWidth = drawThingsPastTheWidth(localView.getGraphics());
// these next two lines clear it
localView.setPreferredSize(new Dimension(newWidth, localView.getHeight()));
localView.revalidate();
What am I doing wrong? Thanks!
drawSomeThings(localView.getGraphics());
Don't use the getGraphics() method to do painting. The painting will be lost the next time Swing determines the components needs to be repainted.
Instead custom painting is done by overriding the paintComponent() method of your component.
Do not use setPreferredSize method, here's a related thread.
Do not specify explicetly the size of the JScrollPane with setBounds. Let the LayoutManager of it's parent take care of this.
JScrollPane should use a
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED by default. So
the scrollbar should appear automatically when the preferred size of
the child component is higher than the displayed area. Try to
revalidate the JScrollPane instead of the JPanel.
It would appear that redraw is being called at some point, I've not used swing for a while so I'm not entirely sure what the problem is but try debugging and running through the code step by step to see where redraw is being called would be a good starting place.
you should certainly use repaint instead of revalidate. revalidate only marks all the container upto the top level as invalid.

Categories

Resources