简体   繁体   English

JTable Swing:我无法通过在标题中拖动光标来调整列的大小

[英]JTable Swing: I cannot resize column by dragging cursor in header

I cannot resize the column width by moving the cursor between header columns and drag the divider, despite of that I have set setResizingAllowed() to true . 尽管我将setResizingAllowed()设置为true ,但是我无法通过在标题列之间移动光标并拖动分隔线来调整列宽的大小。

tbResumen = new JTable();
tbResumen.setEnabled(false);

tbResumen.setRowHeight(20);
tbResumen.setBackground(UIManager.getColor("Table.selectionBackground"));
tbResumen.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
tbResumen.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
tbResumen.setFont(new Font("Arial", Font.PLAIN, 12));
tbResumen.setDefaultRenderer(Object.class, new RenderTablaInfo());
tbResumen.setOpaque(false);

tbResumen.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tbResumen.setColumnSelectionAllowed(false);
tbResumen.setRowSelectionAllowed(true);

tbResumen.setSize(new Dimension(840, 450));
tbResumen.setPreferredScrollableViewportSize(new Dimension(840, 450));

tbResumen.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

tbResumen.setModel(new DefaultTableModel(new Object[][] {{ label1, label2, label3, label4} },
new String[] { "foo", "bar", "foobar", "barfoo" }));

final JTableHeader headerResumen = new JTableHeader();
headerResumen.setReorderingAllowed(false); 
headerResumen.setResizingAllowed(true);
headerResumen.setColumnModel(tbResumen.getColumnModel());

headerResumen.setTable(tbResumen);
tbResumen.setTableHeader(headerResumen);

If someone needs more code to clarify the situation I'll add them soon. 如果有人需要更多代码来澄清这种情况,我会尽快添加。 Thanks. 谢谢。

It's not clear where your fragment goes awry. 目前尚不清楚您的片段在哪里出错。 The minimal, compete example below shows a table whose columns can be resized but not reordered. 下面的最小竞争示例显示了一个表,该表的列可以调整大小,但不能重新排序。 In particular, note that tbResumen is displayed in a JScrollPane . 特别要注意的是, tbResumen显示在JScrollPane

图片

import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

/**
 * @see http://stackoverflow.com/a/38524632/230513
 */
public class Test {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTable tbResumen = new JTable();
        tbResumen.setPreferredScrollableViewportSize(new Dimension(400, 100));
        tbResumen.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        tbResumen.setModel(new DefaultTableModel(new Object[][]{
            {"label1", "label2", "label3", "label4"}},
            new String[]{"foo", "bar", "foobar", "barfoo"}));
        JTableHeader headerResumen = tbResumen.getTableHeader();
        headerResumen.setReorderingAllowed(false);

        f.add(new JScrollPane(tbResumen));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Test()::display);
    }
}
  1. Make sure your header is the one you are about to configure. 确保您的标头是您要配置的标头。

So, with: 因此,具有:

JTableHeader headerResumen = tbResumen.getTableHeader();
headerResumen.setResizingAllowed(true);

I just create another header with the same model of my table and do config there, not realizing that the new header I created is not that of my table . 我只是使用与我的表相同的model创建了另一个标头,并在那里进行配置,却没有意识到我创建的新标头不是我的表的标头 I must call table.getTableHeader() to get the header I want and the config can work. 我必须调用table.getTableHeader()来获取所需的标题,并且配置可以工作。 Sorry for the stupidity. 对不起,我很傻。

  1. Make sure you have set the cursor to be default cursor when hovering onto the edges between the columns in this table, so you can see it becomes a two-headed arrow and you are aware of that they are adjustable. 将鼠标悬停在此表中列之间的边缘时,请确保将光标设置为默认光标,以便可以看到它变成了双向箭头,并且您知道它们是可调整的。

    tbResumen.setCursor(Cursor.getPredefinedCursor(CURSOR_DEFAULT));

If you set it to CURSOR_HAND , no two-way cursor will not be shown. 如果将其设置为CURSOR_HAND ,则不会显示双向光标。

  1. autoResizingMode can also trigger "problems" , making it harder to adjust width. autoResizingMode还可以触发“问题” ,这使得调整宽度变得更加困难。 When it's set OFF , when you drag column edge, it may be moved, but not freely. 设置为OFF时,拖动列边缘时,它可能会移动,但不能自由移动。 Be sure to set the autoResizingMode to JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS or JTable.AUTO_RESIZE_NEXT_COLUMN to see considerable changes. 请确保将autoResizingMode设置为JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNSJTable.AUTO_RESIZE_NEXT_COLUMN以查看大量更改。

  2. (half a year later when I come back to my answer, I still find it useful...) If you disable the JScrollPane containing the table, the cursor will not change neither, and not two-way cursor will be shown. (半年后,当我回到我的答案时,我仍然发现它很有用...)如果禁用包含表的JScrollPane ,则光标将不会更改,也不会显示双向光标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM