我试图删除我移动到第二个Jlist的项目。我一直尝试使用两个不同的代码,一个使用HashSet,另一个使用Arrays和For循环。
HashSet方法:
//So far my code moves the items.
Set<Integer> keepThese = new HashSet<>();
int sel[] = lstLista1.getSelectedIndices();
for (int x : sel) {
keepThese.add(x);
}
for (int i=0 ; i< modelo1.getSize() ; i++) {
if(keepThese.contains(i)) {
} else {
if(modelo2.contains(modelo1.getElementAt(i))) {
} else {
modelo2.addElement(modelo1.getElementAt(i));
}
}
}
数组和For循环
int indices[] = lstLista1.getSelectedIndices();
String arreglo[] = new String[indices.length];
for (int i = 0; i < indices.length; i++) {
arreglo[i] = modelo1.getElementAt(indices[i]).toString();
}
for (int i = indices.length-1; i >= 0; i--) {
if(modelo2.contains(modelo1.getElementAt(indices[i]))) {
} else {
modelo1.remove(indices[i]);
}
}
for (int i = 0; i < modelo1.getSize(); i++) {
if(!modelo2.contains(modelo1.getElementAt(i))) {
modelo2.addElement(modelo1.getElementAt(i));
}
}
modelo1.removeAllElements();
for (int i = 0; i < arreglo.length; i++) {
modelo1.addElement(arreglo[i]);
}
答案 0 :(得分:0)
我不确定为什么你有4个循环?
您需要分两步完成(即两个循环):
第1步应该是直截了当的。只需创建一个循环来获取每个索引的数据并将其添加到另一个ListModel。假设您正在使用DefaultListModel,代码将类似于:
for (int i = 0; i < indices.length; i++) {
model2.addElement( model1.getElementAt(indices[i]) );
这是第2步的示例。它演示了JList和JTable的逻辑:
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.*;
import javax.swing.table.*;
public class ItemDeletion extends JPanel
{
private JList<String> list;
private JTable table;
public ItemDeletion()
{
setLayout( new BorderLayout(5, 5) );
String[] items =
{
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten"
};
// Add the list
DefaultListModel<String> listModel = new DefaultListModel<String>();
for (String item: items)
listModel.addElement( item );
list = new JList<String>( listModel );
JButton listDelete = new JButton( "Delete From List" );
listDelete.addActionListener( new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
DefaultListModel model = (DefaultListModel)list.getModel();
int row = list.getSelectedIndex();
while (row != -1)
{
model.removeElementAt( row );
row = list.getSelectedIndex();
}
}
});
JPanel listPanel = new JPanel( new BorderLayout(5, 5) );
listPanel.add(new JScrollPane( list ), BorderLayout.CENTER);
listPanel.add(listDelete, BorderLayout.PAGE_END);
// Add the table
DefaultTableModel tableModel = new DefaultTableModel(0, 1);
List<String> tableItems = Arrays.asList( items );
Collections.shuffle( tableItems );
for (String item: tableItems)
{
System.out.println( item );
tableModel.addRow( new String[]{item} );
}
table = new JTable( tableModel );
table.setAutoCreateRowSorter(true);
((DefaultRowSorter)table.getRowSorter()).toggleSortOrder(0);
JButton tableDelete = new JButton( "Delete From Table" );
tableDelete.addActionListener( new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
DefaultTableModel model = (DefaultTableModel)table.getModel();
int row = table.getSelectedRow();
while (row != -1)
{
int modelRow = table.convertRowIndexToModel( row );
model.removeRow( modelRow );
row = table.getSelectedRow();
}
}
});
JPanel tablePanel = new JPanel( new BorderLayout(5, 5) );
tablePanel.add(new JScrollPane( table ), BorderLayout.CENTER);
tablePanel.add(tableDelete, BorderLayout.PAGE_END);
add(listPanel, BorderLayout.LINE_START);
add(tablePanel, BorderLayout.LINE_END);
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Multiple Item Deletion");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ItemDeletion(), BorderLayout.NORTH);
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}