Java JComboBox control scrollbar position? - java

I'm working on a simple java program (using NetBeans' swing GUI builder). You're supposed to select a randomly placed item from the JComboBox as fast as you can, then it will tell you your time and save it (keeping statistics and whatnot).
Problem is, once you select an item and go back to find a different item (in a new random position in the JComboBox), the scrollbar seems to have remembered it's previous position.
Since this game is supposed to be competitive, it wouldn't really be fair for the scrollbar to be in the previous player's position.
Is there any way to set the position of the scrollbar in a JComboBox?
P.S. it's a vertical scrollbar. Also, I did try to find the answer online, but couldn't find it after over two hours of searching. Any help appreciated!

Select the first item so the scrollbar goes back to the top, then set the selection to -1.
comboBox.setSelectedIndex(0);
comboBox.setSelectedIndex(-1);

Related

How can I let user rearrange Jbuttons or Jtexfields with drag and drop?

I've made an equation builder program. it displays the terms with textboxes and buttons in a single line. each control is a term 3 * 3 is 2 textboxes for the numbers and a button for the *. I would like drag and drop to be implemented for changing the order of these. I'm just missing on crucial part I need to know how to detect where the control is dropped, either on the jpanel they are in or if dropped on a jbutton which physical part of the jbutton. Basically I just need to know whether to put it on the left or the right.
I currently have it kind of working where when you start to drag I make little catcher buttons appear in between each control but honestly it looks like crap and doesn't work that well.

JCombobox drop-down list is not large enough to show all items

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.

The scroll bar moves after data refreshing

I have a JScrollPane with JPanel on it. I have also many different components on JPanel (for example Labels, JTextFields, JTextAreas). These components are added programmatically (at runtime, on the user request).
JPanel uses SpringLayout. The program calculates preferred size of JPanel after adding components, because scrolling doesn't function properly without calculating. Adding components and calculating preferred size are part of my prepareGui() method.
The data displayed on components are periodically refreshed (in my refreshData() method, which is invoked by listener).
My problem: after refreshing, scroll bar always sets to fixed position (I don't know why). I want the scroll bar to stay in old position after refreshing.
I tried to find a place, where the scroll bar moves. I checked some values at the start and at the end of refreshData() method, but they was the same (they didn't change inside of refreshData()):
scrollPane.getVisibleRect().getX()
scrollPane.getVisibleRect().getY()
scrollPane.getVisibleRect().getWidth()
scrollPane.getVisibleRect().getHeight()
panel.getVisibleRect().getX()
panel.getVisibleRect().getY()
panel.getVisibleRect().getWidth()
panel.getVisibleRect().getHeight()
scrollPane.getHorizontalScrollBar().getX()
scrollPane.getHorizontalScrollBar().getY()
scrollPane.getHorizontalScrollBar().getWidth()
scrollPane.getHorizontalScrollBar().getHeight()
scrollPane.getHorizontalScrollBar().getValue()
scrollPane.getVerticalScrollBar().getX()
scrollPane.getVerticalScrollBar().getY()
scrollPane.getVerticalScrollBar().getWidth()
scrollPane.getVerticalScrollBar().getHeight()
scrollPane.getVerticalScrollBar().getValue()
Changing of layout manager didn't take effect.
I noticed one thing (I don't know if it has any meaning). When I set only preferred size (in prepareGui()), the scrollbar moves to the end. When I set also size, the scrollbar moves to strange, fixed position.
How can I prevent this?
The scroll bar should keep it's current position.
I finally found the answer here: https://stackoverflow.com/a/5230477/5694159 (thanks to Johan M) in the topic about JTextArea instead of JPanel.
When the refreshData() is calling in separate thread it works good.
I hope this question and answer will help someone.

appear and disappear for a scroll depending on a conditional

everyone, excuse my language I speak Spanish and I use google translator
I have a question I can appear and disappear as one scroll lacelda depending on size or table for example when the cell arrives at a height of "300" scrolling appears visible, if not reach 300 not appear
How I can do that?
regards
There is a Swing class called JScrollPane. There are two functions called:
scrollBar.getHorizontalScrollBar().getValue();
scrollBar.getVerticalScrollBar().getValue();
You'll either need an event listener or a loop in a separate thread to check if the scroll bar is past a certain point.
The code to make it disappear is just:
scrollBar.setVisible(false)
The code to make it reappear:
scrollBar.setVisible(true);
scrollBar.validate();
scrollBar.repaint();

How to apply another LayoutManager to a JComboBox? (multi-column JComboBox attempt)

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.

Categories