2

我正在为一家家族企业制定一个计划,该计划将管理其原材料供应商的宣传册。我发现您将需要所有代码,因此我将其发布在 pastebin 上。http://pastebin.com/Gc3aLe10

而且,在这里我附上我认为问题所在的地方:

tnBuscar.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {

            String search = searchField.getText();
            Connection con = null;
            java.sql.Statement st = null;
            ResultSet rs = null;

            String url = "jdbc:mysql://localhost:3306/";
            String db = "FAVEGA";
            String driver = "com.mysql.jdbc.Driver";
            String user = "root";
            String pass = "";
            try {
                Class.forName(driver);
                con = DriverManager.getConnection(url + db, user, pass);
                con.setAutoCommit(false);
                st = con.createStatement();
                String sql = "SELECT * FROM catalogos WHERE id = '" + search + "' OR name LIKE '%" + search + "%' OR keywords LIKE '%" + search + "%'";
                rs = st.executeQuery(sql);
                while (rs.next()) {
                    final String resultName = rs.getString("name");
                    buffer.add(resultName);
                }
            } catch (ClassNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }finally{
                btnEditar.setVisible(true);
                btnReiniciar.setVisible(true);
                final JList list = new JList(buffer.toArray());         
                final JScrollPane scrollPane = new JScrollPane(list);
                scrollPane.add(list);
                scrollPane.setViewportBorder(new LineBorder(new Color(0, 0, 0)));
                scrollPane.setBounds(67, 195, 269, -143);
                buscarPanel.add(scrollPane);                    
                list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
                list.setBounds(0, 0, 435, 240);

                scrollPane.setPreferredSize(new Dimension(70, 80));
                list.setLayoutOrientation(JList.VERTICAL);
                buscarPanel.invalidate();
                buscarPanel.validate();
                btnEditar.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent e) {
                        if(list.getSelectedValue() != null){
                            String selectedValue = (String) list.getSelectedValue();
                            tab.setSelectedIndex(2);
                            btnReiniciar.addActionListener(new ActionListener(){
                                public void actionPerformed(ActionEvent e) {
                                scrollPane.setVisible(false);
                                btnEditar.setVisible(false);
                                btnReiniciar.setVisible(false);
                            }
                            });
                }
                    }
                });
            }
        }
    });

这个按钮在我的数据库中搜索项目,但它在运行时不加载 JList。问题是什么?PS:没有运行时错误,没有 Stacktrace。

4

1 回答 1

3
  1. 不要重新创建整个 GUI,

  2. 使用XxxListModel而不是删除并添加JComponentsJPanel

  3. 对 XxxModel 的所有更新都必须在 EDT 上完成

  4. finally总是触发正确的块,但不是创建 GUI 的正确位置

  5. 必须调用revalidate()&repaint()如果你删除然后添加JComponent/添加到已经可见的container

于 2012-07-11T21:53:50.617 回答