简体   繁体   中英

How do I make a jtable with mySQL data appear in my tabbed pane instead of in a seperate window?

I using java NetBeans for the first time to create my own simple point of sale system. I Have this code:

package pos_system;

import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class Table extends JFrame
{
    public Table()
    {
       ArrayList columnNames = new ArrayList();
       ArrayList data = new ArrayList();

       //  Connect to an MySQL Database, run query, get result set
       String url = "jdbc:mysql://localhost:3306/mydb";
       String userid = "root";
       String password = "nbuser";
       String sql = "SELECT * FROM inventorytable";


       try (Connection connection = DriverManager.getConnection( url,   userid, password );
          Statement stmt = connection.createStatement();
          ResultSet rs = stmt.executeQuery( sql ))
        {
              ResultSetMetaData md = rs.getMetaData();
              int columns = md.getColumnCount();

          //  Get column names
            for (int i = 1; i <= columns; i++)
            {
                columnNames.add( md.getColumnName(i) );
            }

        //  Get row data
            while (rs.next())
            {
                ArrayList row = new ArrayList(columns);

                for (int i = 1; i <= columns; i++)
                {
                    row.add( rs.getObject(i) );
                }

                data.add( row );
            }
        }
        catch (SQLException e)
       {
            System.out.println( e.getMessage() );
       }

    // Create Vectors and copy over elements from ArrayLists to them
    // Vector is deprecated but I am using them in this example to keep 
    // things simple - the best practice would be to create a custom defined
    // class which inherits from the AbstractTableModel class
    Vector columnNamesVector = new Vector();
    Vector dataVector = new Vector();

    for (int i = 0; i < data.size(); i++)
    {
        ArrayList subArray = (ArrayList)data.get(i);
        Vector subVector = new Vector();
        for (int j = 0; j < subArray.size(); j++)
        {
            subVector.add(subArray.get(j));
        }
        dataVector.add(subVector);
    }

    for (int i = 0; i < columnNames.size(); i++ )
        columnNamesVector.add(columnNames.get(i));

        //  Create table with database data    
        JTable table = new JTable(dataVector, columnNamesVector)
        {
            public Class getColumnClass(int column)
            {
               for (int row = 0; row < getRowCount(); row++)
                {
                    Object o = getValueAt(row, column);

                    if (o != null)
                    {
                        return o.getClass();
                    }
                }

                return Object.class;
            }
        };

        JScrollPane scrollPane = new JScrollPane( table );
        getContentPane().add( scrollPane );

        JPanel buttonPanel = new JPanel();
        getContentPane().add( buttonPanel, BorderLayout.SOUTH );
    }

 public static void main(String[] args)
 {
        Table frame = new Table();
        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
        frame.pack();
        frame.setVisible(true);
    }
   }

Which creates the jtable I need, but is in a separate class than My Jframe that has the tabbed pane where I would like the jtable to appear. How do I connect this class to the tabbed pane, or is there another way I should display the table?

You are painting yourself in a corner by having your class extend JFrame, forcing you to create and display JFrames, when often more flexibility is called for. In fact, I would venture that most of the Swing GUI code that I've created and that I've seen does not extend JFrame, and in fact it is rare that you'll ever want to do this. More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM