0
這裏是代碼,任何人都可以運行方便,快捷,那就說明我的問題:一個JList的觸發數據添加到另一個JList中
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.DefaultListSelectionModel;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
public class Driver extends JFrame {
public ArrayList<String> RN = new ArrayList<String>();
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Driver frame = new Driver();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Driver() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 605, 497);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
DefaultListSelectionModel m = new DefaultListSelectionModel();
m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
m.setLeadAnchorNotificationEnabled(false);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(26, 133, 538, 133);
contentPane.add(scrollPane);
scrollPane.setViewportView(listRN);
listRN.setSelectionModel(m);
// listRN.setCellRenderer(new MyListCellRenderer());
listRN.addMouseListener(mouseListener);
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(26, 296, 538, 133);
contentPane.add(scrollPane_1);
scrollPane_1.setViewportView(listPL);
listPL.setSelectionModel(m);
// listPL.setCellRenderer(new MyListCellRenderer());
JButton btnNewButton = new JButton("Load");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
RN.add("Ivan");
RN.add("Gaga");
RN.add("Dragan");
for(int i=0;i<RN.size();i++){
modelRN.addElement(RN.get(i));
}
}
});
btnNewButton.setBounds(386, 58, 89, 23);
contentPane.add(btnNewButton);
}
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
String d = null;
if (e.getClickCount() == 1) {
int index = listRN.locationToIndex(e.getPoint());
System.out.println("clicked on Item " + index);
d = listRN.getSelectedValue();
modelPL.addElement(d);
}
}
};
DefaultListModel<String> modelRN = new DefaultListModel<String>();
DefaultListModel<String> modelPL = new DefaultListModel<String>();
JList<String> listRN = new JList<String>(modelRN);
JList<String> listPL = new JList<String>(modelPL);
}
它是一個簡單的例子。點擊Load按鈕,字符串被添加到第一個JList。點擊那個JList中的一個字符串,它被加載到第二個JList,所以沒什麼大不了的。我的問題是隨後JLists的行爲...當我點擊Jlist中的選擇時,它將字符串加載到第二個Jlist,但突出顯示的選擇跳轉到另一個String ...加上當我點擊一個字符串時在第二個Jlist中,它連接到第一個。
理解JList行爲的根本缺陷是什麼?
謝謝。愚蠢的疏忽對我而言。一切都OK了:) – caniaskyouaquestion 2014-11-24 11:09:53
@caniaskyoua問題我很高興它爲你工作。 – 2014-11-24 11:10:34