【问题标题】:JScrollPane doesn't work (MigLayout)JScrollPane 不起作用(MigLayout)
【发布时间】:2016-04-17 05:11:15
【问题描述】:

我正在尝试将JScrollPaneMiglayout 一起放入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


    【解决方案1】:

    在滚动窗格下方插入这两行:

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

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

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

    【讨论】:

      【解决方案2】:
      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 是表格的一部分,你不能只是“撕掉它”然后在其他地方独立渲染。

      表格添加到scrollPane,然后将scrollPane和表格分别添加到不同的位置是没有意义的。

      JTable table = new JTable(mod);
      JScrollPane pane = new JScrollPane(table);
      //...
      p.add(pane, "dock center");
      

      应该是您需要做的所有事情 - 将表格添加到 scrollPane,然后将 scrollPane 添加到面板的中心。

      【讨论】:

      • 哇,先生,这就是解决方案 :) !!我是一个java初学者,正在尝试用jtable等做一些实验:)非常感谢!
      猜你喜欢
      • 2015-08-09
      • 2013-11-15
      • 1970-01-01
      • 1970-01-01
      • 2013-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多