JList没有显示在JScrollPane上 [英] JList not showing on JScrollPane
问题描述
我正在为一个家族企业制定一个计划,该计划将管理其原材料供应商提供的小册子.我发现您将需要所有代码,因此我将其发布在pastebin上. http://pastebin.com/Gc3aLe10
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);
}
});
}
}
});
}
}
});
此按钮在我的数据库中搜索项目,但是在运行JList时它不知何故不会加载.问题是什么? PS:没有运行时错误,没有Stacktrace.
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.
推荐答案
-
不要重新创建整个GUI,
don't re_create whole GUI,
使用 XxxListModel 而不是删除&将JComponents
添加到JPanel
use XxxListModel instead of remove & add JComponents
to the JPanel
右边的finally
块总是被触发,但是不是创建GUI的正确位置
right finally
block is always fired, but not right place for creating GUI
必须致电revalidate()
& repaint()
(如果您从已经存在的container
have to call revalidate()
& repaint()
if you remove and then add JComponent
from/to the already visible container
这篇关于JList没有显示在JScrollPane上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!