Question about Jlist

Hi,

I first created a JScrollPane.

Then I put a Jlist on that JScrollPane.

Then I add 100 elements in the Jlist

DefaultListModel listModel = new DefaultListModel();

for ( int i = 0; i<100; i++){

listModel.addElement("test" + i);

}

jList1 = new JList(listModel);

When I run the program, it automatically shows the first 17 elements. When I try to scroll down, it still shows the 17th elements .

all are 'test 17' in the screen.

Anyone knows why ?

Many thanks in advance

Ivan

[568 byte] By [egckhada] at [2007-10-3 2:26:34]
# 1

> Anyone knows why ?

>

Difficult to say without seeing the rest of your code. Here is a short demo program that does what your program is supposed to do. Compare it with your program, and see what is different.

public class Test17 {

public static void main(String[] args) {

new Test17().run();

}

public void run() {

final JFrame f = createFrame();

EventQueue.invokeLater(new Runnable() {

public void run() {

f.pack();

f.setLocationRelativeTo(null);

f.setVisible(true);

}

});

}

private JFrame createFrame() {

JFrame f = new JFrame("Test 17");

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(createList(), BorderLayout.CENTER);

return f;

}

private JComponent createList() {

ListModel model = createListModel();

JList list = new JList(model);

return new JScrollPane(list);

}

private ListModel createListModel() {

DefaultListModel model = new DefaultListModel();

for (int i = 0; i < 100; ++i) {

model.addElement("test " + i);

}

return model;

}

}

Torgila at 2007-7-14 19:25:38 > top of Java-index,Java Essentials,New To Java...
# 2
The only thing that I could think of is that the JScrollPane isn't redrawing. Try creating a thread that just calls repaint().
kuriyama@woh.rr.coma at 2007-7-14 19:25:38 > top of Java-index,Java Essentials,New To Java...
# 3

> Then I put a Jlist on that JScrollPane.

I'm not sure what you mean by this.

From the JList API documentation: " JList doesn't support scrolling directly. To

create a scrolling list you make the JList the viewport view of a JScrollPane. For

example: JScrollPane scrollPane = new JScrollPane(dataList);

// Or in two steps:

JScrollPane scrollPane = new JScrollPane();

scrollPane.getViewport().setView(dataList);

..."

Do you do this?

pbrockway2a at 2007-7-14 19:25:38 > top of Java-index,Java Essentials,New To Java...
# 4

Hi,

My simplified code is as following

public class testForm extends JFrame {

...

...

private JScrollPane jScrollPane1 = new JScrollPane();

private GridLayout gridLayout3 = new GridLayout();

private JList jList1 = new JList();

public testForm() {

addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {int action = _popupTransactionDialog();

if (action!=JOptionPane.CLOSED_OPTION){ panelBinding.releaseDataControl(); statusBar.release();System.exit(0);} } });

}

public void jbInit() throws Exception {

........

jScrollPane1.getViewport().setView(jList1);

jPanel2.add(jScrollPane1, null);

jScrollPane1.getViewport().setLayout(gridLayout3);

......

jList1.setModel((ListModel)panelBinding.bindUIControl("BookingView1",jList1));

}

public static void main(String [] args) {

...........

}

}

Ivan

egckhada at 2007-7-14 19:25:38 > top of Java-index,Java Essentials,New To Java...
# 5
> The only thing that I could think of is that the> JScrollPane isn't redrawing. Try creating a thread> that just calls repaint().Hi,Sorry..I am new to Java ..How to call a thread to repaint ? Ivan
egckhada at 2007-7-14 19:25:38 > top of Java-index,Java Essentials,New To Java...
# 6
Hi,I think the main difference between my code and yours is my code without the following EventQueue.invokeLater(new Runnable() {But my class extends the JFrame directly instead Where should I put this code in my java ?Thanks .Ivan
egckhada at 2007-7-14 19:25:38 > top of Java-index,Java Essentials,New To Java...
# 7

> Hi,

>

> I think the main difference between my code and

> yours is my code without the following

>

>EventQueue.invokeLater(new Runnable() {

> But my class extends the JFrame directly instead

>

>

>Where should I put this code in my java ?

>Thanks .

> Ivan

Somewhere you must call setVisible(true) on your frame, no? Wrap that call in an invokeLater and see if that helps (I'm not guaranteeing it will).

Torgila at 2007-7-14 19:25:38 > top of Java-index,Java Essentials,New To Java...