java - JScrollPane 不起作用(MigLayout)

标签 java swing jtable jscrollpane miglayout

我正在尝试使用 MiglayoutJScrollPane 放入 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();
    }
}

最佳答案

在滚动 Pane 下方插入这两行:

pane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
pane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

这会添加上/下和左/右滚动条。

但是你的代码有点复杂,很难看出你真正想要完成什么。尤其是添加大量 JPanel 的部分看起来很奇怪,而且看起来有问题。

关于java - JScrollPane 不起作用(MigLayout),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34746170/

相关文章:

java - 如何在不使用 setPreferredSize() 的情况下调整 JScrollPane 中 JTable 的大小

java - 即使我们必须迭代数组,如何从java中的数组中删除元素,或者我们可以直接这样做吗?

java - 在这种情况下我应该使用哪种排序算法?

java - 将 Word 文档读入处理时出错

java - 如何在 Java Swing 中正确设计 MV 应用程序?

java - 如何更改 JTable 背景颜色默认为白色?

java - 加载用户在 JTable 中插入的所有单元格内容,并将它们作为对象存储在 LinkedList 中

java - 如何显示最后六个日期,java.util.Date?

java - 在 Windows 上设置 Java 进程名称

java - 在现有的 JFrame 中重绘一个新的 JFrame?