我正在尝试将JScrollPane
放到JTable
Miglayout
但是如果我编译它,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();
}
}
答案 0 :(得分:1)
在滚动窗格下面插入这两行:
pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
这会为上/下和左/右添加滚动条。
但是你的代码有点复杂,很难看出你真正想要实现的目标。特别是你添加了很多很多JPanels的部分看起来很奇怪而且看起来很不错。
答案 1 :(得分:1)
JTable table = new JTable(mod);
JScrollPane pane = new JScrollPane(table);
JTableHeader tableHeader = table.getTableHeader();
//...
p.add(tableHeader, "dock north");
p.add(table, "dock center");
p.add(pane, "dock east");
这些组件不会那样工作......
tableHeader是表格的一部分,你不能只是把它撕下来#34;并在其他地方独立呈现。
该表已添加到scrollPane中,然后将scrollPane和表分别添加到不同位置没有意义。
JTable table = new JTable(mod);
JScrollPane pane = new JScrollPane(table);
//...
p.add(pane, "dock center");
应该只需要做 - 将表格添加到scrollPane,然后将scrollPane添加到面板的中心。