[Help-Gấp] Lỗi về JTable

BaBYMagNum

New Member
29/4/16
7
2
1
22
e tạo 1 hàm tạo bảo trong Frame và hàm để truyền dữ liệu vào (đã kết nối thành công vs SQL)
Code:
public JTable CreatTable (JTable table) {
        DefaultTableModel tModel = new DefaultTableModel();
        String []colsName = {"ID", "TênThíSinh", "Place", "Date", "Sex", "Math", "Physical", "Chemistry"};
        tModel.setColumnIdentifiers(colsName);
        table.setModel(tModel);
        JScrollPane scroll = JTable.createScrollPaneForTable(table);
        this.add(scroll);      
        this.setVisible(true);
        updateData(view(), table, tModel);
      
        return table;

public void updateData(ResultSet result, JTable table, DefaultTableModel tableModel){
        String [] colsName = {"ID", "TênThíSinh", "Place", "Date", "Sex", "Math", "Physical", "Chemistry"};
        tableModel.setColumnIdentifiers(colsName);
        table.setModel(tableModel);
        try {
            while(result.next()) {
                String [] row = new String[8];
                row[0] = result.getString(1);
                row[1] = result.getString(2);
                row[2] = result.getString(3);
                row[3] = result.getString(4);
                row[4] = result.getString(5);
                row[5] = result.getString(6);
                row[6] = result.getString(7);
                row[7] = result.getString(8);
                tableModel.addRow(row);
            }
            table.setModel(tableModel);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
   
    public ResultSet view(){
        ResultSet result = null;
        try {
            Statement statement = conn.createStatement();
            return statement.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }
    }
xong e khai báo
Code:
CreatTable(table);
        table.setBounds(10, 100, 450, 250);
        //JScrollPane scroll = JTable.createScrollPaneForTable(table);
        //table.setFillsViewportHeight(true);
        mainPanel.add(table);
nhưng kết quả thì nó ko hiện rowHeader và setBounds cho nó ngắn lại thì cũng ko hiện thanh kéo


Ai fix hộ e ới :((
 

Joe

Thành viên VIP
21/1/13
2,732
1,255
113
Your car won't move and you say to us "IT STOPS !" How can we know it's your car? You posted the wrong part and asked us for things that we have to divine. How can we guess that your setBound() is correct?
 

BaBYMagNum

New Member
29/4/16
7
2
1
22
ok this's all of MyFrame...i just want to find and fix the error of JTable so dont worry about sth else :D
Code:
public class MyFrame extends JFrame  {
   
    private ConnectTo c = new ConnectTo();
    private Connection conn = c.getConnect();
   
    private String sql = "select ID, TenThiSinh, TenTinh, NgaySinh, ts as GioiTinh, Toan, Ly, Hoa from thisinh, tinhthanh, sex where MaQue = MaTinh and GioiTinh = sex.ms order by ID asc";
    private static String ID;
    private JMenuBar myMenu = new MyMenu(this);
    private Panelinfo panelinfo = new Panelinfo(this);
   
    private JTable table = new JTable();
   
   
    private MyToolbar myToolbar = new MyToolbar(this);
    private PanelFiltre panelFiltre = new PanelFiltre(this);
    private JLabel info = new JLabel("Student Infomation");
   
    JButton nhap = new JButton("Nhập mới");
    private JButton exit = new JButton("Exit");
   
    public MyFrame() {
       
        // Initialization
        this.setTitle("Project: Student");
        this.setSize(850, 480);
        this.setLocation(200, 100);
        //this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setJMenuBar(myMenu);
       
        // Add the interface
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(null);
        this.setContentPane(mainPanel);
        //mainPanel.setBackground(Color.PINK);
       
        CreatTable(table);
        table.setBounds(10, 100, 450, 250);
        //JScrollPane scroll = JTable.createScrollPaneForTable(table);
        //table.setFillsViewportHeight(true);
        mainPanel.add(table);
       
        info.setBounds(15, 75, 130, 30);
        mainPanel.add(info);
       
        nhap.setBounds(490, 50, 90, 30);
        mainPanel.add(nhap);
        nhap.addActionListener(this);
       
        exit.setBounds(730, 365, 70, 30);
        mainPanel.add(exit);
        exit.addActionListener(this);
     
        panelinfo.setBounds(490, 100, 310, 260);
        //panelinfo.setBackground(Color.cyan);
        mainPanel.add(panelinfo);
       
        myToolbar.setBounds(490, 7, 265, 40);
        //myToolbar.setBackground(Color.blue);
        mainPanel.add(myToolbar);
       
        panelFiltre.setBounds(40, 7, 385, 71);
        //panelFind.setBackground(Color.MAGENTA);
        mainPanel.add(panelFiltre);
       
    }
public JTable CreatTable (JTable table) {
        DefaultTableModel tModel = new DefaultTableModel();
        String []colsName = {"ID", "TênThíSinh", "Place", "Date", "Sex", "Math", "Physical", "Chemistry"};
        tModel.setColumnIdentifiers(colsName);
        table.setModel(tModel);
        JScrollPane scroll = JTable.createScrollPaneForTable(table);
        this.add(scroll);       
        this.setVisible(true);
        updateData(view(), table, tModel);
       
        return table;
    }
   
    public void updateData(ResultSet result, JTable table, DefaultTableModel tableModel){
        String [] colsName = {"ID", "TênThíSinh", "Place", "Date", "Sex", "Math", "Physical", "Chemistry"};
        tableModel.setColumnIdentifiers(colsName);
        table.setModel(tableModel);
        try {
            while(result.next()) {
                String [] row = new String[8];
                row[0] = result.getString(1);
                row[1] = result.getString(2);
                row[2] = result.getString(3);
                row[3] = result.getString(4);
                row[4] = result.getString(5);
                row[5] = result.getString(6);
                row[6] = result.getString(7);
                row[7] = result.getString(8);
                tableModel.addRow(row);
            }
            table.setModel(tableModel);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
   
    public ResultSet view(){
        ResultSet result = null;
        try {
            Statement statement = conn.createStatement();
            return statement.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }
   
    public ResultSet ShowText (String sql) {
        try {
            Statement statement = conn.createStatement();
            return statement.executeQuery(sql);
        } catch (SQLException e) {
            return null;
        }
       
    }
 

Joe

Thành viên VIP
21/1/13
2,732
1,255
113
boy,
1) if you want help you have to post the part that causes the problem. If you know where it is (as you did before I asked you) then why you ask this forum?
2) it's normal that people help only when they have time and voluntarily do. No begging, no urging. It's impolite and it's against the etiquette and social behavior
3) patience is the mother of every success. This forum is a place where no one is obliged to answer or to reply anyone.
Is that clear for you, my boy?
Back to your codes. As a newbie you have to read and reread the APIs you used and think about what they say. It's in English, but without English you get nowhere. Just study the Job-offers of folder "Tuyển dụng nhân sự CNTT - IT recruitment" then you'll understand what I mean. And that's the main reason why I answer and write in English even I do understand and write in Vietnamese.
I don't fix your codes, but give you the hints to fix your problem. Only that you may learn and understand what Object Oriented Programming means.
1) the method setBound() belongs to the JFrame API and is used to set a fixed form of an object that's included. In this case: the table is fixed to a location x=10, y=100 and with a height of 250, width 450.
2) the table you want to put in is neither defined how big it is nor limited to a certain form. And that means it takes the default form that contains ALL components that fit into the table.
3) the result is that the default form is probably too big to fit into the form of JFrame and that causes the friction or trouble that something is cut off...
Solution: look at the JTable API and search for the way how to "limit" the size of your table.
 

Joe

Thành viên VIP
21/1/13
2,732
1,255
113
...still on the search?
your codes are messy and won't work. A JTable needs a model and your table hasn't. Read THIS TUTORIAL. It's impossible to redesign or fix your codes without a model.
 

BaBYMagNum

New Member
29/4/16
7
2
1
22
my English is not well so i think i don't know more about impolite in the word but actually i urged. So i'm sorry.
but i think i spent too much time for this problem. i searched how to fix this problem as well as another method. i tried use AbstractTableModel but ... i can't find the real problem so i must to create this topic. I really don't have more time to complete this exercise.
Tks about the hints you gave! I will try again and complete my exercise. But if you have time, please fix my code and i will take it as a basis. Thank you again ! :)
 

Joe

Thành viên VIP
21/1/13
2,732
1,255
113
Boy, no problem. No Try, No win. You should write and read in English. It's for your very own future and if you want later to get a GOOD-WELL-PAID job. By the way, you can study the examples of this site HERE
 

BaBYMagNum

New Member
29/4/16
7
2
1
22
mình đã thử như thế nhiều rồi...2 frame riêng chỉ có bảng thì ổn...nhưng cho vào frame chính vs đoạn code gần như y hệt thì lại ko đc :(
mình tạo thêm 1 class nữa với đoạn code tương tự thì cũng chỉ đc như này
 
  • Like
Reactions: Joe

BaBYMagNum

New Member
29/4/16
7
2
1
22
mình phải extends JPanel và add table vào panel...xong add vào Frame như những panel khác :-ss chứ extends JTable xong add vào Frame thì nó ko hiện
Code:
public class MyTable extends JPanel {
   
    private ConnectTo c = new ConnectTo();
    private Connection conn = c.getConnect();
   
    JPanel panel = new JPanel();
    private JTable table = new JTable();
    String []colsName = {"ID", "TênThíSinh", "QuêQuán", "NgàySinh", "GiớiTính", "Toán", "Lý", "Hoá"};
    private DefaultTableModel tableModel = new DefaultTableModel(colsName, 0);
    private String sql = "select ID, TenThiSinh, TenTinh, NgaySinh, ts as GioiTinh, Toan, Ly, Hoa from thisinh, tinhthanh, sex where MaQue = MaTinh and GioiTinh = sex.ms order by ID asc";
    private static String ID;
    private Panelinfo panelinfo = new Panelinfo(null);
   
    private MyFrame myFrame = null;
   
    public MyTable (MyFrame myFrame) {
        this.myFrame = myFrame;
        panel.add(table);    
       
        //TableColumn col = null;
        //for (int i = 0; i < 8; i++) {
        //    col = table.getColumnModel().getColumn(i);
        //    col.setPreferredWidth(120);
        //}
       
        table.setModel(tableModel);
        table.setEnabled(true);
        initComponent(true); 
        updateData(view());
       
    }
   
    public void updateData(ResultSet result){
        String [] colsName = {"ID", "TênThíSinh", "QuêQuán", "NgàySinh", "GiớiTính", "Toán", "Lý", "Hoá"};
        tableModel.setColumnIdentifiers(colsName);
        table.setModel(tableModel);
        try {
            while(result.next()) {
                String [] row = new String[8];
                row[0] = result.getString(1);
                row[1] = result.getString(2);
                row[2] = result.getString(3);
                row[3] = result.getString(4);
                row[4] = result.getString(5);
                row[5] = result.getString(6);
                row[6] = result.getString(7);
                row[7] = result.getString(8);
                tableModel.addRow(row);
            }
            table.setModel(tableModel);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
   
    public void initComponent(Boolean b){
        this.setSize(700, 150);
        JScrollPane scroll = JTable.createScrollPaneForTable(table); 
        this.add(scroll);
        this.setVisible(b);
    }
   
    public ResultSet view(){
        ResultSet result = null;
        try {
            Statement statement = conn.createStatement();
            return statement.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }
 
  • Like
Reactions: Joe

Joe

Thành viên VIP
21/1/13
2,732
1,255
113
=D> Bravos !!!:-bd
 
Sửa lần cuối:

JackV

Administrator
Staff member
Tham khảo đoạn code này nhé, cách bạn viết có vấn đề đó
PHP:
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        getContentPane().setLayout(null);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(50, 10, 452, 312);
 
Sửa lần cuối: