JScrollPane is changing it's size - java

I so my problem is this:
I made a JPanel. Inside it I want to add a JList with a scroll-bar. So I use JScrollPane.
Here you can see a picture of the application. The big white recangle is the size of the JPanel. In the second picture you see what happens when I add the Scrollpane to the code.
Here is the code I use:
public class GUI extends JFrame{
DefaultListModel m = new DefaultListModel();
JList myList = new JList(m);
JScrollPane scrollPane = new JScrollPane(myList);
JPanel listPanel = new JPanel();
public GUI(){
//Stuff about Frame Size, title, and other boring things
scrollPane.setViewportView(myList);
listPanel.add(scrollPane);
this.add(listPanel);
}
}
I have used it before, and it worked. Well, I faced the same problem, but it went away. I have written this code the same way as I did the previous time. But this time it doesn't work.
thanks in advance guys.

With JList you can simply use setVisibleRowCount to adjust the size of the viewable area of the JScrollPane
The other problem is JPanel uses a FlowLayout by default, you may want to change the use something like BorderLayout instead
Updated
As pointed out, if the list contains a large number of elements, you can use setPrototypeValue to improve the efficiency

Okay, so apparently I have to setPreferredSize. You can't just set the size of the panel, you need to set the size of the jscrolpane too!

By default, unless you use something like a grid bag layout...
Also, for developing UI's in Java, it is extremely helpful to install an eclipse plugin called WindowBuilder

Related

JScrollPane in JPanel added to JTabbedPane

I'm currently trying to get into build GUIs with the GridBagLayout in Swift.
When trying to make a GUI for a project I'm working on I ran into a problem:
When creating a list of things, the Panel exceeds my screen height, so I set the frame's preferred Size to 900 pixels.
However, the list exceeds 900 pixels in height. So I Tried adding a JScrollPane to the Panel that conaints the list. There just isn't one.
My Frame architecture is something like this:
Frame f -> JTabbedPane Wrapper -> JPanel p
I tied applying a ScrollPane to either one of those Objects, none worked. Any tips?
So, I just want to let you know, I'm new to Java and just noticed my mistake.
In the function that created the JPanel, I still returned the JPanel instead of the ScrollPane.
Anways, thanks for helping everyone.

Resizing a JFrame to fit added components

I'm making a program for fun, it's basically a computer navigation GUI, details not required :)
Anyway, so far, I have a button called "new button" that, when clicked, it creates a new button named "test", to an infinite amount. Right now, i have my GUI set up like this:
Class Main extends JPanel (the main panel that holds everything in it, size set as)
Dimension size = new Dimension(300, 200);
setPreferredSize(size);
JFrame holding the Main JPanel, called like:
panel.frame = new JFrame();
panel.frame.setResizable(false);
panel.frame.setTitle(panel.title);
panel.frame.add(panel);
panel.frame.pack();
panel.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.frame.setLocationRelativeTo(null);
panel.frame.setVisible(true);
So, how would i have the JFrame/JPanel set their size based on the components inside it? I've tried to use panel.frame.pack(); but i get an error most of the time, and the other times it doesnt wrap, it is just a staight line. I want it to resize in the form of a square. Any ideas? Sorry if my question isnt clear/poorly phrased, i've always had issues articulating questions online, much better in person cause i can use my hands! :) Thanks in advance!
Class Main extends JPanel (the main panel that holds everything in it, size set as) Dimension size = new Dimension(300, 200); setPreferredSize(size);
Don't set the preferred size of the panel. The layout manager will determine the preferred size based on the components that you add to the panel.
and the other times it doesnt wrap,
The default layout manager for a JPanel is a FlowLayout. It is not designed to wrap automatically. Maybe use a different layout manager. Or you can try the Wrap Layout which extends FlowLayout to provide dynamic wrapping.
I've tried to use panel.frame.pack(); but i get an error most of the time
What error. I've never seen an error when using the pack() method.
Post a proper SSCCE if you need more help.

How to keep a swing component updated

