试图让JTable + JScrollPane滚动

时间:2012-10-29 03:04:08

标签: java swing jtable jscrollpane

我已完成Google搜索并尝试实施有关JTable和JScrollPane的许多不同解决方案。以下是我遇到的问题的自包含示例。我希望能够有这些表的水平滚动条。非常感谢您的协助:

package org.examples;

import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;

public class TableTest extends JFrame {

    private JTable tableTest_1;
    private ArrayListStringTableModel tableTestModel_1;

    private JTable tableTest_2;
    private ArrayListStringTableModel tableTestModel_2;

    private JScrollPane tableTestScrollPane_1;
    private JScrollPane tableTestScrollPane_2;

    private class ArrayListStringTableModel extends AbstractTableModel {

        /**
         * 
         */
        private static final long serialVersionUID = 3071398398073956893L;

        private List <String> listOfStrings;

        private String[] columnNames = {""};

        public ArrayListStringTableModel (String[] lColumnNames) {
            columnNames = lColumnNames;
            listOfStrings = new ArrayList<String>();
        }

        public void addString (String lString) {
            String localString = lString;
            listOfStrings.add(localString);
        }

        public int getColumnCount () {
            return columnNames.length;
        }

        public int getRowCount () {
            return listOfStrings.size();
        }

        public String getColumnName (int col) {
            return columnNames[col];
        }

        public Object getValueAt (int row, int column) {
            Object returnObject = null;
            String localString = listOfStrings.get(row);
            switch (column) {
            case 0 :
                returnObject = (Object)localString;
                break;
            }
            return returnObject;
        }

    }


    /**
     * Initialize the contents of the frame.
     */
    public TableTest() {

        String[] rows = {"Damage Resistance (only vs. Head Shots)  Physical Damage Reduction, Resistant, 75% ; Only Works Against Head Shots Rare attack , Only Amyklai can use   Energy Damage Reduction, Resistant, 75% ; Only Works Against Head Shots Rare attack , Only Amyklai can use",
                "Mental Defense (10 points total),Only Amyklai can use",
                "Sight Group Flash Defense (5 points),Only Amyklai can use",
                "Resistant Protection (10 PD/10 ED),STR Min 18 , Costs Half Endurance",
                "Banded Mail Resistant Protection (8 PD/8 ED)"
        };

        String[] columns = {"Damage"};

        tableTestModel_1 = new ArrayListStringTableModel(columns);
        tableTestModel_2 = new ArrayListStringTableModel(columns);

        // Set up the main window frame
        setTitle("Table Test");
        setSize(435, 375);
        getContentPane().setLayout(null);


        for (int i=0; i <= 4; i++) {
            tableTestModel_1.addString(rows[i]);
            tableTestModel_2.addString(rows[i]);
        }
        tableTest_1 = new JTable(tableTestModel_1);
        tableTest_1.setBounds(new Rectangle(10, 10, 395, 250));
        tableTest_1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        tableTestScrollPane_1 = new JScrollPane(tableTest_1);
        tableTestScrollPane_1.setBounds(10, 10, 405, 80);
        tableTestScrollPane_1.setColumnHeaderView(tableTest_1);

        tableTest_2 = new JTable(tableTestModel_2);
        tableTest_2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        tableTest_2.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        tableTestScrollPane_2 = new JScrollPane(tableTest_2);
        tableTestScrollPane_2.setBounds(10, 100, 200, 80);

        JViewport viewport = new JViewport();
        viewport.setView(tableTest_2);
        viewport.setPreferredSize(tableTest_2.getPreferredSize());
        tableTestScrollPane_2.setRowHeaderView(viewport);
        tableTestScrollPane_2.setCorner(JScrollPane.UPPER_LEFT_CORNER, tableTest_2.getTableHeader());

        getContentPane().add(tableTestScrollPane_1);
        getContentPane().add(tableTestScrollPane_2);

    }   

    /**
     * @param args
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TableTest testtableWindow = new TableTest();
                    testtableWindow.setVisible(true);                                                   
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

}

1 个答案:

答案 0 :(得分:6)

这会将表移动到滚动窗格列标题位置,它没有滚动条...摆脱它......

tableTestScrollPane_1.setColumnHeaderView(tableTest_1);

这实质上是从滚动窗格中删除tableTest_2并将其放在新的视图端口中,并将其添加到行标题中......没有滚动条。

viewport.setView(tableTest_2);
...
tableTestScrollPane_2.setRowHeaderView(viewport);

组件只能属于单个父...

尝试类似的事情;

tableTest_1 = new JTable(tableTestModel_1);
tableTest_1.setBounds(new Rectangle(10, 10, 395, 100));
tableTest_1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tableTestScrollPane_1 = new JScrollPane(tableTest_1);

tableTest_2 = new JTable(tableTestModel_2);
tableTest_2.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tableTest_2.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tableTestScrollPane_2 = new JScrollPane(tableTest_2);

getContentPane().add(tableTestScrollPane_1);
getContentPane().add(tableTestScrollPane_2);