JTable不会显示

时间:2016-03-09 04:24:36

标签: java swing jtable awt

我知道有很多关于JTable没有显示的问题,但我找不到任何与我当前问题有关的问题。我正在使用JCreator Pro作为数据库进行IDE和MS访问。这是我的代码。

import javax.swing.*;
import javax.swing.table.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.sql.*;
import java.util.*;
public class JCashiering extends JFrame implements ActionListener,FocusListener {
Container c;
JLabel lblProductCode,lblProductName,lblUnit,lblPrice,lblQuantity,lblAmountTendered,lblTotalAmountDue,lblPayment,lblChange,lblFind;
JTextField txtProductCode,txtProductName,txtPrice,txtQuantity,txtAmountTendered,txtTotalAmountDue,txtPayment,txtChange,txtFind,txtUnit;
JTable dgvProductInformation;
JPanel buttonPanel;
JButton btnFind,btnAdd,btnRemove,btnProceed,btnCancel,btnExit;
JScrollPane scrollPane;
Vector columnNames;
Vector data;

static Connection con;
static ResultSet rs;
static Statement stmt;

static int Quantity=0;
static double Change=0.0;

public JCashiering() {
    c=getContentPane();
    setLayout(null);

    //addlbl
    add(lblProductCode = new JLabel("Product Code:"));
    add(lblProductName = new JLabel("Product Name:"));
    add(lblUnit = new JLabel("Unit:"));
    add(lblPrice = new JLabel("Price:"));
    add(lblQuantity = new JLabel("Quantity:"));
    add(lblAmountTendered = new JLabel("Amount tendered:"));
    add(lblTotalAmountDue = new JLabel("Total amout due:"));
    add(lblPayment = new JLabel("Payment:"));
    add(lblChange = new JLabel("Change:"));
    //setboundslbl1st
    lblProductCode.setBounds(20,250,100,50);
    lblProductName.setBounds(20,280,100,50);
    lblUnit.setBounds(20,310,100,50);
    lblPrice.setBounds(20,340,100,50);
    lblQuantity.setBounds(20,370,100,50);
    //setboundslbl2nd
    lblAmountTendered.setBounds(300,250,130,50);
    lblTotalAmountDue.setBounds(300,280,120,50);
    lblPayment.setBounds(300,310,100,50);
    lblChange.setBounds(300,340,100,50);
    //addtxt
    add(txtProductCode=new JTextField());
    add(txtProductName=new JTextField());
    add(txtPrice=new JTextField());
    add(txtUnit=new JTextField());
    add(txtQuantity =new JTextField());
    add(txtAmountTendered = new JTextField());
    add(txtTotalAmountDue = new JTextField());
    add(txtPayment = new JTextField());
    add(txtChange = new JTextField());
    //settxt&cbo1st
    txtProductCode.setBounds(110,265,150,20);
    txtProductName.setBounds(110,295,150,20);
    txtUnit.setBounds(110,325,150,20);
    txtPrice.setBounds(110,355,150,20);
    txtQuantity.setBounds(110,385,150,20);
    //settxt2nd
    txtAmountTendered.setBounds(410,265,150,20);
    txtTotalAmountDue.setBounds(410,295,150,20);
    txtPayment.setBounds(410,325,150,20);
    txtChange.setBounds(410,355,150,20);
    //btnadd
    add(btnAdd = new JButton("Add to cart"));
    add(btnFind = new JButton("Find"));
    add(btnRemove = new JButton("Remove"));
    add(btnProceed = new JButton("Proceed"));
    add(btnCancel = new JButton("Cancel"));
    add(btnExit = new JButton("Exit"));
    //find
    add(lblFind = new JLabel("Find:"));
    lblFind.setBounds(20,230,150,20);
    add(txtFind = new JTextField());
    txtFind.setBounds(60,230,500,20);
    //btnset
    btnAdd.setBounds(110,420,100,25);
    btnAdd.addActionListener(this);
    btnRemove.setBounds(110,450,100,25);
    btnRemove.addActionListener(this);
    btnProceed.setBounds(410,385,150,25);
    btnProceed.addActionListener(this);
    btnCancel.setBounds(300,420,100,25);
    btnCancel.addActionListener(this);
    btnExit.setBounds(300,450,100,25);
    btnExit.addActionListener(this);
    //focuslistener
    txtQuantity.addFocusListener(this);
    txtPayment.addFocusListener(this);
    //findlistener
    txtFind.addActionListener(this);
    //Disablebtns
    setDisabled(txtProductCode,txtPrice,txtAmountTendered,txtTotalAmountDue,txtChange,txtUnit,txtProductName);
    //dgv
    columnNames = new Vector();
    data = new Vector();

    try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection connect =DriverManager.getConnection("jdbc:odbc:dbCashiering");

            String sql = "Select * from tblProduct";
            Statement stmt = connect.createStatement();
            ResultSet rs = stmt.executeQuery( sql );
            ResultSetMetaData md = rs.getMetaData();
            int columns = md.getColumnCount();
            for (int i = 1; i <= columns; i++) {
            columnNames.addElement( md.getColumnName(i) );
        }
            while (rs.next()) {
            Vector row = new Vector(columns);
            for (int i = 1; i <= columns; i++) {
            row.addElement( rs.getObject(i) );
        }
        data.addElement( row );
        }
        rs.close();
        stmt.close();
        }
        catch(Exception e) {
        JOptionPane.showMessageDialog(null,"Connection failed!");
        }
        //adddgv
    add(dgvProductInformation =new JTable(data, columnNames));
    dgvProductInformation.setBounds(20,20,545,200);
    add(scrollPane = new JScrollPane( dgvProductInformation ));
    add(buttonPanel = new JPanel());
    //frm
    setSize(600,600);
    setVisible(true);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//connection method
/*public static void connect(){
    try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:dbCashiering");
            stmt = con.createStatement();
            //JOptionPane.showMessageDialog(null,"Connection successful!");
        }
    catch(Exception ex){
            JOptionPane.showMessageDialog(null,"Connection failed!");
        }
}*/

//event
        //actionEventdgv
public void actionPerformed(ActionEvent e){
    if(e.getSource()==btnExit){
        System.exit(0);
    }
    if(e.getSource()==btnCancel){
        Clear(txtProductCode,txtProductName,txtPrice,txtQuantity,txtAmountTendered,txtTotalAmountDue,txtPayment,txtChange,txtFind,txtUnit);
    }
        //Event for Find
    if(e.getSource()==txtFind){
        //find action here
        JOptionPane.showMessageDialog(null,"wow");
    }
}
    //focusEvent
public void focusGained(FocusEvent f){
}
public void focusLost(FocusEvent f){
    //validation for field empty
    if(f.getSource()==txtQuantity){
        if(isEmpty(txtQuantity));
    }
    if(f.getSource()==txtPayment){
        if(isEmpty(txtQuantity));
    }
    //validation for correct format
    if(f.getSource()==txtQuantity){
        if(isCorrectFormat(txtQuantity)==false);
    }
    if(f.getSource()==txtPayment){
        if(isCorrectFormat(txtPayment)==false);
    }
}

//main
    public static void main(String[] args) {
    //connect();
    new JCashiering();
}




//method
    public static void setDisabled(JTextField txt1,JTextField txt2,JTextField txt3,JTextField txt4,
    JTextField txt5,JTextField txt6,JTextField txt7){
    txt1.setEnabled(false);
    txt2.setEnabled(false);
    txt3.setEnabled(false);
    txt4.setEnabled(false);
    txt5.setEnabled(false);
    txt6.setEnabled(false);
    txt7.setEnabled(false);
    }
    public static void Clear(JTextField txt1,JTextField txt2,JTextField txt3,JTextField txt4,
    JTextField txt5,JTextField txt6,JTextField txt7,JTextField txt8,JTextField txt9,JTextField txt10){
        txt1.setText("");
        txt2.setText("");
        txt3.setText("");
        txt4.setText("");
        txt5.setText("");
        txt6.setText("");
        txt7.setText("");
        txt8.setText("");
        txt9.setText("");
        txt10.setText("");
    }
    public static boolean isEmpty(JTextField txt){
        if(txt.getText().trim().length()==0){
        JOptionPane.showMessageDialog(null,"Field cannot be empty!");
        txt.requestFocus(true);
        return true;
    }
    else return false;
    }
    public static boolean isCorrectFormat(JTextField txt){
        char temp= ' ';
        for(int i=0;i<txt.getText().length();i++){
        temp=txt.getText().charAt(i);
        if(Character.isDigit(temp)){
        return true;
        }
        else{
            JOptionPane.showMessageDialog(null,"Invalid number format!");
        txt.requestFocus(true);
        }
        return false;
        }
        return true;
    }

}

1 个答案:

答案 0 :(得分:2)

你的问题是关于一张桌子。发布的代码中有95%与表格无关。

我们对您的计划的核心转储不感兴趣。当你作为一个问题发布一个适当的SSCCE来证明问题,因为我们没有时间阅读与问题无关的数百行代码。

dgvProductInformation.setBounds(20,20,545,200);
add(scrollPane = new JScrollPane( dgvProductInformation ));

作为一个狂野的问题我会说问题是你永远不会设置滚动窗格的界限。设置表的边界不起作用,因为滚动窗格使用自己的布局管理器。

不要使用空布局!不要使用setBounds()!!!

使用布局管理器。 Swing旨在与布局管理器一起使用,因此您不必担心设置组件的大小和位置。了解如何正确使用Swing。