组合框没有显示出来

时间:2014-05-09 05:20:22

标签: java eclipse swing ms-access

所以,我的要求是使用Java中的JTable显示来自数据库的记录。我提到了this link。 但错误是,当我在Eclipse中运行它时,我看不到组合框,因为理想情况下它应该出现。如何解决?

 import java.awt.*;
    import java.awt.event.*;
    import java.sql.*;
    import java.util.Vector;

    import javax.swing.*;
    import javax.swing.table.DefaultTableModel;

    public class DisplayEmpData extends JFrame implements ActionListener {

        JFrame frame1;
        JLabel l0, l1, l2;
        JComboBox c1;
        JButton b1;
        Connection con;
        ResultSet rs, rs1;
        Statement st, st1;
        PreparedStatement pst;
        String ids;
        static JTable table;
        String[] columnNames = {"User name", "Email", "Password", "Country"};
        String from;

        DisplayEmpData() {

            l0 = new JLabel("Fatching Employee Information");
            l0.setForeground(Color.red);
            l0.setFont(new Font("Serif", Font.BOLD, 20));
            l1 = new JLabel("Select name");
            b1 = new JButton("submit");

            l0.setBounds(100, 50, 350, 40);
            l1.setBounds(75, 110, 75, 20);
            b1.setBounds(150, 150, 150, 20);
            b1.addActionListener(this);

            setTitle("Fetching Student Info From DataBase");
            setLayout(null);
            setVisible(true);
            setSize(500, 500);
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

            add(l0);
            add(l1);;
            add(b1);
            try {

                String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + "C:\\users\\ppreeti\\emp_details.accdb";
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                con=DriverManager.getConnection(url,"","");
                /*con = DriverManager.getConnection("jdbc:oracle:thin:@mcndesktop07:1521:xe", "sandeep", "welcome");*/
                st = con.createStatement();
                rs = st.executeQuery("select uname from emp");
                Vector v = new Vector();
                while (rs.next()) {
                    ids = rs.getString(1);
                    v.add(ids);
                }
                c1 = new JComboBox(v);
                c1.setBounds(150, 110, 150, 20);

                add(c1);
                st.close();
                rs.close();
            } catch (Exception e) {
            }
        }

        public void actionPerformed(ActionEvent ae) {
            if (ae.getSource() == b1) {
                showTableData();
            }

        }

        public void showTableData() {

            frame1 = new JFrame("Database Search Result");
            frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame1.setLayout(new BorderLayout());
    //TableModel tm = new TableModel();
            DefaultTableModel model = new DefaultTableModel();
            model.setColumnIdentifiers(columnNames);
    //DefaultTableModel model = new DefaultTableModel(tm.getData1(), tm.getColumnNames());
    //table = new JTable(model);
            table = new JTable();
            table.setModel(model);
            table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
            table.setFillsViewportHeight(true);
            JScrollPane scroll = new JScrollPane(table);
            scroll.setHorizontalScrollBarPolicy(
                    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            scroll.setVerticalScrollBarPolicy(
                    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            from = (String) c1.getSelectedItem();
    //String textvalue = textbox.getText();
            String uname = "";
            String email = "";
            String pass = "";
            String cou = "";

            try {
                pst = con.prepareStatement("select * from emp where UNAME='" + from + "'");
                ResultSet rs = pst.executeQuery();
                int i = 0;
                if (rs.next()) {
                    uname = rs.getString("uname");
                    email = rs.getString("umail");
                    pass = rs.getString("upass");
                    cou = rs.getString("ucountry");
                    model.addRow(new Object[]{uname, email, pass, cou});
                    i++;
                }
                if (i < 1) {
                    JOptionPane.showMessageDialog(null, "No Record Found", "Error", JOptionPane.ERROR_MESSAGE);
                }
                if (i == 1) {
                    System.out.println(i + " Record Found");
                } else {
                    System.out.println(i + " Records Found");
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
            frame1.add(scroll);
            frame1.setVisible(true);
            frame1.setSize(400, 300);
        }

        public static void main(String args[]) {
            new DisplayEmpData();
        }
    }

2 个答案:

答案 0 :(得分:2)

首先,我的建议是为代码中的方法和字段使用更有意义的名称:它现在看起来很清晰,但对于外部读者和您自己在几个月内代码确实赢得了什么显而易见。

也就是说,使用LayoutManager可能会更好,这可以确保在平台上正确Component放置和可见性(正如@Andrew Thompson已经指出的那样)。

基本上,第一步是创建一个JPanel并将其添加到您的JFrame:

JPanel framePanel;
framePanel = new JPanel();
this.add(framePanel);

如果JPanel()构造函数中未指定参数,则布局会自动设置为BorderLayout。此布局有5个区域,您可以放置​​组件:北,南,西,东,中心。

您现在可以向JPanels添加其他framePanel,如下所示:

JPanel northPanel;
JPanel southPanel;
JPanel westPanel;
JPanel eastPanel;
JPanel centerPanel;

northPanel = new JPanel();
southPanel = new JPanel();
westPanel = new JPanel();
eastPanel = new JPanel();
centerPanel = new JPanel();

framePanel.add(northPanel, BorderLayout.NORTH);
framePanel.add(southPanel, BorderLayout.SOUTH);
framePanel.add(westPanel, BorderLayout.WEST);
framePanel.add(eastPanel, BorderLayout.EAST);
framePanel.add(centerPanel, BorderLayout.CENTER);

每个面板可能包含另一个JPanel或您的一个组件。可用于放置多个组件的最有用和最基本的LayoutManager之一是GridLayout,它允许您指定网格大小(行数和列数):每次添加组件时,它都是放在从左上角开始的第一个空闲位置。

如果您要为northPanel添加3个组件,则需要编码

northPanel.setLayout(new GridLayout(1, 3));

northPanel.add(component1);
northPanel.add(component2);
northPanel.add(component3);

这些只是示例,如果您搜索LayoutManager,可以在Java API中找到更多信息。只是一些快速提示:

  • 您可以直接在Layout构造函数中指定JPanel

    JPanel testPanel = new JPanel(new GridLayout(rows, columns));

  • 您可以在网格位置插入空JPanel来添加空白区域:

    JPanel whiteSpacePanel = new JPanel();   
    
    testPanel.add(component1);
    testPanel.add(whiteSpacePanel);
    testPanel.add(component2);
    
  • 您可以在Components构造函数中的Layout之间指定一些基本边框

    JPanel panelWithBorders = new JPanel(new GridLayout(rows, columns, xBorder, yBorder));

参考文献:

答案 1 :(得分:0)

代码中的问题是在设置frame的visibily = true后添加了组合框。请在设置框架可见性之前添加所有组件。我改变了你的构造函数。请注意,使用空布局不是一个好的习惯(我认为是这样)使用任何LayoutManagers

DisplayEmpData() {

        l0 = new JLabel("Fatching Employee Information");
        l0.setForeground(Color.red);
        l0.setFont(new Font("Serif", Font.BOLD, 20));
        l1 = new JLabel("Select name");
        b1 = new JButton("submit");

        l0.setBounds(100, 50, 350, 40);
        l1.setBounds(75, 110, 75, 20);
        b1.setBounds(150, 150, 150, 20);
        b1.addActionListener(this);

        setTitle("Fetching Student Info From DataBase");
        setLayout(null);

        setSize(500, 500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        add(l0);
        add(l1);;
        add(b1);
        try {

            Vector v = new Vector();

            String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + "C:\\users\\ppreeti\\emp_details.accdb";
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con=DriverManager.getConnection(url,"","");
            /*con = DriverManager.getConnection("jdbc:oracle:thin:@mcndesktop07:1521:xe", "sandeep", "welcome");*/
            st = con.createStatement();
            rs = st.executeQuery("select uname from emp");

            while (rs.next()) {
                ids = rs.getString(1);
                v.add(ids);
            }
            c1 = new JComboBox(v);
            c1.setBounds(150, 110, 150, 20);

            add(c1);
            st.close();
            rs.close();
        } catch (Exception e) {
        }

        setVisible(true);
    }