尝试获取JTable + JScrollPane进行滚动 [英] Trying to get a JTable + JScrollPane to scroll

查看:71
本文介绍了尝试获取JTable + JScrollPane进行滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了Google搜索,并尝试实现了许多有关JTable和JScrollPane的解决方案.以下是我所遇到的问题的一个独立示例.我希望能够在这些表格中使用水平滚动条.非常感谢您的协助:

I have done the Google search and tried implementing a number of different solutions concerning the JTable and JScrollPane. The following is a self contained example of the problem I am having. I want to be able to have a horizontal scroll bar with these tables. Your assistance would be deeply appreciated:

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();
                }
            }
        });

    }

}

推荐答案

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

This is moving the table into the scroll panes column header position, which doesn't have scroll bars...get rid of it...

tableTestScrollPane_1.setColumnHeaderView(tableTest_1);

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

This is essentially removing the tableTest_2 from the scroll pane and placing it within a new view port and adding it to the row header...which has no scroll bars.

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

一个组件只能属于一个父对象...

A component can only belong to a single parent...

尝试类似的方法;

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);

这篇关于尝试获取JTable + JScrollPane进行滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