我有一个类读取目标文件并将其导入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());
}
}
);
//========================================================================
}
}
答案 0 :(得分:1)
不要保留单独的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中删除条目,请查看How to Use Lists上的Swing教程中的部分。 &#34; Fire&#34;按钮将删除所选行。本教程还将介绍有关使用自定义渲染器的更多信息。
答案 1 :(得分:0)
将一个toString()方法添加到Log类。
public void toString(){
return "" + getDiveNumber()+"__"+getDiveDate()+"/"+getDiveMonth()+"/"+getDiveYear() +"__"+getLocation()+"_"+getDiveSite();
}
然后添加listmodel日志实例
model.addElement(logList.get(i));