您应该创建一个表模型来保存您的数据,并在需要时创建渲染器以正确呈现它。有关详细信息,请参阅如何使用表格。然后在表的选择侦听器中,您可以检索相关行。您可以使用TableModel.getValueAt()
或添加辅助方法来检索选定的数据。下面是一个简单的用户模型示例。该应用程序打印一个选定的用户,包括在表中不可见的 id。模型中有一个方便的方法getUser()
来获取特定行的用户。请注意,您也可以将DefaultTableModel
其用于简单的场景。
import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.AbstractTableModel;
public class UserTableDemo {
public UserTableDemo() {
JFrame frame = new JFrame("UserTable");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final UserModel model = new UserModel();
final JTable table = new JTable();
table.setModel(model);
JScrollPane scrollPane = new JScrollPane(table);
JPanel content = new JPanel(new BorderLayout());
content.add(scrollPane, BorderLayout.CENTER);
final JTextArea output = new JTextArea(5, 30);
content.add(output, BorderLayout.SOUTH);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
UserEntry entry = model.getUser(table
.getSelectedRow());
if (entry != null) {
output.setText("Selected row:"
+ table.getSelectedRow() + " "
+ entry.toString());
} else {
output.setText("");
}
}
}
});
frame.add(content);
frame.setLocationByPlatform(true);
frame.pack();
frame.setVisible(true);
}
public class UserModel extends AbstractTableModel {
private static final long serialVersionUID = 1L;
private List<UserEntry> users = new ArrayList<UserEntry>();
public UserModel() {
//TODO - load users
users.add(new UserEntry(3, "John", "Doe"));
users.add(new UserEntry(2, "John", "Doe"));
users.add(new UserEntry(1, "John", "Doe"));
}
public UserEntry getUser(int row) {
if(row >= 0 && row < users.size())
return users.get(row);
return null;
}
@Override
public int getRowCount() {
return users.size();
}
@Override
public int getColumnCount() {
return 2;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
UserEntry entry = users.get(rowIndex);
switch (columnIndex) {
case 0:
return entry.getFirstName();
case 1:
return entry.getLastName();
}
return null;
}
@Override
public String getColumnName(int column) {
switch (column) {
case 0:
return "First Name";
case 1:
return "Last Name";
}
return null;
}
}
public class UserEntry {
private int id;
private String firstName;
private String lastName;
public UserEntry(int id, String firstName, String lastName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
@Override
public String toString() {
return "UserEntry [id=" + id + ", firstName=" + firstName
+ ", lastName=" + lastName + "]";
}
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new UserTableDemo();
}
});
}
}
如果您使用JList
而不是JTable
.
编辑:
这是一个类似的示例,它使用JList
withDefaultListModel
和自定义渲染器来显示用户列表。
import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.*;
import javax.swing.event.*;
public class UserListDemo {
public UserListDemo() {
JFrame frame = new JFrame("User List");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JList list = new JList();
JScrollPane scrollPane = new JScrollPane(list);
JPanel content = new JPanel(new BorderLayout());
content.add(scrollPane, BorderLayout.CENTER);
final JTextArea output = new JTextArea(5, 40);
content.add(output, BorderLayout.SOUTH);
final DefaultListModel model = new DefaultListModel();
model.addElement(new UserEntry(3, "John", "Doe"));
model.addElement(new UserEntry(1, "John", "Doe"));
model.addElement(new UserEntry(3, "John", "Doe"));
list.setModel(model);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setCellRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList list,
Object value, int index, boolean isSelected,
boolean hasFocus) {
if (value instanceof UserEntry) {
return super.getListCellRendererComponent(list,
((UserEntry) value).getFirstName() + " "
+ ((UserEntry) value).getLastName(), index,
isSelected, hasFocus);
}
return super.getListCellRendererComponent(list, value, index,
isSelected, hasFocus);
}
});
list.getSelectionModel().addListSelectionListener(
new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
UserEntry entry = (UserEntry) list
.getSelectedValue();
if (entry != null) {
output.setText("Selected row:"
+ list.getSelectedIndex() + " "
+ entry.toString());
} else {
output.setText("");
}
}
}
});
frame.add(content);
frame.setLocationByPlatform(true);
frame.pack();
frame.setVisible(true);
}
public class UserEntry {
private int id;
private String firstName;
private String lastName;
public UserEntry(int id, String firstName, String lastName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
@Override
public String toString() {
return "UserEntry [id=" + id + ", firstName=" + firstName
+ ", lastName=" + lastName + "]";
}
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new UserListDemo();
}
});
}
}