I am trying to resize the widths of the columns in a JTable.
I would need to resize them when the program is running to see the full column header names.
I want to resize table of database showing in swing frame.
I have tried
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS)
But I didn't get the desired output because when I use this I have a horizontal scrollbar to scroll to the columns that are off screen. But I want the size of the whole table to become approximately equal to the size of frame.
How can I make the columns as wide as they need to be to display all the text?
But I want the size of the whole table to become approximately equal to the size of frame.
JTable table = ...;
JFrame frame = ...;
JPanel contentPane = new JPanel( new BorderLayout() );
contentPane.add( new JScrollPane( table ), BorderLayout.CENTER );
frame.setContentPane( contentPane );
How can I make the columns as wide as they need to be to display all the text?
Unfortunately, the maximum width of the JTable will be limited to the size of the JFrame, and there is no mechanism to push from your JTable the desired width to the JFrame when your JTable is contained in a JScrollPane.
What you could do is try to give your JScrollPane the correct width. I haven't tried this, but I suspect that giving your scrollpane the same width as the preferred width of your table (when autoresize is on) should solve this (+ perhaps some extra width for the vertical scrollbar). Note however that this will only work if you allow your JFrame in which the JScrollPane is contained to grow in width as well.
Related
I need a vertical scrollbar in my JTextArea to only appear when it's needed, and for that I know I need to use Vertical Scrollbar As Needed. But the scrollbar keeps showing up, like this:
enter image description here
even when it's clearly not needed. What am I doing wrong?
// make top panel where output from the menu selections will appear
topP = new JPanel(new BorderLayout());
topP.setSize(new Dimension(500,150));
// make default text message to be displayed in top panel
output = new JTextArea("Output printed here...", 20, 20);
// styles the text in the textarea
output.setForeground(Color.BLUE);
output.setFont(new Font("Times New Roman", Font.BOLD, 20));
topP.add(output, BorderLayout.NORTH); // add default text to the top panel
right.add(output);
// here's the scrollbar guys
top = new JScrollPane(output); // applies to the textarea
top.setPreferredSize(new Dimension(500,150));
top.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
top.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
topP.add(top, BorderLayout.NORTH);
Take a look at the documentation for JTextArea. If you scroll down to the constructor summary section, you'll see the following:
JTextArea​(String text, int rows, int columns)
Constructs a new TextArea with the specified text and number of rows and columns.
This is the same as the constructor you're using:
output = new JTextArea("Output printed here...", 20, 20);
By specifying rows and columns, you're effectively telling the JTextArea that it has to be a certain size, and the scrollpane is just responding to your specifications by showing scroll controls. Those scroll controls are displayed based on the number of rows and columns you asked for, as opposed to the amount of visible text within the text area.
If you construct a JTextArea without specifying rows and columns, there won't be any scrollbars. Since you're setting the size of the text area, specifying rows and columns is redundant anyhow. Just don't do it.
Note: You should avoid setting the absolute size of Swing components. Set a preferred size only, and when you call pack(), Swing will try to accommodate your specification as much as possible. When you specify absolute sizes, your UI can look really bad on different platforms or when a user resizes the frame.
I have a Panel inside which I add a scroll table. I want this scroll table's header to be resizable (all columns should be able to resize and if table width exceeds panel width then a horizontal scroll bar should be displayed). Also if I resize the panel, scroll table should also be resized to fit the with of panel.
If I use AUTO_RESIZE_OFF for the table, I am able to resize header (columns) and a horizontal scroll bar is displayed but with this scroll table is not fit to panel's size when I resize the panel.
Adding a simple code base:
JPanel panel = new JPanel();
JTable table = new JTable();
table.getTableHeader().setResizingAllowed(true);
table.setAutoResizeMode(Auto_Resize_Off) // this is done to show horizontal scroll bar when table's columns are resized
panel.add(table);
This allows me to resize my table's columns a display a horizontal scroll bar if table width is more than panel's width.
But when I resize the panel, it is not resizing the table also which I need.
I'm using Netbeans graphical JFrame design. So I have a jList inside a jScrollPane (from the designer) and during the program I add some long strings (file paths) to the list.
Some strings are too long to fit in the list's width so the jList expands horizontally to fit the longest item. However, I want the width to be fixed and have a horizontal scrollbar at the bottom.
How can I do this?
You can use:
JList list = new JList(...);
list.setPrototypeCellValue("XXXXXXXXXXXXXXXXXXXX");
JScrollPane scrollPane = new JScrollPane( list );
panel.add( scrollPane );
to control the preferred width. The scrollbar will appear as required.
I have a JXTable that is created like this:
JScrollPane scrollPane = new JScrollPane();
contentPanel.add(scrollPane, BorderLayout.CENTER);
tbl = new JXTable();
tbl.setColumnSelectionAllowed(true);
tbl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tbl.setEditable(false);
scrollPane.setViewportView(tbl);
The problem is that when it gets filled, all the columns are squashed to fit into the table. I expected the columns to be adjusted according to the width of the column header or column value (which ever is broader) and then for a horizontal scroll bar to be displayed that will enable me to scroll left or right.
How do I bridge this problem?
You should use the packAll method: this resizes columns to fit the viewport; if horizontal scrolling is enabled, columns will get their preferred width.
I created a form. Actually it is 10 JLabels with each JLabel having a text field next to it.
consider,
JLabel_called_Name JTextField_to_obtain_name
JLabel_called_Phone JTextField_to_obtain_phone_number
and so on..
I usually position this in a JPanel and display it in a frame. But my panel and frame have height smaller than the size required to hold 10 of these Labels and Textfields.
So I wish to add them to a JScrollPane.
But in every question I only obtained information of how to add Jlabels to a scroll pane using a Box,
or adding JLabels to a JList.
However I would like to represent it in the format I showed above. A Jlabel beside a JTextField.
How can one acheive this?
But in every question I only obtained information of how to add Jlabels to a scroll pane using a Box, or adding JLabels to a JList.
You can add any component to a JScrollPane:
JPanel = new JPanel();
panel.add( label1 );
panel.add( textField1 );
JScrollPane scrollPane = new JScrollPane( panel );
The trick is choosing the correct layout manager for you panel. Read the Swing tutorial on Layout Managers to help you decide how to design the panel. You can also nest panels to get your desired layout.