Java ScrollPane And JList

92 views Asked by At

I've ran into a problem and cannot seem to fix it.

I've made two list, list1 holds and array that is initialized outside of the constructor and list2 has nothing in it.

One of the problem I really can't seem to fix is, when I select a word in list1 by clicking the add button, it displays in list2 but for some weird reason each time I try to select another value in list1, it replaces the value that was already there in list2. I've try everything and nothing worked.

The other problem am having is, in list2 when I type in a word longer than the list width, the whole word doesn't show. It only shows about a couple words and three dots at the end of the word. I have a scroll pane set up in it.

Can anyone help me on this one???

private String[] c = {"blue","white","cyan","darkGray","green","gray","black,"d","purple","orange","cyan"};    

public lala(){

model = new DefaultListModel();

list1 = new JList(c);
list1.setVisibleRowCount(10);
list1.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);   


b2 = new JButton("ADD");
b2.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        list.setListData(list1.getSelectedValues());

    }
});

b3 = new JButton("MOVE-->>");
b3.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
          model.addElement(field.getText());
          list.setModel(model);
          field.setText("");

    }
});

list2 = new JList();
list2.setFixedCellHeight(50);
list2.setFixedCellWidth(50);
list2.setVisibleRowCount(10);
list2.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

scroll = new JScrollPane(list2);
scroll.setPreferredSize(new Dimension(150,150));

field = new JTextField(19);
field.setToolTipText("Input Text Area Here");
field.setFont(new Font("Courier",Font.BOLD,20));
field.setBackground(Color.BLACK);
field.setForeground(Color.RED);
field.setDragEnabled(true);

panel = new JPanel();
panel.setBackground(Color.BLACK);

panel.add(b3);
panel.add(b2);
panel.add(field);
panel.add(new JScrollPane(list1));
panel.add(scroll);
add(panel);

   }
}
1

There are 1 answers

3
Hovercraft Full Of Eels On

One of the problem I really can't seem to fix is, when I select a word in list1 by clicking the add button, it displays in list2 but for some weird reason each time I try to select another value in list1, it replaces the value that was already there in list2.

Your code is only doing what you tell it to:

b2.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        list.setListData(list1.getSelectedValues());    
    }
});

Here you're calling setListData(...) on list which completely replaces the data that it holds with new data. Instead get the JList's model and simply call addElement(...) on the model.

b2.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
        DefaultListModel model = (DefaultListModel) list.getModel();
        model.addElement(list1.getSelectedValues());    
    }
});

I've try everything and nothing worked.

Sorry, but you may wish to avoid saying this in the future because it patently isn't true. Shoot, all you had to do was read the JList Tutorial as it's all well explained there.


Regarding,

The other problem am having is, in list2 when I type in a word longer than the list width, the whole word doesn't show. It only shows about a couple words and three dots at the end of the word. I have a scroll pane set up in it.

Call setPrototypeCellValue(...) on list2, and pass in a String big enough to hold the Strings that it will receive.


Edit
You were already told about the DefaultListModel's addElement(...) method in your very recent previous question which makes me scratch my head and wonder why you're not using it here.