【发布时间】:2016-04-17 05:11:15
【问题描述】:
我正在尝试将JScrollPane 与Miglayout 一起放入JTable,但如果我编译它,JScrollpane 不起作用(所以没有箭头等等......)。
这是我的代码:
public class MyTableModel extends AbstractTableModel {
final String[] cols = {"", "", ""};
final Object[][] data = {
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
{new ImageIcon(), new String(), new String(), new JPanel()},
};
public int setRowHeight() {
return 50;
}
public int getColumnCount() {
return cols.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return cols[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
return false;
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
System.out.println("setVal" + value); // da sind die Daten!
}
static void create() {
MigLayout layout = new MigLayout("debug,fill");
JFrame f = new JFrame();
JPanel p = new JPanel();
//Button
JButton addRow = new JButton("Add Row");
addRow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("test OK");
}
});
p.setLayout(layout);
p.setBackground(Color.RED);
MyTableModel mod = new MyTableModel();
JTable table = new JTable(mod);
JScrollPane pane = new JScrollPane(table);
JTableHeader tableHeader = table.getTableHeader();
tableHeader.setReorderingAllowed(false);
tableHeader.setResizingAllowed(true);
table.setValueAt(new ImageIcon("C:\\redIcon.png"), 0, 2);
table.setRowHeight(40);
p.add(tableHeader, "dock north");
p.add(table, "dock center");
p.add(pane, "dock east");
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setSize(600, 300);
f.setContentPane(p);
f.setVisible(true);
}
public static void main(String[] Args) {
create();
}
}
【问题讨论】:
标签: java swing jtable jscrollpane miglayout