Jtable没有显示Java中

时间:2015-04-23 02:08:58

标签: java mysql swing jtable

我正在研究从数据库获取数据并将其传输到Java中的JTable的东西。我的问题是,收集的数据没有显示在我的JTable中。我尝试通过在Java中打印查询来调试它,它打印但是它没有在JTable中显示任何数据。请帮助我,提前谢谢。 TT_TT

这是我的UI代码

    /*
 * Programmer       : Inodeo, Claire Fatima R.
 * Date Developed   : April 20, 2015 
 * Company          : 
 * Purpose          : To gather data from database and display it 
 *                    to JTable. A desktop application which requires Java to be installed
 *                    in PC for it to run. For Java installer visit www.oracle.com and download the
 *                    latest version of Java. 
 * */               
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.*;


@SuppressWarnings("serial")
public class UserInterface extends JPanel
{



    private JButton  srch_btn;
    table_data obj;

    private JScrollPane scrollPane;

    //JLabel
    private JLabel   date_lbl;
    private JLabel   frm_lbl;
    private JLabel   to_lbl;
    private JLabel   pl_lbl;
    private JLabel   where_lbl;

    //SQL
    Connection con=null;
    Statement stmt = null;  

    //ComboBox
    private JComboBox<String> frm_drp;
    private JComboBox<String> to_drp;
    private JComboBox<String> brngy_drp;

    public static String names[] = {"Negros", "Panay","Leyte"};
    public static JComboBox<String> city_drp;

    //******CONSTRUCTOR*********************

    public UserInterface()
    {
        //construct components
        obj         = new table_data    ();
        srch_btn    = new JButton       ("Search");
        date_lbl    = new JLabel        ("DATE:");
        frm_lbl     = new JLabel        ("From             :");
        to_lbl      = new JLabel        ("To                  :");
        frm_drp     = new JComboBox<>   ();
        to_drp      = new JComboBox<>   ();
        pl_lbl      = new JLabel        ("PLACE");
        where_lbl   = new JLabel        ("Where           :");
        city_drp    = new JComboBox<>   (names); 
        brngy_drp   = new JComboBox<>   ();

        //adjust size and set layout
        setPreferredSize (new Dimension (900, 400));
        setLayout (null); 
        //*****************************SQL GOES HERE************************************
        //==================================================================================
        //=========================Combox for places==========================================
        //==================================================================================
        city_drp.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent e) {

                String chosenName =  (String) city_drp.getSelectedItem();
                //removes all the unecessary things in comboBox
                //prevents overlapping informations
                brngy_drp.removeAllItems();
                to_drp.removeAllItems();
                frm_drp.removeAllItems();

                if(chosenName.equals("Panay"))
                {

                    try {
                        con = DriverManager.getConnection("jdbc:mysql://:3306/ngcp_table","root","root");
                        stmt = con.createStatement();
                        ResultSet rs = stmt.executeQuery("SELECT DISTINCT Panay_Place, Panay_Date FROM ngcp_table.panay;");



                        while(rs.next())
                         {

                            brngy_drp.addItem   (rs.getString("Panay_Place"));    
                            frm_drp.addItem     (rs.getString("Panay_Date"));
                            to_drp.addItem      (rs.getString("Panay_Date"));  

                         }
                         rs.close();
                         stmt.close();
                         con.close();
                    } catch (SQLException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }else if(chosenName.equals("Negros"))
                {

                    try {
                        con = DriverManager.getConnection("jdbc:mysql://:3306/ngcp_table","root","root");
                        stmt = con.createStatement();
                        ResultSet rs = stmt.executeQuery("SELECT DISTINCT Negros_Place, Negros_Date FROM ngcp_table.negros;");

                        while(rs.next())
                         {

                                    brngy_drp.addItem   (rs.getString("Negros_Place")); 
                                    frm_drp.addItem (rs.getString("Negros_Date"));
                                    to_drp.addItem  (rs.getString("Negros_Date"));

                         }
                         rs.close();
                         stmt.close();
                         con.close();
                    } catch (SQLException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }else if(chosenName.equals("Leyte"))
                {

                    try {
                        con = DriverManager.getConnection("jdbc:mysql://:3306/ngcp_table","root","root");
                        stmt = con.createStatement();
                        ResultSet rs = stmt.executeQuery("SELECT DISTINCT Leyte_Place, Leyte_Date FROM ngcp_table.leyte;");

                        while(rs.next())
                         {
                                     brngy_drp.addItem  (rs.getString("Leyte_Place"));
                                     frm_drp.addItem    (rs.getString("Leyte_Date"));
                                     to_drp.addItem     (rs.getString("Leyte_Date"));


                         }
                         rs.close();
                         stmt.close();
                         con.close();
                    } catch (SQLException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            }

        });
        //***************************FOR TABLE***********************
        //====================SEARCH BUTTON==========================
        srch_btn.addMouseListener(new MouseListener() {

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mousePressed(MouseEvent arg0) {
                // TODO Auto-generated method stub

            }
            @Override                   
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                //loads the information from the database to the JTABLE


                scrollPane = obj.table((frm_drp.getSelectedIndex()+1),(to_drp.getSelectedIndex()+1),(brngy_drp.getSelectedIndex()+1)); //JScrollPane table(char sample_Place, char sample_Date, int KW,int KV)

                System.out.println("this is " +frm_drp.getSelectedItem());
                System.out.println("this is " +to_drp.getSelectedItem());
                System.out.println("this is " +brngy_drp.getSelectedItem()); 

                if (scrollPane == null) {
                        scrollPane.setSize(100, 300); //END OF TABLE CODES
                        System.out.println("no");

                    } else {
                        add(scrollPane);
                        System.out.println("yes");
                    }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public void mouseExited(MouseEvent e) {
                // TODO Auto-generated method stub

            }


        });//*********************END OF SEARCH BUTTON CODE******************

        //=============================================================================
        //===========COMPONENTS WILL BE ADDED HERE AND SIZE WILL BE SET================
        //=============================================================================

        //add components
        add (srch_btn);
        add (date_lbl);
        add (frm_lbl);
        add (to_lbl);
        add (frm_drp);
        add (to_drp);
        add (pl_lbl);
        add (where_lbl);
        add (city_drp);
        add (brngy_drp);
        scrollPane = obj.table((frm_drp.getSelectedIndex()+1),(to_drp.getSelectedIndex()+1),(brngy_drp.getSelectedIndex()+1));
        add (scrollPane);

        //*******set component bounds (only needed by Absolute Positioning)***********
        srch_btn.setBounds (140, 240, 105, 25);
        date_lbl.setBounds (25, 40, 100, 25);
        frm_lbl.setBounds (45, 70, 100, 25);
        to_lbl.setBounds (45, 110, 100, 25);
        frm_drp.setBounds (100, 70, 150, 25);
        to_drp.setBounds (100, 105, 150, 25);
        pl_lbl.setBounds (25, 140, 100, 25);
        where_lbl.setBounds (40, 170, 100, 25);
        city_drp.setBounds (100, 170, 150, 25);
        brngy_drp.setBounds (100, 200, 150, 25);

    }

    //===================================================================================
    //=============================MAIN PROGRAM GOES HERE================================
    //===================================================================================
     public static void main (String[] args) 
     {
            JFrame frame = new JFrame ("NGCP DATA SYSTEM");
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add (new UserInterface());
            frame.pack();
            frame.add(new JScrollPane());
            frame.setVisible (true);
      }
}

这是我的表格代码

