package com.test.tab;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
public class JTableExample extends JFrame {
private JTable table;
private CustomTableModel model;
public JTableExample() {
setTitle("JTable Example");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 500);
// Prepare data source (List<Person>)
List<Person> personList = new ArrayList<>();
personList.add(new Person("John", "Smith", 25, "Male", "Engineer", "New York"));
personList.add(new Person("Jane", "Doe", 30, "Female", "Doctor", "Los Angeles"));
personList.add(new Person("Alice", "Johnson", 40, "Female", "Lawyer", "San Francisco"));
personList.add(new Person("Bob", "Williams", 50, "Male", "Accountant", "Chicago"));
personList.add(new Person("David", "Brown", 45, "Male", "Professor", "Boston"));
personList.add(new Person("Sarah", "Taylor", 35, "Female", "Journalist", "Washington D.C."));
personList.add(new Person("James", "Davis", 28, "Male", "Developer", "Seattle"));
personList.add(new Person("Emily", "Martin", 42, "Female", "Manager", "Houston"));
personList.add(new Person("Michael", "Clark", 38, "Male", "Designer", "Atlanta"));
personList.add(new Person("Olivia", "White", 31, "Female", "Marketing", "Miami"));
personList.add(new Person("Thomas", "Harris", 48, "Male", "Consultant", "Denver"));
personList.add(new Person("Sophia", "Allen", 27, "Female", "Artist", "Portland"));
personList.add(new Person("William", "Young", 36, "Male", "Writer", "Philadelphia"));
personList.add(new Person("Chloe", "King", 33, "Female", "Entrepreneur", "Dallas"));
personList.add(new Person("Daniel", "Nelson", 44, "Male", "Architect", "Phoenix"));
personList.add(new Person("Ava", "Anderson", 29, "Female", "Scientist", "San Diego"));
personList.add(new Person("Christopher", "Baker", 39, "Male", "Businessman", "Las Vegas"));
personList.add(new Person("Ella", "Garcia", 26, "Female", "Chef", "New Orleans"));
personList.add(new Person("Andrew", "Mitchell", 43, "Male", "Investor", "Austin"));
personList.add(new Person("Victoria", "Turner", 32, "Female", "HR", "Kansas City"));
personList.add(new Person("Joseph", "Parker", 47, "Male", "CEO", "San Antonio"));
personList.add(new Person("Mia", "Collins", 34, "Female", "Fashion", "Nashville"));
personList.add(new Person("Richard", "Edwards", 37, "Male", "Sales", "Orlando"));
personList.add(new Person("Natalie", "Stewart", 41, "Female", "Analyst", "Salt Lake City"));
personList.add(new Person("Kevin", "Scott", 46, "Male", "Consultant", "Detroit"));
personList.add(new Person("Caroline", "Cooper", 24, "Female", "Intern", "Raleigh"));
personList.add(new Person("Jason", "Green", 52, "Male", "Executive", "Cleveland"));
personList.add(new Person("Grace", "Adams", 23, "Female", "Assistant", "Tampa"));
personList.add(new Person("Brian", "Barnes", 53, "Male", "Director", "St. Louis"));
personList.add(new Person("Isabella", "Martin", 22, "Female", "Student", "Honolulu"));
personList.add(new Person("Steven", "Taylor", 51, "Male", "Investigator", "Charlotte"));
personList.add(new Person("Hailey", "Hill", 21, "Female", "Athlete", "Memphis"));
personList.add(new Person("Jacob", "Wright", 49, "Male", "Trader", "Pittsburgh"));
personList.add(new Person("Lily", "Lewis", 20, "Female", "Model", "Minneapolis"));
personList.add(new Person("Eric", "Allen", 54, "Male", "Consultant", "Albuquerque"));
personList.add(new Person("Samantha", "Howard", 19, "Female", "Volunteer", "Sacramento"));
personList.add(new Person("Mark", "Robinson", 55, "Male", "Advisor", "Louisville"));
personList.add(new Person("Madison", "Walker", 18, "Female", "Dancer", "Kansas City"));
personList.add(new Person("Adam", "Perez", 56, "Male", "Project Manager", "Omaha"));
personList.add(new Person("Julia", "Mitchell", 17, "Female", "Singer", "Cincinnati"));
personList.add(new Person("Nicholas", "Hall", 57, "Male", "Consultant", "Tucson"));
personList.add(new Person("Avery", "Young", 16, "Female", "Student", "Portland"));
personList.add(new Person("Matthew", "King", 58, "Male", "Consultant", "Milwaukee"));
// Create custom table model with Person objects
model = new CustomTableModel(personList);
getContentPane().add(new JScrollPane(table));
// Add mouse listener to print selected Person object on cell click
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int row = table.rowAtPoint(e.getPoint());
int column = table.columnAtPoint(e.getPoint());
if (row >= 0 && column >= 0) {
table.changeSelection(row, column, false, false);
Person selectedPerson = model.getPersonAt(row);
System.out.println(selectedPerson);
}
}
});
}
public static void main(String[] args) {
JTableExample example = new JTableExample();
example.setVisible(true);
}
// Person class
private static class Person {
private String firstName;
private String lastName;
private int age;
private String gender;
private String occupation;
private String city;
public Person(String firstName, String lastName, int age, String gender, String occupation, String city) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.gender = gender;
this.occupation = occupation;
this.city = city;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public String getOccupation() {
return occupation;
}
public String getCity() {
return city;
}
@Override
public String toString() {
return "Person{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", occupation='" + occupation + '\'' +
", city='" + city + '\'' +
'}';
}
}
// Custom TableModel implementation
private class CustomTableModel extends AbstractTableModel {
private List<Person> personList;
private String[] columnNames = {"First Name", "Last Name", "Age", "Gender", "Occupation", "City"};
public CustomTableModel(List<Person> personList) {
this.personList = personList;
table = new JTable(this);
// JTableHeader tableHeader = table.getTableHeader();
// tableHeader.setReorderingAllowed(false);
// tableHeader.setResizingAllowed(false);
table.setCellSelectionEnabled(true);
// table.setDefaultRenderer(Object.class, new DefaultTableCellHeaderRenderer());
}
class DefaultTableCellHeaderRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component component = super.getTableCellRendererComponent(table, value, false, hasFocus, row, column);
if (isSelected) {
component.setBackground(table.getSelectionBackground());
} else {
component.setBackground(table.getBackground());
}
return component;
}
}
public Person getPersonAt(int row) {
return personList.get(row);
}
@Override
public int getRowCount() {
return personList.size();
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public String getColumnName(int column) {
// return columnNames[column];//标题
return "";
}
@Override
public Object getValueAt(int row, int column) {
Person person = personList.get(row);
if (column == 0) {
return person.getFirstName();
} else if (column == 1) {
return person.getLastName();
} else if (column == 2) {
return person.getAge();
} else if (column == 3) {
return person.getGender();
} else if (column == 4) {
return person.getOccupation();
} else if (column == 5) {
return person.getCity();
}
return null;
}
}
}
setCellSelectionEnabled这一行是关键