gpt4 book ai didi

java - 从 ArrayList 中删除对象以及 Jlist 中相应的索引

转载 作者:行者123 更新时间:2023-12-01 10:36:26 25 4
gpt4 key购买 nike

我有一个类,它读取对象文件并将其导入到 ArrayList 中。然后我制作了一个 Jlist 来显示该 ArrayList 中每个对象的一些信息。因此,当他们选择 Jlist 中的一个元素时,它将显示 ArrayList 中相应的整个索引对象。我想添加一个“删除”按钮来从 ArrayList 中删除选定的 Jlist 元素和相应的索引对象。然后更新 Jlist,显示该元素已被删除。

这是我的课:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JScrollPane;
import javax.swing.JList;

import java.awt.TextArea;

import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.JTextArea;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class LogBook2 extends JFrame {


private static ArrayList<Log> logList = new ArrayList<Log>();



private static String logs;



private JPanel contentPane;
private JScrollPane scrollPane;
private JList list;
private JScrollPane scrollPane_1;
private String tpContent;

private int diveNumber;
private int diveDate;
private String diveMonth;
private int diveYear;
private String location;
private String diveSite;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LogBook frame = new LogBook();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public LogBook2() {


//===============================================================================
//Importing form the Object file to ArrayList "logList"
File fileOfLogs=new File("logList.txt");
if(fileOfLogs.exists()){
try{
ObjectInputStream myStream = new ObjectInputStream(new FileInputStream("logList.txt"));

logList = (ArrayList<Log>) myStream.readObject();



myStream.close();


}
catch(Exception e){
setVisible(false);
JOptionPane.showMessageDialog(null, " 00000 There was a problem while reading dive records.\n "
+ "Please contact SEASFiRE Tech Team.", "Error", JOptionPane.ERROR_MESSAGE);

}

}
else if(!fileOfLogs.exists()){
JOptionPane.showMessageDialog(null, " 11111 There was a problem while reading dive records.\n "
+ "Please contact SEASFiRE Tech Team.", "Error", JOptionPane.ERROR_MESSAGE);

}
//===============================================================================

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 888, 680);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);



JScrollPane scrollPane_2 = new JScrollPane();
scrollPane_2.setBounds(10, 88, 297, 542);
contentPane.add(scrollPane_2);

JList list_1 = new JList();
scrollPane_2.setViewportView(list_1);
list_1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);


//===============================================================================
//getting some information of each object and making a Jlist element with it.
DefaultListModel model=new DefaultListModel();
for( int i=0;i<logList.size();i++){



model.addElement(""+logList.get(i).getDiveNumber()+"__"+logList.get(i).getDiveDate()+"/"+logList.get(i).getDiveMonth()+"/"+logList.get(i).getDiveYear()
+"__"+logList.get(i).getLocation()+"_"+logList.get(i).getDiveSite());

}

list_1.setModel(model);


//===============================================================================



JLabel lblAddNewLog = new JLabel("Log Book");
lblAddNewLog.setHorizontalAlignment(SwingConstants.CENTER);
lblAddNewLog.setFont(new Font("Myanmar Text", Font.BOLD, 40));
lblAddNewLog.setBounds(0, 16, 862, 61);
contentPane.add(lblAddNewLog);



scrollPane = new JScrollPane();
scrollPane.setBounds(317, 88, 322, 542);
contentPane.add(scrollPane);

JTextArea textArea = new JTextArea();
textArea.setEditable(false);
scrollPane.setViewportView(textArea);
JButton btnDelete = new JButton("Delete");
//===============================================================================
//trying to make the "Delete" button. Not working...
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {


model.remove(list_1.getSelectedIndex());
list_1.remove(list_1.getSelectedIndex());
textArea.setText("");
list_1.clearSelection();






}
});
btnDelete.setBounds(649, 561, 115, 29);
contentPane.add(btnDelete);
//========================================================================




//========================================================================
//the action for when an element is selected.
list_1.addListSelectionListener(
new ListSelectionListener(){
public void valueChanged(ListSelectionEvent event){
textArea.setText("");
textArea.append("Dive Number: "+logList.get(list_1.getSelectedIndex()).getDiveNumber()+"\t\tDive Date: "+
logList.get(list_1.getSelectedIndex()).getDiveDate()+"/"+logList.get(list_1.getSelectedIndex()).getDiveMonth()+
"/"+logList.get(list_1.getSelectedIndex()).getDiveYear()+"\n\nLocation: "+logList.get(list_1.getSelectedIndex()).getLocation()+
"\n\nDive Site: "+logList.get(list_1.getSelectedIndex()).getDiveSite()+"\n\nDive Type: "+logList.get(list_1.getSelectedIndex()).getDiveType()+
"\t\tCondition: "+logList.get(list_1.getSelectedIndex()).getCondition()+"\n\nVisibility: "+logList.get(list_1.getSelectedIndex()).getVisibility()+
"m\t\tSuit Type: "+logList.get(list_1.getSelectedIndex()).getSuitType()+"\n\nWeights: "+logList.get(list_1.getSelectedIndex()).getWeight()+
"m\t\tAltitude: "+logList.get(list_1.getSelectedIndex()).getAltitude()+"m\n\nTime In: "+logList.get(list_1.getSelectedIndex()).getHourIn()+
":"+logList.get(list_1.getSelectedIndex()).getMinIn()+"\t\tDuration: "+logList.get(list_1.getSelectedIndex()).getDuration()+
"min\n\nMax Depth: "+logList.get(list_1.getSelectedIndex()).getMaxDepth()+"m\t\tSafety Stop: "+logList.get(list_1.getSelectedIndex()).getSafetyStop()+
"\n\nAir in: "+logList.get(list_1.getSelectedIndex()).getAirIn()+"bar\t\tAir Out: "+logList.get(list_1.getSelectedIndex()).getAirOut()+
"bar\n\n02 Perc: "+logList.get(list_1.getSelectedIndex()).getOxgPerc()+"%\t\tDive Buddy: "+logList.get(list_1.getSelectedIndex()).getDiveBuddy()+
"\n\nNote: "+logList.get(list_1.getSelectedIndex()).getNote());


}
}
);



//========================================================================







}
}

最佳答案

不要保留单独的 ArrayList 来保存数据。

相反,所有数据都应该存储在 ListModel 中

DefaultListModel<Log> model = new DefaultListModel<Log>();
model.addElement(...);
JList<Log> list = new JList<Log>( model );

然后,您为 JList 创建一个自定义呈现器,以显示您想要的 Log 类中的任何数据:

list.setCellRenderer( new DefaultListCellRenderer()
{
public Component getListCellRendererComponent(
JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
Log log = (Log)value;
setText( log.get...() ):
return this;
}
});

现在您只有一个位置的数据,因此当您想要删除一行时,只需从 DefaultListModel 中删除该行即可。

如果您想了解如何从 ListModel 中删除条目,请查看 Swing 教程 How to Use Lists 中的部分。 。 “Fire”按钮将删除所选行。本教程还将详细解释如何使用自定义渲染器。

关于java - 从 ArrayList 中删除对象以及 Jlist 中相应的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34702475/

25 4 0