调整表第一列的大小,即在java中显示列的数字

发布于 2024-11-03 13:24:45 字数 2465 浏览 1 评论 0原文

的示例代码

我包括我的应用程序第一类

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import org.jdesktop.swingx.JXTable;
import javax.swing.table.TableColumn;

public class setcolwidth {

    public static void main(String args[]) {

        final Object rowData[][] = {
            {"kirti", "one", "I"},
            {"kkk", "two", "II"},
            {"kiki", "three", "III"}};
        final String columnNames[] = {"#", "English", "Roman"};

        final JTable table = new JTable(rowData, columnNames);
        JScrollPane scrollPane = new JScrollPane(table);

        JXTable lineTable = new LineNumberTable(table);
        scrollPane.setRowHeaderView(lineTable);
        table.getTableHeader().setReorderingAllowed(false);
        JFrame frame = new JFrame("Resizing Table");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(scrollPane, BorderLayout.CENTER);


        TableColumn column = null;
        int i=0;
        try{

            column = lineTable.getColumnModel().getColumn(i);

                column.setPreferredWidth(50);

        }catch(Exception ex){
            System.out.println("Error for the column index"+i+" Err"+ex.toString());
        }

        frame.setSize(300, 150);
        frame.setVisible(true);

    }
    }

和第二类

import javax.swing.JTable;
import javax.swing.table.*;
import org.jdesktop.swingx.JXTable;

public class LineNumberTable extends JXTable {
    private JTable mainTable;

    public LineNumberTable(JTable table) {
        super();
        mainTable = table;
        setAutoCreateColumnsFromModel( false );
        setModel( mainTable.getModel() );
        setAutoscrolls( false );
        addColumn( new TableColumn() );
        getColumnModel().getColumn(0).setCellRenderer(mainTable.getTableHeader().getDefaultRenderer());
      //  mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        getColumnModel().getColumn(0).setPreferredWidth(40);
        setPreferredScrollableViewportSize(getPreferredSize());

    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }

    @Override
    public Object getValueAt(int row, int column) {
         return Integer.valueOf(row+1);
    }

    @Override
    public int getRowHeight(int row) {
        return mainTable.getRowHeight();
    }
}

,但是行号列没有调整大小。

I include sample code of my application

1st class

import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import org.jdesktop.swingx.JXTable;
import javax.swing.table.TableColumn;

public class setcolwidth {

    public static void main(String args[]) {

        final Object rowData[][] = {
            {"kirti", "one", "I"},
            {"kkk", "two", "II"},
            {"kiki", "three", "III"}};
        final String columnNames[] = {"#", "English", "Roman"};

        final JTable table = new JTable(rowData, columnNames);
        JScrollPane scrollPane = new JScrollPane(table);

        JXTable lineTable = new LineNumberTable(table);
        scrollPane.setRowHeaderView(lineTable);
        table.getTableHeader().setReorderingAllowed(false);
        JFrame frame = new JFrame("Resizing Table");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(scrollPane, BorderLayout.CENTER);


        TableColumn column = null;
        int i=0;
        try{

            column = lineTable.getColumnModel().getColumn(i);

                column.setPreferredWidth(50);

        }catch(Exception ex){
            System.out.println("Error for the column index"+i+" Err"+ex.toString());
        }

        frame.setSize(300, 150);
        frame.setVisible(true);

    }
    }

and 2nd class

import javax.swing.JTable;
import javax.swing.table.*;
import org.jdesktop.swingx.JXTable;

public class LineNumberTable extends JXTable {
    private JTable mainTable;

    public LineNumberTable(JTable table) {
        super();
        mainTable = table;
        setAutoCreateColumnsFromModel( false );
        setModel( mainTable.getModel() );
        setAutoscrolls( false );
        addColumn( new TableColumn() );
        getColumnModel().getColumn(0).setCellRenderer(mainTable.getTableHeader().getDefaultRenderer());
      //  mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        getColumnModel().getColumn(0).setPreferredWidth(40);
        setPreferredScrollableViewportSize(getPreferredSize());

    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }

    @Override
    public Object getValueAt(int row, int column) {
         return Integer.valueOf(row+1);
    }

    @Override
    public int getRowHeight(int row) {
        return mainTable.getRowHeight();
    }
}

But the row number column is not resizing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏佲洛 2024-11-10 13:24:45

这是因为您要在列上设置首选大小,并且需要在行标题的 JViewport 上设置它。

在这一行之后:

    scrollPane.setRowHeaderView(lineTable);

试试这个:

    scrollPane.getRowHeader().setPreferredSize(new Dimension(150,150));

That is because you are setting the preferred size on the column and need to be setting it on the JViewport on the row header instead.

After this line:

    scrollPane.setRowHeaderView(lineTable);

Try this:

    scrollPane.getRowHeader().setPreferredSize(new Dimension(150,150));
↙厌世 2024-11-10 13:24:45

我不使用 JXTable,所以无法发送您的代码。但对于 JTable 来说,你的基本逻辑在我看来没问题。

看看我的行号表,它对我有用。也许差异在于我创建 TableColumn 并分配标题值的方式。

I don't use JXTable so I can't text your code. But your basic logic looks ok to me for a JTable.

Take a look at my Row Number Table which works for me. Maybe the difference is the way I create the TableColumn and assign a header value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文