Adil
Adil

Reputation: 4623

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?

Upvotes: 0

Views: 1597

Answers (1)

Freek de Bruijn
Freek de Bruijn

Reputation: 3622

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

Upvotes: 1

Related Questions