Java Swing:如何使用 JTable?

发布于 2024-08-31 10:01:15 字数 2552 浏览 0 评论 0原文

我正在尝试构建一个小表来显示约会。 这是我到目前为止所拥有的。也许你可以给我一些提示,告诉我我做错了什么,或者该走哪条路。

            public class AppointmentTable extends JFrame{


        public static void main(String[] args) {
            JTable table = new JTable(new AppointmentTableModel(10, 6, new   ArrayList<Appointment>()));
            JScrollPane scrollPane = new JScrollPane(table);
            table.setFillsViewportHeight(true);
            AppointmentTable  frame = new AppointmentTable();
            frame.add(scrollPane);
            frame.setVisible(true);
        }
    public class AppointmentTable extends JFrame{


    public static void main(String[] args) {
        JTable table = new JTable(new AppointmentTableModel(10, 6, new ArrayList<Appointment>()));
        JScrollPane scrollPane = new JScrollPane(table);
        table.setFillsViewportHeight(true);
        AppointmentTable  frame = new AppointmentTable();
        frame.add(scrollPane);
        frame.setVisible(true);
    }
    }

    public class AppointmentTableModel extends AbstractTableModel {
        private int columns;
        private int rows;
        ArrayList<Appointment> appointments;

        public AppointmentTableModel(int columns, int rows,
                ArrayList<Appointment> appointments) {
            this.columns = columns;
            this.rows = rows;
            this.appointments = appointments;
        }

        @Override
        public int getColumnCount() {

            return columns;
        }

        @Override
        public int getRowCount() {

            return rows;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {

            return appointments.get(rowIndex).getByColumn(columnIndex);
        }
    }

public class Appointment {

    private Date date;
    private Sample sample;
    private String comment;
    private ArrayList<Action> history;

    public Appointment(Date date, Sample sample, String comment) {
        this.date = date;
        this.sample = sample;
        this.comment = comment;
        this.history = new ArrayList<Action>();
    }

    public Object getByColumn(int columnIndex) {
        switch (columnIndex) {
        case 0: return date;

        case 1: return date;

        case 2: return sample;

        case 3: return sample;

        case 4: return history;

        case 5: return comment;


        }
        return null;
    }

}
public class Action {
String action;

public Action(String act){
    this.action=act;
}

}

I am trying to build a little Table to show appointments.
Here is what I have so far. Maybe you can give me a hint about what I am doing wrong, or which way to go.

            public class AppointmentTable extends JFrame{


        public static void main(String[] args) {
            JTable table = new JTable(new AppointmentTableModel(10, 6, new   ArrayList<Appointment>()));
            JScrollPane scrollPane = new JScrollPane(table);
            table.setFillsViewportHeight(true);
            AppointmentTable  frame = new AppointmentTable();
            frame.add(scrollPane);
            frame.setVisible(true);
        }
    public class AppointmentTable extends JFrame{


    public static void main(String[] args) {
        JTable table = new JTable(new AppointmentTableModel(10, 6, new ArrayList<Appointment>()));
        JScrollPane scrollPane = new JScrollPane(table);
        table.setFillsViewportHeight(true);
        AppointmentTable  frame = new AppointmentTable();
        frame.add(scrollPane);
        frame.setVisible(true);
    }
    }

    public class AppointmentTableModel extends AbstractTableModel {
        private int columns;
        private int rows;
        ArrayList<Appointment> appointments;

        public AppointmentTableModel(int columns, int rows,
                ArrayList<Appointment> appointments) {
            this.columns = columns;
            this.rows = rows;
            this.appointments = appointments;
        }

        @Override
        public int getColumnCount() {

            return columns;
        }

        @Override
        public int getRowCount() {

            return rows;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {

            return appointments.get(rowIndex).getByColumn(columnIndex);
        }
    }

public class Appointment {

    private Date date;
    private Sample sample;
    private String comment;
    private ArrayList<Action> history;

    public Appointment(Date date, Sample sample, String comment) {
        this.date = date;
        this.sample = sample;
        this.comment = comment;
        this.history = new ArrayList<Action>();
    }

    public Object getByColumn(int columnIndex) {
        switch (columnIndex) {
        case 0: return date;

        case 1: return date;

        case 2: return sample;

        case 3: return sample;

        case 4: return history;

        case 5: return comment;


        }
        return null;
    }

}
public class Action {
String action;

public Action(String act){
    this.action=act;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲喜皆因你 2024-09-07 10:01:15

首先,您的模型的 getRowCount() 方法不正确,它应该是

public int getRowCount() {
  return appointments.size();
}

然后,您将一个空的约会列表传递给您的模型,因此该表什么也没有显示!

JTable table = new JTable(new AppointmentTableModel(10, 6, new ArrayList<Appointment>()));

在创建表之前使用一些数据初始化列表。

First, your model's getRowCount() method is incorrect, it should be

public int getRowCount() {
  return appointments.size();
}

Then, you're passing an empty appointment list to your model, so the table shows nothing!

JTable table = new JTable(new AppointmentTableModel(10, 6, new ArrayList<Appointment>()));

Initialize your list with some data before creating the table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文