简体   繁体   中英

how to make this jtable scrollable

When I try to put a vertical scrollpane in the table, the table ctab just disappears. here is the code:

    ctab = new JTable();
    ctab.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);



    ctab.setBounds(15, 97, 780, 347);
    frmKusinaNiKambal.getContentPane().add(ctab);
            ctab.setModel(new DefaultTableModel(
                new Object[][] {
                    {"", "", null, null, null, null, null, null, null},
                },
                new String[] {
                    "Contract #", "Engager", "Contact #", "Client Address", "Date", "Time", "Event Address", "Contract FilePath", "Referred By"
                }
            ) {
                boolean[] columnEditables = new boolean[] {
                    false, false, false, false, false, false, false, false, false
                };
                public boolean isCellEditable(int row, int column) {
                    return columnEditables[column];
                }
            });
            ctab.getColumnModel().getColumn(3).setPreferredWidth(102);
            ctab.getColumnModel().getColumn(4).setPreferredWidth(62);
            ctab.getColumnModel().getColumn(5).setPreferredWidth(41);
            ctab.getColumnModel().getColumn(6).setPreferredWidth(95);
            ctab.isEnabled();

How can i get this table to be scrollable?

I don't see in your posted code where you deal anywhere with a JScrollPane. Rather your code shows you placing the JTable directly into the contentPane.

The solution is to

  • place the JTable into a JScrollPane's viewport. You can do this by passing the JTable into the JScrollPane's constructor, or by calling setViewportView(...) on the JScrollPane and passing in the JTable.
  • Then place the JScrollPane, not the JTable, into your GUI.
  • Learn about and use the Swing layout managers. Absolute positioning with null layouts and setBounds will lead you to rigid difficult to upgrade GUIs.

Useful Tutorial Links:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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