I'm trying to make a multi-column JComboBox. I've looked around quite a bit and it seems to be a very tricky thing to do. Unless many people, I'm not interested in having a table (where you select a row): I need to eliminate the scroll bar in the JComboBox and, in order to achieve this, I want to lay its items in a multi-column list instead of having them in only one column.
My best bet so far was to do this:
JComboBox dropdown = new JComboBox(validValues);
CellRendererPane crp = (CellRendererPane) dropdown.getComponent(1);
crp.setLayout(new GridLayout(4, 4)); // for 16 items...
But it doesn't work. It still lays cells in a single column. I tried adding items after setting the LayoutManager, but it doesn't affect the result.
Anyone has a clue about how to achieve this?
So far, I've seen the ListCellRenderer as useless to play with. It only specifies how to draw a cell (one at a time), not how to lay all of them (what is their relative position to each other).
Any help is welcome!
Thanks!
MJ
A combobox uses a JList to render the items in a popup. By default each item is displayed in a single row. You can access this list directly using:
Object child = comboBox.getAccessibleContext().getAccessibleChild(0);
BasicComboPopup popup = (BasicComboPopup)child;
JList list = popup.getList();
Now that you have acess to the list you should be able to change the default display by using:
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
Hopefully the items will now wrap when the width of the dropdown is reached. The width of the dropdown is controlled by the width of the combo box so you may need to play with the width of the combo box by using:
list.setPrototypeDisplayValue(....);
Edit:
Actually, forget about using setPrototypeDisplayValue(...), I think you will need to manually set the size of the popup.
By default the width of the popup is always equal to the width of the combo box. You can modify this behaviour by using a PopupMenuListener to override the size of the popup. To get you started you can look at the Combo Box Popup entry. Your code will be much simpler since all you will need to do is hardcode the desired width of your popup.
Related
When I run the application and I click on the JCombobox for the first time, the drop-down list looks like this
The second time I click, it seems to rearrange everything and all items are shown.
Any idea why this may be happening?
I would appreciate your help.
SOLVED: I was adding the JComboBox instance to the panel before adding the items to the JComboBox.
Check the height property, may be it is collapsing with other component. Design and placement of the component should be correct in swing to behave properly.
I am making a dialog for the purpose of selecting multiple file paths. My dialog consists of two panels. One for buttons such as "Add" and "Remove", and a second panel containing a JTable wrapped in a scrollPane. The table has only one column. The cells of the table are not editable directly. When a user selects a file using a JFileChooser, the full path of that file will be added to the table. Although my dialog is resizeable, I still need a horizontal scroll behavior in the event that the file path is longer than the user's screen is wide.
I have researched the combination of resizeable table and horizontal scroll bar. That is similar, but not my issue. The typical scroll behavior is that the columns are scrolled, not the contents of the columns. I need the contents of a single column to scroll horizontally.
doesn't matter whether you scroll a multiple or only a single column: the basic issue is to get the horizontal scrollBar to start with :-)
There are two screws to tweak:
- enable horizontal scrolling by setting the table's resizeMode: default is to always fit the table's size to the size of the scrollPane, that is no scrolling
- resize the column width to fit its content
In a core JTable that maps into pseudo-code like
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// on receiving a TableModelEvent which might increase the column width
// calculate new width by measuring pref of the renderer
int newWidth = ...
// set it as pref of the column
table.getColumnModel().getColumn(0).setPreferredWidth(newWidth);
The catch is that without resizeMode, you are always responsible to sizing the column: it its width is less than the scrollPane, there's an empty region at its trailing side.
JXTable (part of SwingX project), supports an addition sizing mode which fills the available horizontal space as long as the table's prefWidts is less than parent width and shows a horizontal scrollBar if needed
table.setHorizontalScrollEnabled(true);
// on receiving a TableModelEvent which might increase the column width
// tell the table to re-evaluate
table.packColumn(0);
I selected kleopatra's answer as correct because it addresses my specific question regarding table manipulation. I am adding this answer because I ended up solving my root problem in a different manner.
I chose to use a JList to represent my file paths instead of a single column table. The only real reason that I had wanted to use the JTable was because of the appearance of the lined rows that a table has, and because of my unfamiliarity with JList. I discovered how to edit the appearance of the JList by extending the DefaultListCellRenderer. Because I now knew about editing the appearance, the JList's natural resizing and scroll behavior made it a much more natural fit to my needs.
I have a JCombobox and I'm adding some items at runtime. Some of them are very long and my Jcombobox is growing very long too. It's in a JPanel container with BoxLayout (PAGE_AXIS). I have no idea how to prevent growing...
I wonder there should be a property like overflow and in combobox item will be shown like "123..." if it's "123456789".
EDIT:
I read your answers, thank you very much. But the problem is that my JPanel can resize and I need to make it's children always fill parent. So I cant set preferred size because my JPanel's size can change at runtime.
And I also can't set maximum size or use setPrototypeDisplayValue() because of the same reason.
I tried to use other layouts (GroupLayout, BorderLayout) but the result is the same.
I can post code if you want but I think it's not necessary. Let me know if you need it anyway.
Just tried with JTextArea with single row instead of JCombobox. Result is the same. It's growing when I type.
I solved similair issue by overrode JComboBox.setPrototypeDisplayValue()
and there are two ways (as mentioned #Stas) resize JComboBox or its derived Popup or both together
You can try this :
myCombo.setPreferredSize(new Dimension(myCombo.getHeight(), myCombo.getWidth()))
There are multiple solutions for the problem. You can change layout to limit growing.
Or fix somehow max size of the combobox
Or add a custom renderer (extending default renderer) and set preferred size there.
My friend is facing an issue where in he has a Swing Dialog and it has several text fields, combo boxes and radio buttons. Also it has a JPanel which holds the search results if at all the user wants to perform any search.
It looks fine with normal font size. But once the font size is increased to say 150% or even 200%, then the text boxes are not growing and the text in them is growing. So, they are not fitting in and are getting clip-ed.
He managed to overcome this limitation by using the setPreferredSize method on the UI components. Now it seems that he is able to control the behaviour in case of text boxes, combo boxes etc. But the search panel is still an issue.
Could some one please point out what the issue could be?
UPDATE:
They also have a JTable where the search results are displayed. Now, the thing is, they are hardcoding the height of each row in that JTable using the call setRowHeight. And due to this, if the font size is increased, the row height still remains the same. Is there any method call that resolves this.
We honestly think that they should not have done that hardcoding. Is there any solution for this? Please share.
Thanks,
Pavan.
Which layout is your friend using? Choosing a suitable layout may help.
Try pack() it will automatically adjust the Window to fit the preferred size of the components.
I recently had a problem where I needed to have a field that can wrap the text and increase the height of the row as the text is wrapped, similar to Microsoft Excel. I managed to get it working correctly, the only problem is that the table contains multiple JComboBoxes. When the row's height increases from the field that wraps the text, the size of the JComboBox window and ArrowButton also increase. I am using a DefaultCellEditor for the JComboBox fields, and created my own Editor/Renderer to be used with the JTextArea field. Once the JComboBox's value is selected, the value gets displayed correctly in the field, the only problem is while I am selecting the value, the JComboBox window and ArrowButton could be HUGE depending on the size of the row. Is there any way to increase the height of the row, but have the JComboBox field height remain the same instead of growing to fill the column that it's in? I am thinking I might need to make a Custom Cell Editor for the JComboBox fields as well instead of using the default. Thanks in advance!
I am thinking I might need to make a
Custom Cell Editor for the JComboBox
fields as well instead of using the
default
That would probably be the solution since the size of the editor is determined by the size of the cell.
I would try using a JPanel with a BorderLayout as the editor component. Then you add your editor to the North of the panel.
It would be the easiest editor to create since all the mouse events and key events are passed to the editor I believe, which means the panel will get the events, not the combo box. So I guess you would need to forward these events to the combo box.
First, is the JComboBox in a BorderLayout and set to BorderLayout.CENTER?
If so, I'd change it to a different layout such as AbsoluteLayout so it does not stretch to fill the cell.
Also, I will also refer you to this post Putting JComboBox into JTable