jtable doesnot show scrollable bar

59 views Asked by At

the code below is of a table that displays data fetched from database,hte table desnot display the scrollable bar,it only shows the moving up arrow only, so i cannot scroll downwards,it also became difficult to set column widths for specific columns..i did research in the internet but could not solve it

static JTable table1 = new JTable();
static DefaultTableModel model;


 table1=new JTable(model);
 JScrollPane    pane = new JScrollPane(table1);
 String col[] = {"CLASS","TERM","PAYABLE","YEAR"};
 model = new  DefaultTableModel(col,100);


 saachs.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent e) {
             String adm = adms.getText();


            Connection con = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException ex) {
                try {
                    throw new Exception ("driver not found");
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            String url = "jdbc:mysql://localhost/test";
              try {
                con = DriverManager.getConnection(url,"abda","abda");
            } catch (SQLException e1) {
                e1.printStackTrace();
            }if(con!=null){
                Statement st = null;
                ResultSet rs = null;
String query = "select date,term,paid,receipt from fees   where adm = '"+adms.getText()+"'";

                try {
                    st = con.createStatement();
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                try {
                    rs = st.executeQuery(query);
                } catch (SQLException e1) {
                    System.out.println("query not executed");
                    e1.printStackTrace();
                }

                try {
                    while(rs.next()){
                        Vector<String> rowData = new Vector<>();
                         rowData.add(rs.getString(1));
                         rowData.add(rs.getString(2));
                         rowData.add(rs.getString(3));
                         rowData.add(rs.getString(4));
                        // row++;
                         model.addRow(rowData);
                    }
                    table1.setModel(model);
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

            }


        }

    });

snapshot for my application

2

There are 2 answers

0
camickr On
model = new  DefaultTableModel(col,100);

When you use the addRow(...) method the rows added are 101, 102, 103 etc. so you can't see the data unless you scroll through all 100 rows.

Don't hardcode the number of rows. The default should be 0. Then the addRow(...) method will add rows to the table one at a time. Scrollbars will then appear as necessary.

if i dont declare components as static..i get an error

You have a problem with your program structure. Using statics is NOT the way to fix it.

I suggest you read the section from the Swing tutorial on How to Use Table. The SimpleTableDemo will show you how to better structure your code. It simply creates a panel with all the components and adds the panel to the frame. All the variables are contained in the panel class.

0
Rodrigo V On

This is weird that you can’t see the scroll bar. Can you navigate down the table by selecting a row and pressing the arrow down in your keyboard? It seems that JVM is having problem to draw the component in the GUI. What Java version are you using?

Your code have several problems. As @Andrew Thompson said, the GUI components are static, fix that first.

Also, from the code you posted it seems that you are adding rows the table model without cleaning it before.

After you query the database you add more rows:

model.addRow(rowData);

But the table model already contains 100 empty rows added by the constructor:

model = new  DefaultTableModel(col,100);

So, before the loop to add rows, clean it first as below:

model.setRowCount(0);

Try those things and then update it here. The size of the fields may also impact the way that the table is displayed.

UPDATE:

By the screeshot you added, it seems that the problem is with the Panel that contains the JTable, not with the JTable itself. It seems that the area of the Panel that contains the JTable, is fixed and is bigger than the area that you have available in the screen.

Check the Layout choose for the Panel.