skwatamkar -4 Newbie Poster

here is the code for displaying the content of the database if a run the file 'java listall' then it will work well but when i try to run the code
from the main class with the help of JButton list as
if(ae.getSource() == list)
{
System.out.println("list working");
new listall();
}
but no table is shown what is code is needed so i will display this table with the help of button.
here is the code

import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;
import java.awt.event.MouseEvent;
import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;

public class listall extends JPanel
{
	protected String[] columnToolTips = {"The person's Record id", 
	"The person's First Name", .... };
	public listall() 
	{
		
        super(new GridLayout(1,0));
        setVisible(true);
        
        JTable table = new JTable(new MyTableModel()) {
		
	//Implement table cell tool tips.
            public String getToolTipText(MouseEvent e)
			{
                String tip = null;
                java.awt.Point p = e.getPoint();
                int rowIndex = rowAtPoint(p);
                int colIndex = columnAtPoint(p);
                int realColumnIndex = convertColumnIndexToModel(colIndex);

                if (realColumnIndex == 0) 
				{ 
                    tip = "This person's Record no. in the database is : "
                           + getValueAt(rowIndex, colIndex);
                }
                else if (realColumnIndex == 1) 
				{ 
                    tip = "This person's First Nane is : "
                           + getValueAt(rowIndex, colIndex);
                }
                else if (realColumnIndex == 2) 
				{ 
                    tip = "This person's Last Name is : "
                           + getValueAt(rowIndex, colIndex);
                }
                ....
                }
                else if (realColumnIndex == 30) 
				{ 
                    tip = "Other Notes : "
                           + getValueAt(rowIndex, colIndex);
                }
           
				else 
				{ 
                    tip = super.getToolTipText(e);
                }
                return tip;
            }

            //Implement table header tool tips. 
            protected JTableHeader createDefaultTableHeader()
			{
                return new JTableHeader(columnModel) 
				{
                    public String getToolTipText(MouseEvent e)
					{
                        String tip = null;
                        java.awt.Point p = e.getPoint();
                        int index = columnModel.getColumnIndexAtX(p.x);
                        int realIndex = columnModel.getColumn(index).getModelIndex();
                        return columnToolTips[realIndex];
                    }
                };
            }
        };
     
        table.setPreferredScrollableViewportSize(new Dimension(800, 600));
        table.setFillsViewportHeight(true);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
                
        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);

        //Add the scroll pane to this panel.
        add(scrollPane);
    }
	
	class MyTableModel extends AbstractTableModel 
	{
		Connection con;
		PreparedStatement pst;
		ResultSet rs,rs1;
		ResultSetMetaData rsm;
		Object[][] data;
		
        private String[] columnNames = {	"Record id",... };
        
		MyTableModel()
	{
		try{
			con=DriverManager.getConnection("jdbc:odbc:bdr","","");
			pst = con.prepareStatement("select count(*) from person");
			rs1 = pst.executeQuery();
			rs1.next();
			int rc = Integer.parseInt(rs1.getString(1));
			pst = con.prepareStatement("select * from person");
			rs = pst.executeQuery();
			rsm = rs.getMetaData();
			int cc= rsm.getColumnCount();
			String[] col = new String[cc];
			for(int i=0;i<cc;i++)
			{
				col[i]=rsm.getColumnLabel(i+1);
			}		
			data = new Object[rc][cc];
			int cnt=0;
			while(rs.next())	
			{
				for(int i=0;i<cc;i++)
				{
					data[cnt][i] = rs.getString(i+1);		
				}
				cnt++;
			}
			rs.close();
			con.close();	
	
		}catch(Exception e){}
	}

        public int getColumnCount()
		{
            return columnNames.length;
        }

        public int getRowCount()
		{
            return data.length;
        }

        public String getColumnName(int col)
		{
            return columnNames[col];
        }

        public Object getValueAt(int row, int col)
		{
            return data[row][col];
        }

       
        public Class getColumnClass(int c)
		{
            return getValueAt(0, c).getClass();
        }
										
    }
	
	private static void createAndShowGUI() 
	{
        //Create and set up the window.
        JFrame frame = new JFrame("listall");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        JComponent newContentPane = new listall();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true); 
       	frame.setSize(800,600);
    }

    public static void main(String[] args) 
	{
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
	
}

thanx in advance.
Have a good day!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.