Question

I create a new Jtable and after that I add more rows.But the scrollbar can't reach the last row ? please help me

here is my constructor

public AvailableSeatInRoomFrm() {
    setSize(600,400);
    setLocation(200,300);
    setResizable(true);
    setVisible(true);
    Object[][] obj1= {{null, null,null} };
    String[] col1 ={"ID", "Seat","isPaid"};
    DefaultTableModel x = new DefaultTableModel(obj1,col1){
        boolean[] canEdit = new boolean [] {
            false, false, false
        };

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    };
    tabSeat= new JTable(x);
    tabSeat.setPreferredSize(new Dimension(300, 100));

    tabSeat.setRowSelectionAllowed(true);


    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    tabSeat.setPreferredScrollableViewportSize(tabSeat.getPreferredSize());
    JScrollPane jc1= new JScrollPane(tabSeat);
    jc1.setViewportView(tabSeat);
    JPanel pan1 = new JPanel();
    pan1.add(jc1);
    this.add(pan1);
    pack();
}

and the code add more rows

public void setListS(Vector<Seat> listS) {
    this.listS = listS;
    DefaultTableModel m =(DefaultTableModel)tabSeat.getModel();
    m.getDataVector().removeAllElements();
    TableRowSorter s = new TableRowSorter(m);
    tabSeat.setRowSorter(s);
    for(Seat t : listS)
    {
        m.addRow(new Object[]{t.getID(),t.getSeatNumber(),t.getRoom().getID()});
    }

}

and i create one object AvailableSeatInRoom and call funcition setLists() thank you

Was it helpful?

Solution

Here is my solution remove tabSeat.setPreferredSize(new Dimension(300, 100));

and change

tabSeat.setPreferredScrollableViewportSize(tabSeat.getPreferredSize());

to

tabSeat.setPreferredScrollableViewportSize((new Dimension(300, 100))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top