Search code examples
javaswingcursor-position

How to fix cursor type or prevent it from changing


I am working on a Jtable and I fixed the columns widths using:

colModel.getColumn(0).setMinWidth(15); colModel.getColumn(0).setMaxWidth(15); colModel.getColumn(0).setPreferredWidth(15);

but even so when hovering over the table edges/borders the double sided arrow cursos appears as if prompting the user to expand the columns. I want either to have the cursor type fixed throughout the whole dialog or prevent it from changing into this cursor in this specific instance.


Solution

  • It seems to be fine to use TableColumn#setResizable(false) method together.

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class ResizeCursorTest {
      public JComponent makeUI() {
        JTable table = new JTable(new DefaultTableModel(10, 3));
    
        TableColumnModel colModel = table.getColumnModel();
        colModel.getColumn(0).setMinWidth(15);
        colModel.getColumn(0).setMaxWidth(15);
        colModel.getColumn(0).setPreferredWidth(15);
        colModel.getColumn(0).setResizable(false);
    
        JPanel p = new JPanel(new BorderLayout());
        p.add(new JScrollPane(table));
        return p;
      }
      public static void main(String... args) {
        EventQueue.invokeLater(() -> {
          JFrame f = new JFrame();
          f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          f.getContentPane().add(new ResizeCursorTest().makeUI());
          f.setSize(320, 240);
          f.setLocationRelativeTo(null);
          f.setVisible(true);
        });
      }
    }