JScrollPane in JPanel added to JTabbedPane - java

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.

Related

Java scrolling through jbuttons

Ok here is my problem...
I have this jframe with a jpanel inside. And in that jpanel there is about 30 jbuttons stacked under eachother. But only 17 can be veiwed at a time. Is there a way i can scroll down worts to see the rest?
Btw im not looking for JScrollPane
You need to use a scroll pane, that seems to be the best solution here!,if the size of the buttons exceeds the frame area ,do they ?

JScrollPane is changing it's size

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

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.

JPanel adjust to JScrollpane and children

I'm trying to make a ToDoManager in java. For now I have about what I want it to be for a basic version. But I'm having a problem with the size of a panel.
I have a main JFrame. This contains a JPanel, say jPanel1.
jPanel1 has 2 buttons (add and remove) and another JPanel (say jPanel2).
jPanel2 contains a JScrollPane, which contains a modified version of JTable.
The thing I want is to tell the JTable to stretch out, so i can view everything in the JTable, and then tell the JScrollPane and jPanel2 to "Pack", or resize, so the JTable is completely vissable (if not possible the JScrollPane should do its work and draw the scrollbars).
This is what I have got at the moment:
So maybe you can see 2 problems:
1) The horizontal scroll bar does not appear. (But I did set the scroll bar: HORIZONTAL_AS_NEEDED)
2) I did not set any preferred size for the main JFrame, nor for the jPanel1, but it packs always as the same size. So I would like to stretch the jPanel2 to the full JTable, and if that would exceed the screen size, draw the scroll bars.
Using another layout manager, it's a lot easier to comprehend the usage of the JPanels and this concludes the problem.

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.

Categories