admin

JList没有显示在JScrollPane上

sql

我正在为一个家族企业制作一个程序,该程序将管理其原材料供应商提供的小册子。我发现您将需要所有代码,因此我将其发布在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。


阅读 134

收藏
2021-07-01

共1个答案

admin

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

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

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

  4. finally始终会触发正确的块,但创建GUI的正确位置却不正确

  5. 必须致电revalidate()&(repaint()如果您删除了它,然后JComponent从/添加到已经可见的container

2021-07-01