Scroll bar covers last row of JList with VERTICAL_WRAP - java

I want to layout a JList on one line horizontally, and have it scroll only horizontally. I've found list.setLayoutOrientation(JList.VERTICAL_WRAP); which works well if there are few enough items. However, when the list needs to scroll, the scrollbar covers the last (and only) row of the list, so you can't see it at all. How can I prevent this?
My test code:
JList<String> list = new JList<>("TIVFBJPAVUOHCVINPNYLMSMNNDUSHVSWUYUSNZXTYTXJMJPTISAVVYHOPBFIAXSUUQYYPVGAKEEWOTRCBWQWRXQTYJLCTTHTXPMZWDLQRRUZJSVWDMLYNRUDZXRTEJWAZUOBQCWNCYEPVCPXVWOGVZPOEKPWZZFDGZZGXPBFZQQVKFIXCYFTHRPJJMOYISEUCUTJGZQI".split("[A-D]"));
list.setLayoutOrientation(JList.VERTICAL_WRAP);
list.setVisibleRowCount(1);
frame.add(new JScrollPane(list), BorderLayout.NORTH);
The result:

Force the scroll pane to include the horizontal scrollbar in its size calculation:
JList<String> list = new JList<String>( ... );
list.setLayoutOrientation(JList.VERTICAL_WRAP);
list.setVisibleRowCount(1);
JScrollPane scrollPane = new JScrollPane( list );
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.add( scrollPane );

Related

two synchronized scrollBars don't work

I need to do two element with one horizontal scroll bar. I did:
JTable myTable = new JTable(10, 5);
JTable myTable2 = new JTable(1, 5);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JScrollPane SP1 = new JScrollPane(myTable, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
SP1.setColumnHeaderView(myTable.getTableHeader());
JScrollPane SP2 = new JScrollPane(myTable2, ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
SP2.getHorizontalScrollBar().setModel(SP1.getHorizontalScrollBar().getModel());
panel.add(SP1, BorderLayout.CENTER);
panel.add(SP2, BorderLayout.SOUTH);
getContentPane.add(panel);
I need to scroll my SP2 together with the SP1.
But They don't. I can't see horizontal scrollBar after SP2 panel.
And no one element hasn't horizontal scrolling.
Please, help me to find what I missed.
I looked the example in that post How to scroll two or more JTables with a single Scrollbar?

how to add scrollbar to textarea in java

the jscrollpane that I am adding doesnt appearin my textarea
textArea = new JTextArea();
scroll = new JScrollPane(textArea);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.add(textArea);
this.add(scroll);
this.setSize(1000, 600);
this.setLayout(new BorderLayout());
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
textArea = new JTextArea();
scroll = new JScrollPane(textArea);
//this.add(textArea); // get rid of this
this.add(scroll);
You create the scrollpane with the text area, but then the next statement removes the text area from the scrollpane because a component can only have a single parent.
Get rid of that statement and just add the scrollpane to the frame.
Then scrollbars will appear automatically as you add data to the text area.
Also you should create the text area using something like:
textArea = new JTextArea(5, 20);
to give a suggestion on how big to make the text area.
I did what you said but still nothing happens
Another problem is that you need to set the layout manager BEFORE you start adding components to the frame (or panel).
Remove this.add(textArea); and add scroll.setSize( 100, 100 ); will also work for you.

JList not showing JScrollpane or changing size

I have the following code:
JList<Song> list = new JList<Song>(this.lib);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setLayoutOrientation(JList.VERTICAL);
list.setVisibleRowCount(3);
list.setSize(1, 1);
JScrollPane listScroller = new JScrollPane();
listScroller.setViewportView(list);
list.setSize(10, 10);
setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
add(list);
I am not sure what is wrong here. When I look at the JFrame, I see the list in a single box with all 9 items that I have in my list. I tried messing with the size to see if I could get that to work but it isn't. The size doesn't seem to change no matter what I set it to.
My goal is to have a vertical and horizontal scroll-bar when necessary and a JList that is of some fixed size (fixed compared to the frame size would be best if possible).
You're adding the list to the container, not the listScroller.
JScrollPane listScroller = new JScrollPane();
listScroller.setViewportView(list);
list.setSize(10, 10);
setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
add(list);
You should try using something like...
JScrollPane listScroller = new JScrollPane();
listScroller.setViewportView(list);
setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
//add(list);
add(listScroller);
I think you will also find list.setSize(10, 10); is pointless, as the JScrollPane (actually the viewport, but lets keep it simple) has it's own layout manager.
You should take a look at Jlist#setVisibleRowCount if you want to affect the number of visible rows a JList will show before it needs to be scrolled.
The actual width and height of each row is determined by the ListCellRenderer, but you can use JList#setPrototypeCellValue to affect how these might be calculated.

JList horizontal auto-scroll to right

I have a jlist that contains items written in arabic, and unfortunately if any item is long, all items disappear after loading, because the scroller doesn't autmaticly scroll to right, and I should manualy scroll the list horizontaly to right.
I have set the JList's componentOrientation to right.
Any idea?
Assuming the JList is in a JScrollPane, you can set the value of the horizontal scroll bar:
final int maximum = scrollPane.getHorizontalScrollBar().getMaximum();
scrollPane.getHorizontalScrollBar().setValue(maximum);
Like in the example here:
final JFrame frame = new JFrame("JList horizontal auto-scroll to right");
frame.setBounds(100, 100, 80, 600);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
final String[] model = {"aaa", "bbbb", "ccccccccccccccccccccccccc"};
final JList<String> jList = new JList<String>(model);
jList.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
final JScrollPane scrollPane = new JScrollPane(jList);
frame.getContentPane().add(scrollPane);
final int maximum = scrollPane.getHorizontalScrollBar().getMaximum();
scrollPane.getHorizontalScrollBar().setValue(maximum);
frame.setVisible(true);
For me, the window looks like this:
screenshot frame

Scrolling issue (Java-JFrame-JScrollBar)

INTRO:
I created a java application using JFrame. I have a JMenuBar at the top and under that I'd like to display rows of text.
PURPOSE:
When I have 50 rows and only 20 are displayable at once, I'd like to be able to scroll down and back up again.
PROBLEM:
Of course, my theory doesn't wanna work as it should. My problem is that I don't know how to add a vertical scroll properly.
QUESTION:
How should I change this code to reach my goal?
public void display(){
Container content = this.window.getContentPane();
content.setLayout(new BorderLayout());
Border border = LineBorder.createGrayLineBorder();
//this is just a sample
for(int i = 0;i<50;i++){
JLabel lab = new JLabel("lonyaladek");
lab.setSize(570, 20);
lab.setBorder(border);
lab.setLocation(10, 20+(i*25));
content.add(lab);
}
//scroll
JScrollBar sb = new JScrollBar(JScrollBar.VERTICAL, 0, 0, 0, 0);
content.add(sb);
}
First you need to start with a layout manager that allows you to add multiple components to the container. Maybe a GridLayout is the best place to start.
Then you add this container to the scrollPane and then you add the scrollpane to the window.
So the basic code would be:
JPanel panel = new JPanel( new GridLayout(0, 1) );
panel.add(...);
panel.add(...);
JScrollPane scrollPane = new JScrollPane( panel );
window.add(scrollPane, BorderLayout.CENTER);
I suggest you read the section from the Swing tutorial on How to Use Scroll Panes for more info.

Categories

Resources