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.
Related
I have 2 tables each are added for a scrollpane however the scrollbar only apperas in one of them
JScrollPane pane = new JScrollPane(table);
JScrollPane pane_2 = new JScrollPane(table_1);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table_1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
panel.add(pane_2);
panel.add(pane);
the scrollbar only appears in pane_2 and not in pane
Any ideas how to fix this ?
Possible reason of such behavior could be different size of the tables. According to my experience JScrollPane shows the scrollbar only in case that its content is bigger and does not fit within the pane.
You can try to experiment with following JScrollPane properties:
horizontalScrollBarPolicy
verticalScrollBarPolicy
They can contain following values (e.g. in vertical case):
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED
ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
They should control the behavior you ask about. The names are self explanatory I think.
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.
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.
I want to add different buttons, vertically stacked, to a JPanel at run-time and use a JScrollPane so that all buttons will be visible (with some scrolling).
In order to do this, I have added my JPanel to a JScrollPane, after which I add buttons to my JPanel.
However, when I do this the vertical scrollbar does not allow me to see all images. For example when I add 7 buttons I can only scroll to see 5 full images and half of the 6 images.
Why doesn't my scrollbar allow me to display all 7 buttons?
Create the panel and scrollpane like:
JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane( panel );
When you add buttons to the panel at run time the code should be:
panel.add( button );
panel.revalidate();
As long as you are using a layout manager the preferred size will be recalculated and the scrollbar will appear.
Make scroll pane a wrapper over your panel - new JScrollPane (myPanel) and add it instead of naked panel in your panel's container.
You also may want to play with its setPreferredSize() method.
I need to create a panel where I can put some rectangles and it automatically reorder just inserting a scrollbar and growing up vertically. Also this panel can be resizable and again the rectangles must to be reordered to correctly be displayed inside the panel.
If I understand the question you want components to wrap to the next line so that the panel grows vertically while the width remains fixed.
If so then check out the WrapLayout
Note: the FlowLayout already supports the wrapping of components to a new row on the panel. This issue is that the preferred size calculation assumes all components are placed on a single row. The WrapLayout overrides the preferred size calculation to support the wrapping of components on a new row.
Use a JScrollPane. If you never want a horizontal scroll bar you can add the following:
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
(By default the scroll pane will add horizontal and vertical scroll bars when required.)
The scroll pane itself will only be resizeable if you add it to a Container with the appropriate layout manager; e.g.
JFrame frm = new JFrame();
frm.setLayout(new BorderLayout());
JScrollPane sp = new JScrollPane();
frm.add(sp, BorderLayout.CENTER); // Adding a component to the CENTER will cause the component to grow as the frame is resized.