0

I have a table with 5 columns and my last column is the working days of a teacher.

the user select the days the teacher work with a checkbox, and the row is added to the Jtable when the user press the add button.

I am limited in my width and want to use Jscrollpane's horizontal scroll bar to display the data full

for some reason, when adding new rows and exceeding the size i allocated for the JTable the vertial scroll bar does appear, but its not the case for the horizontal scroll bar.

I am adding a picture:

enter image description here

as you can see I have a vertical scroll bar, but at line 15 the days are cut off, i would like it to have an horizontal scroll bar so i could see the full data

    scrollPane = new JScrollPane();
    scrollPane.setBorder(new EmptyBorder(0, 0, 0, 0));
    scrollPane.setOpaque(false);
    scrollPane.setBounds(10, 11, 508, 241);
    teacherPanel.add(scrollPane);

    String[] columnNames = { "ID", "Name", "Course", "Max Hours", "Days" };
    teacherTableModel = new DefaultTableModel();
    teacherTableModel.setColumnIdentifiers(columnNames);

    teacherTable = new JTable(teacherTableModel) {
        public boolean isCellEditable(int data, int columns) {
            return false;
        }
    };
    teacherTable.setFillsViewportHeight(true);
    teacherTable.setOpaque(false);
    teacherTable.setGridColor(new Color(0, 0, 0));
    teacherTable.setFont(new Font("Tahoma", Font.BOLD, 12));
    teacherTable.setBackground(new Color(204, 204, 204));
    teacherTable.setFocusable(false);
    teacherTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    teacherTable.getTableHeader().setResizingAllowed(false);
    teacherTable.getTableHeader().setReorderingAllowed(false);
    teacherTable.getTableHeader().setOpaque(false);
    teacherTable.getTableHeader().setFont(new Font("Tahoma", Font.BOLD, 12));
    teacherTable.getTableHeader().setBackground(new Color(71, 71, 71));
    teacherTable.getTableHeader().setForeground(new Color(204, 204, 204));

    DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
    centerRenderer.setHorizontalAlignment(SwingConstants.CENTER);

    for (int i = 0; i < 5; i++) {
        teacherTable.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
    }
    teacherTable.getColumnModel().getColumn(0).setMaxWidth(25);
    teacherTable.getColumnModel().getColumn(1).setMinWidth(150);
    teacherTable.getColumnModel().getColumn(3).setMaxWidth(75);
    teacherTable.getColumnModel().getColumn(4).setMinWidth(200);

    scrollPane.setViewportView(teacherTable);
    scrollPane.getViewport().setBackground(new Color(62, 62, 62));

    teacherTableRow = new Object[5];
camickr
  • 321,443
  • 19
  • 166
  • 288
Amit Amir
  • 25
  • 7
  • 2
    Simple answer is to use: `table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );`. However, this can result in the columns not filling the entire viewport. Check out: https://stackoverflow.com/questions/6104916/how-to-make-jtable-both-autoresize-and-horizontall-scrollable/6104955#6104955 for a more flexible solution. – camickr Jun 16 '21 at 16:12
  • having the table auto resize mode to auto resize off and increasing the min width of the days columns solved my problem, but the link was very informative and helpful, so thanks for your time – Amit Amir Jun 16 '21 at 16:15

0 Answers0