调整JTable列的大小以适应表内容

时间:2012-04-20 06:12:34

标签: java

一旦JTable显示,我使用下面粘贴的代码来调整列宽,延迟5秒。但是,当我运行“TestColumnResizer.java”时,它显示错误“java.lang.NoClassDefFoundError”,主要找不到。如何修改代码以使其运行?或者如何在其他类中调用TestColumnResizer程序?

public class ColumnResizer {
    public static void adjustColumnPreferredWidths(JTable table) {
        // strategy - get max width for cells in column and
        // make that the preferred width
        TableColumnModel columnModel = table.getColumnModel();
        for (int col = 0; col < table.getColumnCount(); col++) {

            int maxwidth = 0;
            for (int row = 0; row < table.getRowCount(); row++) {
                TableCellRenderer rend = table.getCellRenderer(row, col);
                Object value = table.getValueAt(row, col);
                Component comp = rend.getTableCellRendererComponent(table,
                        value, false, false, row, col);
                maxwidth = Math.max(comp.getPreferredSize().width, maxwidth);
            } // for row
            TableColumn column = columnModel.getColumn(col);
            column.setPreferredWidth(maxwidth);
        } // for col
    }
}

// Testing automatic column sizing

public class TestColumnResizer {
    final static Object[][] TABLE_DATA = {
            { new Integer(1), "ONJava", "http://www.onjava.com/" },
            { new Integer(2), "Joshy's Site", "http://www.joshy.org/" },
            { new Integer(3), "Anime Weekend Atlanta",
                    "http://www.awa-con.com/" },
            { new Integer(4), "QTJ book",
                    "http://www.oreilly.com/catalog/quicktimejvaadn/" } };

    final static String[] COLUMN_NAMES = { "Count", "Name", "URL" };

    public static void main(String[] args) {
        // 142 mac l&f has a header bug - force metal for today
        try {

            UIManager.setLookAndFeel(UIManager
                    .getCrossPlatformLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        DefaultTableModel mod = new DefaultTableModel(TABLE_DATA, COLUMN_NAMES);
        JTable table = new JTable(mod);
        JScrollPane pane =

        new JScrollPane(table, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        JFrame frame = new JFrame("JTable Column Widths");
        frame.getContentPane().add(pane);
        frame.pack();
        frame.setVisible(true);

        try {
            Thread.sleep(5000);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // now get smart about col widths
        final JTable fTable = table;
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                ColumnResizer.adjustColumnPreferredWidths(fTable);
                fTable.revalidate();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:2)

从我所看到的,代码的工作没有任何问题,只是定义类的方式。

如果这是一个类文件中的所有内容,那么您将收到错误。要纠正这个问题,只需编辑类修饰符,如下所示:

import java.awt.Component;
import javax.swing.*;
import javax.swing.table.*;

//Testing automatic column sizing
public class TestColumnResizer {
    final static Object[][] TABLE_DATA = {
            { new Integer(1), "ONJava", "http://www.onjava.com/" },
            { new Integer(2), "Joshy's Site", "http://www.joshy.org/" },
            { new Integer(3), "Anime Weekend Atlanta",
                    "http://www.awa-con.com/" },
            { new Integer(4), "QTJ book",
                    "http://www.oreilly.com/catalog/quicktimejvaadn/" } };

    final static String[] COLUMN_NAMES = { "Count", "Name", "URL" };

    public static void main(String[] args) {
        // 142 mac l&f has a header bug - force metal for today
        try {

            UIManager.setLookAndFeel(UIManager
                    .getCrossPlatformLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        DefaultTableModel mod = new DefaultTableModel(TABLE_DATA, COLUMN_NAMES);
        JTable table = new JTable(mod);
        JScrollPane pane =

        new JScrollPane(table, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        JFrame frame = new JFrame("JTable Column Widths");
        frame.getContentPane().add(pane);
        frame.pack();
        frame.setVisible(true);

        try {
            Thread.sleep(5000);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // now get smart about col widths
        final JTable fTable = table;
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                ColumnResizer.adjustColumnPreferredWidths(fTable);
                fTable.revalidate();
            }
        });
    }
}


class ColumnResizer {
    public static void adjustColumnPreferredWidths(JTable table) {
        // strategy - get max width for cells in column and
        // make that the preferred width
        TableColumnModel columnModel = table.getColumnModel();
        for (int col = 0; col < table.getColumnCount(); col++) {

            int maxwidth = 0;
            for (int row = 0; row < table.getRowCount(); row++) {
                TableCellRenderer rend = table.getCellRenderer(row, col);
                Object value = table.getValueAt(row, col);
                Component comp = rend.getTableCellRendererComponent(table,
                        value, false, false, row, col);
                maxwidth = Math.max(comp.getPreferredSize().width, maxwidth);
            } // for row
            TableColumn column = columnModel.getColumn(col);
            column.setPreferredWidth(maxwidth);
        } // for col
    }
}

如果它不是一个类文件而不是try:http://www.tech-recipes.com/rx/826/java-exception-in-thread-main-javalangnoclassdeffounderror/。基本上你的类路径可能有问题。

但是,不用担心,代码似乎可以执行您指定的所有操作!