Jtable, JScrollPane, JFrame issues - java

I am new to Java AWT. I want to draw a treetable (i.e. table with expandable rows) inside a JScrollpane which in turn is inside a JFrame.
By default I want to set the size of the frame to some fixed size and table inside should occupy the frame completely. This is because only when I set the frame to certain size, all cells of the table are rendered sufficiently large enough and there is no need to expand the frame to see data inside cells.
But I am not able to do this. Mostly, the table occupies only a small portion of the frame and about half of the frame remains empty. Any help will be highly appreciated. Thanks!

assuming your JFrame is named frame and your table is called table, try using this:
frame.setSize(table.getPreferredSize());
frame.pack();

Related

Frame and components in java

I know frame is act as container object that we can add components , there are many of components in swing package ( like button , menu and table etc...)
I have tried to add table to frame but I could not , why ??
unless I have add scroll pane object to frame then add table to scroll pane so what is difference between frame and scroll pane ? why I cannot add table directly to frame and what are real benefits of scroll pane for table
in this code , the table is shown with very small size and in corner and when add table directly to frame I cannot see any thing
Test t1=new Test(Test.Accounts());
ScrollPane scroll=new ScrollPane();
scroll.add(t1);
add(scroll);
note Test class is act as table class because I have extend it with JTable class and this code inside JFrame constructor
You have to tell Java how large you want the Table to be. You can do so by either set the size manually with t1.setBounds(x, y, width, height), or by using a layout. If you want to set the bounds manually you have to check that no layout is being used. You can do so by setting the layout to null: frame.setLayout(null);.
You can set the container's layout by frame.setLayout(new BorderLayout()); where in this case I'm using a BorderLayout (https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html). Now you can add the table and tell Java to fill the whole frame by typing frame.add(t1, BorderLayout.CENTER). CENTER can be replaced by NORTH, EAST, SOUTH, WEST depending on where you want the table to be.

Scrollable JTextArea that adjusts its size on resize

I've a JFrame with multiple panels. One of those should contain a JTextArea of a given size, that can scroll if the text exceeds the area, and that resizes acording to the frame size.
The thing is that when the panel is an instance of JPanel, it looks as I'd like to both in window or fullscreen size, as long as the content doesn't exceed the current area, when it does, it takes the space given to the other components, making them shrink in the frame (here it should show scrollbars instead).
On the other hand, when I use a JScrollPane, I can't manage to make it have the same behaviour of the JPanel, keeping the proper size. Actually it seem to adjust to it's content, and since it's empty, it's just some pixels wide.
How can I achieve what I'm looking for?
Thanks in advance
Following the code that keeps the right size (JPanel):
add(new JPanel(){
{
input = new JTextArea(20,20);
input.setLineWrap(true);
input.setBackground(Color.WHITE);
input.setBorder(cb);
this.setLayout(new BorderLayout());
//this.setPreferredSize(new Dimension(20,20));
this.setBorder(eb);
this.add(input);
}
});
Don't add the text area to the frame.
this.add(input);
Instead you need to add a JScrollPane containing the text area to the frame:
this.add(new JScrollPane(input));

Java Swing, making a gridlayout fill parent?

I just recently started using Swing to create GUIs for programs, and it's been pretty fun to mess around with so far. However, I'm having an issue with a JPanel with the layout set to gridLayout. Right now it looks like this:
The grid on the right is a JPanel set to a GridLayout, with each cell being a bordered JLabel. The options on the left are also inside a JPanel, and the left JPanel and right JPanel are nested in a GridBagLayout set on a JFrame.
Essentially, my problem is that I want to "scale" the grid on the right so that each cell is a certain height and width. The grid itself will have a variable number of rows and columns, which are set when the program first starts up. Eventually, I plan to have the right JPanel in a JScrollPane (if that's how that works...), so I'm not really concerned about whether or not all of the grid shows up onScreen.
I tried setting the fill value for the gridLayout to "BOTH" and it gave me the following result:
This is closer to my intention, but I wanted the actual ImageIcon in the JLabels to fill the entire JLabel. Additionally, I would want the JLabels to be the same height and width. However, I don't know exactly how to do that. I've been messing around with it for a while now, and I'm not sure if I'm just too much of a noob with Swing, or if I'm missing something in the documentation.
In the end, I'd like the grid cells to be a fixed height and width, no matter the number of cells, and no matter whether it goes offscreen or doesn't fill it.
(Also, I just thought, maybe it's not the best idea to code this and then shove it in a JScrollPane later and expect it to perform the same.... I guess I'll just see what happens.)
but I wanted the actual ImageIcon in the JLabels to fill the entire JLabel.
Check out Darryl's Stretch Icon which will allow the icon to resize to file the space available for the JLabel.

How to stick down border of JScrollPane with the Jframe or JPanel?

I mean when I resize the frame to the left or right side the table automatically resizes itself but I want the same future when I resize it from down or up sides of the table. There is scroll for down and up but I want default size to be smaller.
In fact aJScrollPane.setSize() doesn't work, aJPanel.setSize() too. Can you help me?
If you are trying to restrict the number of rows the table displays by default, you can use JTable.setPreferredScrollableViewportSize(). For example for 10 rows you can do the following:
table.setPreferredScrollableViewportSize(
new Dimension(table.getPreferredScrollableViewportSize().width,
10 * table.getRowHeight()));
Note however, table rows can have variable heights. Also note, that hard-coding sizes in general can have side effects.

Java's FlowLayout does not resize correctly

I created a JFrame initialized with a BorderLayout and a JScrollPane as its CENTER element.
The scroll pane is set with VERTICAL_SCROLLBAR_ALWAYS and HORIZONTAL_SCROLLBAR_NEVER policies. The intent of my frame is to have a controlled width, while the height should grow/shrink as data is added/removed.
Inside my scroll pane, I added a simple JPanel (lets call it the content panel) which is initialized with a FlowLayout (and LEADING policy).
In order to test this, I simply populate my content panel with 20 JLabel("Item " + n) components where n is the loop counter.
I would expect to see my labels shown on a single row if the frame is large enough and the labels wrap to other lines when I shrink the width. But instead, there is only a single line displayed with no wrapping... ever.
Does anyone know why the flow layout does not wrap when a scroll pane is involved?
If I remove the scroll pane all together and put the content panel directly in the frame, the desired wrapping effect occurs, but if the frame height is shrunk smaller than the content panel height it just disappears.
The idea is that I want my labels to be wrapped when necessary but also always be visible if it means having to scroll up/down.
Any suggestions on how to fix this?
Thanks.
Wrap Layout gives an explanation and a solution.
If you work with the designer, you have to set the prefferedSize property to null (delete what is set) then set the preferred size by clicking the triple dots [...] button next to the prefferedsize property name and put your preferred value.
I encountered the same problem and it works for me.

Categories

Resources