M.Shark
M.Shark

Reputation: 25

how to make JTable display all records form database

My problem is when i run the program the data displayed at the JTable but not all records are displayed although i have a scroll Pane and aJtable inside it,when i run it provides a scroll but for the firs few records ,the jtable have a textfield to provide searching in table using rowSorter ,although jtable doesn't show all records when i search for a record that is not viewed i gets it.this is an image describing the problem.please how can i fix that. this is the link i have no permission to attache an image here http://www.megafileupload.com/en/file/561971/Untitled-jpg.html this is piece of code where i initialize these component

    table2 = new JTable(tableModel);


    table2.setFont(new Font("Tahoma", Font.PLAIN, 16));
    table2.getTableHeader().setFont(new Font("Tahoma", Font.BOLD, 14));


    scrollPane_1.setColumnHeaderView(table2.getTableHeader());
    table2.setBackground(new Color(210, 180, 140));
    table2.setBorder(new LineBorder(new Color(105, 105, 105)));
    table2.setForeground(new Color(0, 0, 0));
    sorter2 = new TableRowSorter<TableModel>(table2.getModel());
    table2.setRowSorter(sorter2);
    JViewport vp2 = new JViewport();
    vp2.setBackground(new Color(210, 105, 30));
    vp2.setForeground(new Color(0, 0, 0));
    scrollPane_1.setViewportView(vp2);
    vp2.add(table2);

this is the method that fill the table

    table2Data = new Vector<>();
    Iterator<SellBean> buyIt = boughtedList.iterator();
    while (buyIt.hasNext()) {
        SellBean buyBean = buyIt.next();
        Vector rec = new Vector<>();

        rec.add(buyBean.getPRODUCT_ID());

        rec.add(buyBean.getPRODUCT_NAME());
        // System.out.println(buyBean.getPRODUCT_NAME());
        rec.add(buyBean.getQUANTITY());
        rec.add(buyBean.getBUY_PRICE());
        rec.add(buyBean.getSELL_PRICE());
        rec.add(buyBean.getPROFIT());
        rec.add(buyBean.getBUY_DATE());
        rec.add(buyBean.getRETURNED());
        table2Data.add(rec);
    }
    tm2.setDataVector(table2Data, table2ColNames);
    for (int i = 0; i < table2.getColumnCount(); i++) {
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment(JLabel.CENTER);
        table2.getColumnModel().getColumn(i)
                .setCellRenderer(centerRenderer);
    }

    for (int i = 0; i < table2.getRowCount(); i++) {

        table2.setRowHeight(i, 28);
    }
    table2.getColumnModel().getColumn(0).setPreferredWidth(25);
}

i hope it is clear now

Upvotes: 0

Views: 117

Answers (1)

Ben
Ben

Reputation: 3518

Maybe you have problems with the scrollpane (like: no scrollbars visible).
Are your sure you are using it correct?
this looks strange to me:
You create a ViewPort and put this as View?!

JViewport vp2 = new JViewport();
scrollPane_1.setViewportView(vp2);
vp2.add(table2);

why not just set the Table as ViewportView (as the name implies?)
just:

scrollPane_1.setViewportView(table2);

Edit: Info about the ViewPort:
The "viewport" or "porthole" through which you see the underlying information. When you scroll, what moves is the viewport. It is like peering through a camera's viewfinder. Moving the viewfinder upwards brings new things into view at the top of the picture and loses things that were at the bottom.
http://docs.oracle.com/javase/7/docs/api/javax/swing/JViewport.html

Upvotes: 1

Related Questions