JList not showing on JScrollPane

北战南征 提交于 2019-12-23 22:29:50

问题


I am making a program for a family enterprise that will manage the brochures from their raw materials providers. I find you will need all the code, so I posted it on pastebin. http://pastebin.com/Gc3aLe10

But also, here I attach where I think the problem is:

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);
                            }
                            });
                }
                    }
                });
            }
        }
    });

This button searches for items in my database, but it somehow doesn't load the JList when it runs. What is the problem? PS:No runtime errors, no Stacktrace.


回答1:


  1. don't re_create whole GUI,

  2. use XxxListModel instead of remove & add JComponents to the JPanel

  3. all updates to the XxxModel must be done on EDT

  4. right finally block is always fired, but not right place for creating GUI

  5. have to call revalidate() & repaint() if you remove and then add JComponent from/to the already visible container



来源:https://stackoverflow.com/questions/11441963/jlist-not-showing-on-jscrollpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!