如何在同一帧中显示JTable?

时间:2018-06-09 05:04:55

标签: java jtable

我的代码从数据库中获取员工列表,并将其显示在新框架中的JTable中。如何在同一帧中显示它?

import javax.swing.*;
import java.awt.*;
import java.sql.ResultSet;


public class Display {




    public Display() {

        final JFrame frame = new JFrame("Employee record");

        String[] columns = {"Id", "Name", "Contact", "email",
                            "Age", "Team", "Salary"};


        try{
        int i=0;
        DbHandler d=new DbHandler();
        ResultSet resultSet=d.display();
        while(resultSet.next()){

            i++;

        }
        resultSet.beforeFirst();
        String datas[][] = new String[i][8];
        int j=0;
        while(resultSet.next()){
            for(int k=1;k<=8;k++){

                datas[j][k-1]=resultSet.getString(k);
        }
            j++;
        }
        JTable table = new JTable(datas, columns);
        JScrollPane scrollPane = new JScrollPane(table);
        table.setFillsViewportHeight(true);

        JLabel lblHeading = new JLabel("Records");
        lblHeading.setFont(new Font("Arial",Font.TRUETYPE_FONT,24));

        frame.getContentPane().setLayout(new BorderLayout());

        frame.getContentPane().add(lblHeading,BorderLayout.PAGE_START);
        frame.getContentPane().add(scrollPane,BorderLayout.CENTER);


        frame.setSize(550, 200);
        frame.setVisible(true);
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }

} 

从名为Admin的另一个类调用display类,该类调用jtable框架。帮我在管理框架中显示表格。

Display d=new Display();
      d.setSize(new Dimension(600,600));
      d.setTitle("Display");
      d.setVisible(true);
      this.dispose();

0 个答案:

没有答案