我的應用程序由幾個JTables組成。 JXTables。目標是存儲列寬並在啓動時重新加載它們。如何在JTable/JXTable中設置列寬?
但是,下面的代碼不會對錶的列寬沒有什麼變化:
tblTasks.getColumn(1).setWidth(36);
而且這個什麼也不做:
tblTasks.getColumn(1).setPreferredWidth(36);
任何想法?
我的應用程序由幾個JTables組成。 JXTables。目標是存儲列寬並在啓動時重新加載它們。如何在JTable/JXTable中設置列寬?
但是,下面的代碼不會對錶的列寬沒有什麼變化:
tblTasks.getColumn(1).setWidth(36);
而且這個什麼也不做:
tblTasks.getColumn(1).setPreferredWidth(36);
任何想法?
但下面的代碼確實在 表的列寬沒有什麼變化:
tblTasks.getColumn(1).setWidth(36);
而且這個什麼也不做:
tblTasks.getColumn(1).setPreferredWidth(36);
任何想法?
。
適當構造函數是JTable.getColumnModel().getColumn(0).setPreferredWidth(36);
例如
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
public class TableRowHeight {
private static final long serialVersionUID = 1L;
private JFrame frame = new JFrame("p*s*s*s*s*t*t");
private String[] columnNames = {"one", "two", "Playing with", "four", "five",};
private String[][] data = {
{"aaaaaa", "bbbbbb", "cccccc", "dddddd", "eeeeeee",},
{"bbbbbb", "cccccc", "dddddd", "eeeeeee", "aaaaaa",},
{"cccccc", "dddddd", "eeeeeee", "aaaaaa", "bbbbbb",},
{"dddddd", "eeeeeee", "aaaaaa", "bbbbbb", "cccccc",},
{"eeeeeee", "aaaaaa", "bbbbbb", "cccccc", "dddddd",}};
private JTable table = new JTable(new DefaultTableModel(data, columnNames));
private TableColumnModel tcm = table.getColumnModel();
private Stack<TableColumn> colDeleted = new Stack<TableColumn>();
private JButton restoreButton = new JButton("Restore Column Size");
private JButton hideButton = new JButton("Set Column Size to Zero");
private JButton deleteButton = new JButton("Delete Column");
private JButton addButton = new JButton("Restore Column");
public TableRowHeight() {
table.setRowMargin(4);
table.setRowHeight(30);
table.setFont(new Font("SansSerif", Font.BOLD + Font.PLAIN, 20));
JScrollPane scrollPane = new JScrollPane(table);
for (int i = 0; i < (tcm.getColumnCount()); i++) {
tcm.getColumn(i).setPreferredWidth(150);
}
table.setPreferredScrollableViewportSize(table.getPreferredSize());
restoreButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
tcm.getColumn(2).setPreferredWidth(100);
}
});
hideButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
tcm.getColumn(2).setPreferredWidth(000);
}
});
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (table.getColumnCount() > 0) {
TableColumn colToDelete = table.getColumnModel().getColumn(table.getColumnCount() - 1);
table.removeColumn(colToDelete);
table.validate();
colDeleted.push(colToDelete);
addButton.setEnabled(true);
} else {
deleteButton.setEnabled(false);
}
}
});
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (colDeleted.size() > 0) {
table.addColumn(colDeleted.pop());
table.validate();
deleteButton.setEnabled(true);
} else {
addButton.setEnabled(false);
}
}
});
JPanel btnPanel = new JPanel();
btnPanel.add(hideButton);
btnPanel.add(restoreButton);
btnPanel.add(deleteButton);
btnPanel.add(addButton);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(scrollPane, BorderLayout.CENTER);
frame.add(btnPanel, BorderLayout.SOUTH);
frame.pack();
frame.setLocation(150, 150);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
TableRowHeight frame = new TableRowHeight();
}
});
}
}
table.getColumn("Column Name").setMinWidth(36);
table.getColumn("Column Name").setMaxWidth(36);
我所做的是將最小寬度設置爲每列的值。對於最後一列的首選寬度,我選擇Integer.MAX_VALUE,將所有其他列都推到最小大小。
這是我的項目的代碼段:
ruleTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
ruleTable.getColumnModel().getColumn(0).setMinWidth(25);
ruleTable.getColumnModel().getColumn(1).setMinWidth(125);
ruleTable.getColumnModel().getColumn(2).setMinWidth(175);
ruleTable.getColumnModel().getColumn(2).setMaxWidth(Integer.MAX_VALUE);
希望它會幫助你或者其他人......對你來說可能是有點晚了;-P
是的,但無論如何感謝:-)! – salocinx 2016-08-10 19:40:25
這個工作對我來說! – 2017-08-31 09:59:19