One of my classes is returning a JPanel which is added on a JFrame by some other class.
The JPanel contains a JTree and some buttons. On some events the panel is created again and returned to the JFrame.
My problem is that I have to add the JPanel to the Container of the JFrame again and then resize the frame for changes to be visible. I can't figure how to have the frame update without resizing.I tried removing old objects and adding updated ones but still doesn't work.
What is the best way to deal with this issue? Ideally I would have a reference to the JPanel and when the JPanel is changed, the frame will also be updated.
The whole model is changing not just its data. I will probably change this in the future but for now when data change a new JTree is created
Then your code should be something like:
JTree tree = new JTree( theNewModel );
scrollPane.setViewportView( tree );
That is you need to add the new JTree to the GUI, you can't just change the reference to the tree variable.
Or even easier, you don't need to create a new JTree, just replace the model in the existing tree using:
tree.setModel( theNewModel );
If this still doesn't help then you need to post your SSCCE that demonstrates the problem because your question still isn't clear.
try JFrame.invalidate() first, then call JFrame.validate()
http://docs.oracle.com/javase/6/docs/api/java/awt/Container.html#invalidate%28%29

Java: JScrollPane didn't show scroll bars

I am developing a small desktop application in Java using Netbeans. On my jframe i have various pannels and one scroll panes. The purpose of this JScrollPane is to show some visual elements to its users. I achieve this by following the below steps in sequence:
Drag and drop JScrollPane at desired location of my JFrame
Adjust the size of JScrollPane according to my needs.
Write a new java class and extend that class with JPanel
Override the public void paintComponent(Graphics g) method
Then i add that panel to above JScrollPane,
using following code:
JPanel jpnl = new myClass();
jScrollPane2.setViewportView(jpnl);
jScrollPane2.repaint();
Now every thing is working fine as per my requirements, the only thing which is lacking is that when my drwaing is big then no sroll bars are shown at JScrollPane. This is my first application and i don't know much about Java, so any guidence regarding what is missing would be highly appreciated
Remember to add the required component to the JScrollPane object, and the scroll pane object to the panel. Also, it could be that you need to change the scroll bar policy: use scroll pane's setHorizontalScrollBarPolicy() and setVerticalScrollBarPolicy().
Consult the JScrollPane documentation for these methods.

Java Swing Scrollpane in NetBeans

I have Java application which adds JTextFields # runtime to JPanel. Basically user clicks a button and new JTextField is added, clicks again added again...
Each new JTextField is directly below the previous one. Obviously I run out of space pretty soon so I'm trying to use JScrollPane and thats where the hell begins, because it just doesnt work no matter what I try.
Right click on JPanel and Enclose in Scroll Pane. Didnt work.
After reading some examples I realized I must have JPanel as an argument for JScrollPane constructor. Which I did via right clicking on ScrollPane and CustomizeCode. Because apparently auto-generated code is protected in NetBeans and I cannot just change all those declarations, etc. manually. Still doesnt work.
I did try to set PreferedSize to null for JPanel and/or JScrollPane, didnt help.
JScrollPane is a child of lets call it TabJPanel (which in turn is a tab of TabbedPane). I tried to mess with their relationships, basically trying every possible way of parentship between JFrame, JPanel(holding textfields), TabJPanel and JScrollPane, but nothing worked.
I also made VerticalScrollBar "always visible" just in a case. So I see the scrollbar, it's just that populating that JPanel with JTextFields does not affect it.
When there are too many JTextFields I they go "below" the bottom border of JPanel and I cannot see them anymore.
Code for adding new JTextFields is like this, in a case it's relevant.
JTextField newField = new JTextField( columns );
Rectangle coordinates = previousTextField.getBounds();
newField.setBounds(coordinates.x , coordinates.y + 50, coordinates.width, coordinates.height);
JPanel.add(newField);
JPanel.revalidate();
JPanel.repaint();
Sorry for a long post I'm just trying to provide as much info as possible, because being newbie I dont know whats exactly relevant and whats not. Thanks in advance :)
As there is another answer now, I'm adding my suggestion too.
This sounds exactly like a problem to use a JTable with a single column. JList is not yet editable (and might never be).
JTable would handle the layout problems for you, and you can easily access the values via the table.
Use your own TableModel (a simple Vector should be sufficient in your case), and add values to it.
An option you have is to utilize a LayoutManager, instead of setting the bounds directly on the components. To test this, a simple single column GridLayout with the alignment set to vertical should prove the concept.
panel.setLayout(new GridLayout(0,1));
zero in the rows param allows for rows to be added to the layout as needed.
I do this way to add a scrollpane, create a panel and fill it with few components, then create a scrollpane in the component you want to add it, cut and paste the panel in which all your details will fall in and resize the scrollpane.Because the components take a larger space than the one visible right click on the scrollpane and select design this container, there you can increase the size of the scrollpane and add as many components as you have.

Categories