如何在Java中从JList变量获取文本
我试图获取从一个JList项传递到另一个JFrame的信息。我没有问题将JList中的第一个项目转移过来,但是一旦我添加了另一个项目,我就没有任何运气了。举个例子, 我的名单包括:如何在Java中从JList变量获取文本,java,swing,Java,Swing,我试图获取从一个JList项传递到另一个JFrame的信息。我没有问题将JList中的第一个项目转移过来,但是一旦我添加了另一个项目,我就没有任何运气了。举个例子, 我的名单包括: John Doe Jane Doe 当选择John Doe时单击编辑按钮时,它会自动分配JList added Jane Doe(在本例中为Jane Doe)中最后一项的名字和姓氏字段。如何让它选择JList中所选项目的FirstName字段?我希望这是有意义的 注意:忽略集群代码 public class Mai
John Doe
Jane Doe
当选择John Doe时单击编辑按钮时,它会自动分配JList added Jane Doe(在本例中为Jane Doe)中最后一项的名字和姓氏字段。如何让它选择JList中所选项目的FirstName字段?我希望这是有意义的
注意:忽略集群代码
public class MainJPanel extends javax.swing.JPanel {
DefaultListModel contacts = new DefaultListModel();
/**
* Creates new form JPanel
*/
public MainJPanel() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
firstNameLabel = new javax.swing.JLabel();
lastNameLabel = new javax.swing.JLabel();
firstNameField = new javax.swing.JTextField();
lastNameField = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
contactList = new javax.swing.JList();
jPanel1 = new javax.swing.JPanel();
editButton = new javax.swing.JButton();
deleteButton = new javax.swing.JButton();
addButton = new javax.swing.JButton();
setBorder(javax.swing.BorderFactory.createTitledBorder("Add Contact"));
firstNameLabel.setText("First Name:");
lastNameLabel.setText("Last Name:");
jScrollPane1.setViewportView(contactList);
editButton.setText("Edit");
editButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
editButtonActionPerformed(evt);
}
});
deleteButton.setText("Delete");
deleteButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
deleteButtonActionPerformed(evt);
}
});
addButton.setText("Add");
addButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(86, 86, 86)
.addComponent(editButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(deleteButton))
.addComponent(addButton)))
);
jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {addButton, deleteButton, editButton});
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(addButton)
.addComponent(deleteButton)
.addComponent(editButton))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(firstNameLabel)
.addComponent(lastNameLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lastNameField)
.addComponent(firstNameField))
.addGap(6, 6, 6))
.addGroup(layout.createSequentialGroup()
.addGap(41, 41, 41)
.addComponent(jScrollPane1))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(firstNameLabel)
.addComponent(firstNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lastNameLabel)
.addComponent(lastNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
.addContainerGap())
);
}// </editor-fold>
private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
contactList.setModel(contacts);
Object fullName = firstNameField.getText() + " " + lastNameField.getText();
contacts.addElement(fullName);
}
private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {
int reply = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete this contact?", "Delete Contact?", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
contacts.remove(contactList.getSelectedIndex());
}
}
private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {
EditJFrame frame = new EditJFrame();
String editFirstNameField;
String editLastNameField;
int index = contactList.getSelectedIndex();
editFirstNameField = (String) contactList.
EditJFrame.editFirstNameField.setText(editFirstNameField);
EditJFrame.editLastNameField.setText(editLastNameField);
frame.setVisible(true);
}
你能和我们分享整个课程吗?从通读开始,它会使你的代码更容易阅读。其次,您可能希望完成editFirstNameField=String联系人列表。行,因为不清楚您在做什么,也不清楚如何确定EditLastNameField它不会编译,因为行editFirstNameField=String contactList。不完整…您是否创建了类EditJFrame?若要更快获得更好的帮助,请发布一个。