Reputation: 127
I know there have been a lot of questions about JTables not showing but I can't find anything that relates to my current problem. I'm using JCreator Pro for my IDE and MS access as a database. Here's my code.
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;
}
}
Upvotes: 0
Views: 86
Reputation: 324118
Your question is about a table. 95% of the code posted has nothing to do with a table.
We are not interested in a core dump of your program. When you as a question post a proper SSCCE that demonstrates the problem because we don't have the time to read through hundreds of lines of code not related to the problem.
dgvProductInformation.setBounds(20,20,545,200);
add(scrollPane = new JScrollPane( dgvProductInformation ));
As a wild quess I would say the problem is that you never set the bounds of the scrollpane. Setting the bounds of the table doesn't do anything because a scroll pane uses its own layout manager.
Don't use a null layout!!! Don't use setBounds()!!!
Use layout managers. Swing was designed to be used with layout managers so you don't have to worry about setting the size and location of components. Learn how to use Swing properly.
Upvotes: 2