again I am having problems with JList in displaying data from mysql database, I used the code given below but it is not displaying anything on screen..
JFrame f8 = new JFrame("Schedule");
f8.setVisible(true);
f8.setSize(1000, 1000);
JPanel jpa1 = new JPanel(new GridBagLayout());
String query = "SELECT * FROM Location";
DefaultListModel model = new DefaultListModel();
DefaultListModel model1 = new DefaultListModel();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Statement stmt = null;
ResultSet rs;
Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1/SPL", "root", "PWD");
stmt = (Statement) conn.createStatement();
rs = stmt.executeQuery(query);
while (rs.next()) {
String stadium = rs.getString("Stadium");
String city = rs.getString("City");
model.addElement(stadium);
model1.addElement(city);
}
JList list = new JList(model);
JList list1 = new JList(model1);
f8.add(jpa1);
jpa1.add(list);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setVisibleRowCount(1);
JScrollPane listScroller = new JScrollPane(list);
} catch (SQLException e) {
System.out.println("Message : " + e.getMessage());
}
can you tell me please where am I WRONG?
It's difficult to say what the problem is based on the snippet of code, not to mention a bit of unformatted code that is difficult to read, but...
You should consider:
Hovercraft Full of Eels almost hit the nail on the head, but I forgive him cause until I started taking whole sections of you code out I didn't see it.
The last line of your code reads
JScrollPane listScroller = new JScrollPane(list);
You never add this scroll pane to your UI. It has the effect of removing the list
from it's previous parent ( jpa1
) and adding to the scroll pane, which leaves nothing to be displayed on the screen.
I'd follow ALL of Hovercraft Full of Eels recommendations and then figure out how to fix this last problem...
jpa1.add(new JScrollPane(list));
Should just about do it
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.