问题
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:
don't re_create whole GUI,
use XxxListModel instead of remove & add
JComponents
to theJPanel
all updates to the XxxModel must be done on EDT
right
finally
block is always fired, but not right place for creating GUIhave to call
revalidate()
&repaint()
if you remove and then addJComponent
from/to the already visiblecontainer
来源:https://stackoverflow.com/questions/11441963/jlist-not-showing-on-jscrollpane