    //for SQL
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//javaSwing
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class table_data extends JFrame 
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private JTable table_Panel;
    private JScrollPane scrollPane;
    private DefaultTableModel dtm;
    Connection con=null;
    Statement stmt = null;  

    @SuppressWarnings("serial")
    public JScrollPane table(int Date,int Date2, int place)
    {
        String columnLabel[] = {"Date","KW","KV"};

        dtm = new DefaultTableModel();
        dtm.addColumn(columnLabel[0]);
        dtm.addColumn(columnLabel[1]);
        dtm.addColumn(columnLabel[2]);

        //creates a static table

        try {
            con = DriverManager.getConnection("jdbc:mysql://:3306/ngcp_table","root","root");
            stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery
                    (
                                    "SELECT * FROM ngcp_table.negros "
                                    + "WHERE Negros_Place = '"+place+"'AND Negros_Date BETWEEN '" + Date + "' AND '" + Date2 + "'"


                    );
            System.out.println("Hellowsss");

            while(rs.next())
            {                   
                    dtm.addRow(new Object[]
                            { 

                                rs.getString("Negros_Date"), rs.getString("KW"), rs.getString("KV")                         
                            }
                    );
                    System.out.println("YEAH VAERY NICE");
            }

             rs.close();
             stmt.close();
             con.close();

        } catch (SQLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        table_Panel = new JTable(dtm);


        scrollPane = new JScrollPane(table_Panel);
        scrollPane.setVisible(true);
        scrollPane.setBounds(280, 15, 600, 380); //setbounds(x,y,width,lenth);


        return scrollPane;

    }
}

0 个答案:

没有答案