i have been trying to retrive data from mysql in the list
and that is working fine but the problem is i'm not able to select the name
of the list
.. i want to select name
(not index or value) so that i can complete my where clause
Here is my Code :-
private void proceed(){
PreparedStatement stmt = null;
Connection conn = null;
ResultSet rs=null;
String i = jList1.getSelectedValue();
try {
Class.forName("java.sql.DriverManager");
conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/hotel","root","root");
stmt = conn.prepareStatement("select * from hotelbookings where GuestName = '"+i+"'");
rs = stmt.executeQuery();
if (rs.next()){
String BN = rs.getString("BookNo");
String GN = rs.getString("GuestName");
String AD = rs.getString("Address");
String NOD = rs.getString("No_of_Days");
String PN = rs.getString("PhoneNo");
String ID = rs.getString("ID_Proof");
String CN = rs.getString("Country");
String ARD = rs.getString("Arival_Date");
String DRD = rs.getString("Departure_Date");
NewJFrame1_1 second = new NewJFrame1_1(BN,GN,AD,NOD,PN,ID,CN,ARD,DRD);
second.setVisible(true);
}
} catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
Here is The code for Show List Button:-
private void fillList(){
PreparedStatement stmt = null;
Connection conn = null;
try {
Class.forName("java.sql.DriverManager");
conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/hotel","root","root");
stmt = conn.prepareStatement("select GuestName from hotelbookings");
stmt.executeQuery();
ResultSet rs = stmt.getResultSet();
int i =0;
DefaultListModel info = new DefaultListModel();
while (rs.next()){
String[] data = new String[100];
data[i] = rs.getString("GuestName");
jList1.setModel(info);
info.addElement(data[i]);
i = i + 1;
jList1 = new JList(info);
}
}
catch(Exception e){
JOptionPane.showMessageDialog (this, e.getMessage());
}
}
fillList class:-
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
fillList();
}
private void fillList(){
PreparedStatement stmt = null;
Connection conn = null;
try {
Class.forName("java.sql.DriverManager");
conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/hotel","root","root");
stmt = conn.prepareStatement("select GuestName from hotelbookings");
stmt.executeQuery();
ResultSet rs = stmt.getResultSet();
int i =0;
DefaultListModel info = new DefaultListModel();
while (rs.next()){
String[] data = new String[100];
data[i] = rs.getString("GuestName");
info.addElement(data[i]);
i = i + 1;
}
jList1 = new JList(info);
} catch(Exception e){
JOptionPane.showMessageDialog (this, e.getMessage());
}
}
Well, the basic problem with the fillList
as far as I can tell from your posted code is the while loop. You create a new JList
for every loop. This will make invalid (or should I say dirty ) the jList1
reference you have in the proceed
method. You should not create a new JList
every time. Just keep the same list for every loop. Do not even change it outside the loop. Just keep it the same and update its model every time.
To update the model, you have at least the following two options:
jList1.setModel(your_created_model)
.JList
to DefaultListModel
once (on construction time for example), and then in each fillList
method call:
getModel()
). You know it's a DefaultListModel
so you can safely cast it to one.removeAllElements()
).ResultSet
to the model.Take for example the following code:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
public class Main {
public static void fillList(final JList<String> list) {
final DefaultListModel<String> model = (DefaultListModel<String>) list.getModel(); //Step 1: Get the model and cast it to DefaultListModel.
model.removeAllElements(); //Step 2: Remove all elements from the model.
final double rand = Math.random();
for (int i = 0; i < 10; ++i)
model.addElement("name " + Math.round((1 + i) * rand * 100)); //Step 3: Fill the model with new elements.
}
public static void proceed(final JList<String> list) {
JOptionPane.showMessageDialog(list, "You selected: \"" + list.getSelectedValue() + "\"!");
//Here you have the selected value approprietly to do whatever you like with...
}
public static void main(final String[] args) {
final JList<String> names = new JList<>(new DefaultListModel<>()); //Do not forget to set the ListModel of the list to DefaultListModel!
//List initialization:
names.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
fillList(names);
//Creating the buttons:
final JButton fill = new JButton("Fill"),
proceed = new JButton("Proceed");
//Adding listeners:
fill.addActionListener(e -> fillList(names));
proceed.addActionListener(e -> proceed(names));
//Creating buttons' panel:
final JPanel buttons = new JPanel();
buttons.add(fill);
buttons.add(proceed);
//Scroll pane of list:
final JScrollPane scroll = new JScrollPane(names);
scroll.setPreferredSize(new Dimension(400, 200));
//Main panel:
final JPanel contents = new JPanel(new BorderLayout());
contents.add(scroll, BorderLayout.CENTER);
contents.add(buttons, BorderLayout.PAGE_END);
//Frame:
final JFrame frame = new JFrame("List of MesureTypes.");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(contents);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
It utilizes the second option (refilling the model each time).
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.