您好我很难解决这个难题。教授给出了代码并告诉我们改变它以使列头可以点击。任何人都可以为我改变它。浪费了差不多4个小时:(在下面的java波动中不太好是代码...... 下面是代码:
package academic.emailClient.view;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.*;
import academic.emailClient.model.Message;
import academic.emailClient.view.entities.MessageView;
public class MailBox extends JFrame {
JTable tableinbox;
private final int MAILBOX_FRAME_WIDTH = 1080;
private final int MAILBOX_FRAME_HEIGHT = 650;
static final boolean VISIBLE = true;
public MailBox() {
Object[] columnNamesinbox = { "Boolean", "Sender", "Subject", "Body" };
Object[][] datainbox = {
{ false, "5@gmail.com", "D", "T" },
{ false, "r@gmail.com", "projhsdject work", "I " },
{ false, "r.@ymail.com", "Job Placement","Iut that" },
{ true, "yam@gmail.com", "s1", "as" }
};
DefaultTableModel modelinbox = new DefaultTableModel(datainbox,
columnNamesinbox);
tableinbox = new JTable(modelinbox) {
@Override
public Class getColumnClass(int columninbox) {
switch (columninbox) {
case 0:
return Boolean.class;
case 1:
return String.class;
case 2:
return String.class;
case 3:
return String.class;
default:
return Boolean.class;
}
}
};
tableinbox.setBackground(Color.lightGray);
tableinbox.setGridColor(new Color(0,128,0));
JTableHeader inboxheader = tableinbox.getTableHeader();
inboxheader.setBackground(Color.DARK_GRAY);
inboxheader.setForeground(Color.white);
}
public int getMesssagesCount(int i) {
return 0;
}
public int GetCheckedItemsNumber(int i) {
return 0;
}
public Object[][] GetCheckedItems() {
return GetCheckedItems();
}
public Object[][] GetMail() {
return GetMail();
}
public Object[][] OrderByDate() {
return OrderByDate();
}
public Object[][] OrderBySender() {
return OrderBySender();
}
public Object[][] OrderBySubject() {
return OrderBySubject();
}
public JTable getMailBox() {
return tableinbox;
}
public void run() {
JFrame MailBoxFrame = new JFrame();
MailBoxFrame.setMaximumSize(new Dimension(MAILBOX_FRAME_WIDTH, MAILBOX_FRAME_HEIGHT));
MailBoxFrame.setPreferredSize(new Dimension(MAILBOX_FRAME_WIDTH, MAILBOX_FRAME_HEIGHT));
MailBoxFrame.setBounds(0, 0, MAILBOX_FRAME_WIDTH, MAILBOX_FRAME_HEIGHT);
MailBoxFrame.setVisible(VISIBLE);
MailBoxFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
答案 0 :(得分:1)
教授给出了代码并告诉我们改变它以使列头可点击
见JTable.setAutoCreateRowSorter(true)
。例如。这是您在点击Cat.
列标题后可能会看到的内容。小的向上指向箭头表示该表在该列上排序。
答案 1 :(得分:0)
如果您只想通过单击列表来排序表,那么您只需创建自己的表模型并覆盖get列类函数,例如。
public class TableViewModel extends AbstractTableModel{
@Override
public Class<?> getColumnClass( int column ) {
switch(column) {
case 0:
return String.class;
case 1:
return String.class;
。 。
然后用你的JTable与那个tablemodel和sorter绑定
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(<YOURJTABLE>.getModel());
.setRowSorter(sorter);