Resize JTable/Scrollpane - java

It's actually an question about this. How do I resize this now? I used this, I was able to resize my JPanel, but I can't find how to resize the table or scrollpane. I have 10 columns and the table just stays the same size.
Thanks in advance

I was able to resize my JPanel, but I can't find how to resize the table or scrollpane.
The problem is that the JPanel uses a FlowLayout. A FlowLayout will respect the preferred size of any component added to the panel. So in the example code the table is displayed at its default preferred size.
A couple of solutions:
There is no need for the panel. Just add the scrollpane directly to the frame. The table will grow/shrink as the frame size changes.
Change the layout manager of the panel to one that will allow components added to it to grow to fill the available space. Again the table will grow/shrink as required.
Override the getPreferredScrollableViewportSize(...) method of the JTable. This will override the default preferred size of the table. The table may grow/shrink depending of the layout manager used.

Related

Make a panel resize dynamically in Swing

I'm creating an interface composed out of a box layout which contains panels inside every space.
In this specific case I've got a "cascade" of panels, the first is supposed to be a panel with a FlowLayout as layout manager, underneath it there's a GridLayout and under it there's supposed to be another label.
The thing is that I'd need the first panel to dynamically resize as the window get resized itself.
Here's the problem: I need the first panel to have a specific size in relation to the absolute size.. the thing is that I can't set my preferred size.. in the class I do the following but the panel stays the exact same size as before..
this.setPreferredSize(new Dimension(windowWidth, 50))
I'll send the code as soon as I get home, for now the situation is the one written above.
I need the first panel to have a specific size in relation to the absolute size
Your main panel should be a BorderLayout (which is the default layout of a JFrame), not a BoxLayout.
Then you add the panel that needs to be a specific size to the BorderLayout.NORTH of the frame.
Then your second panel can use a BoxLayout and add this panel to BorderLayout.CENTER of the frame. Now this panel will get all the extra space as the frame is resize.

Set JFrame's components' size relative to window size

I have a resizable JFrame and (since I do not know much about Layouts yet) set its JPanel Layout to null.
Is there a way I can tell my program to resize each of its components' size relative to the window's size?
e.g. I have a 200x100 default sized JFrame, on which a button is 20x10. If I resize the window to 400x200, I want the button to be (still on the same position relative to the window's edges) resized to 40x20.
If that works is there a way to do the same thing with fonts? E.g. size 11 on 200x100 but something like size 14 on 400x200.
Regardless of what convention you use to scale your components and font, it is not a good idea to set a layout to null. Widgets behaving strangely when using "setlayout(null)" will give insight to why this is error prone. Automatically size JPanel inside JFrame might help with scaling when you change the size of the JFrame manually.
Add a ComponentListener to your frame and create a table with the defaultSizes of the Components for a specific windowsize. The componentResized(ComponentEvent e) will be called each time the window is resized. Inside it, you can resize the components. Or you implement your own LayoutManager.

Cosmetics with GridLayout

I'm posting this question because I'm new to programming at the present time and I have a pet peeve that when I create the app I don't want the objects to go across the entire window.
I use GridLayout the most often and I was wondering if there was a way to make components such as a JTextField or JTextArea NOT span the entire window, leave a little space on both ends?
You can add a component to a JPanel, which uses a FlowLayout by default and all components are displayed at their preferred sizes. Then add the panel to the layout using the GridLayout. The panel will increase in size but the components on the panel will stay at their preferred size.

making a jtable bigger to fit all data

I currently have a JTable inside a JscrollPane inside a JPanel. No matter what I set the size of either of those 3 elements, the table always shows up as the same size. The JPanel is in BorderLayout and I am packing the frame. the reason I want to do this is because some of the data in my columns doesn't fit without having to make other columns way too small.
override table.getPreferredScrollableViewportSize(new Dimension(int, int));
change JPanels default LayoutManager FlowLayout (implemented in API) to BorderLayout or GridLayout
You can change the JScroller preferred size because a table is often put inside of a scroller.
scrollerPhone.setPreferredSize(new Dimension(600, 400));

Auto-resizable JFrame Container

How to make my JFrame auto-resizable? Size depends on existing components in the frame, but the user can add more components dynamically.
You could use the JFrame.pack() method, that according documentation:
Causes this Window to be sized to fit the preferred size and layouts
of its subcomponents.
See the Nested Layout Example for both dynamically added components (labels added using a button - in a scroll pane), as well as a resizable GUI.

Categories