Reputation: 43
I was developing a Java Swing
application to select data
from the database
.But my Action Listener
doesn't work. Please help me. No errors are showing while executing the program.Please check my code.
public class Availability implements ActionListener {
JPanel panelForMedicine,inputPanel;
JLabel mediLabel;
JComboBox listCombo;
JButton dbButton;
final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
final String DB_URL = "jdbc:mysql://localhost/paliative_care_unit";
final String USER = "root";
final String PASS = "root";
Connection conn = null;
Statement stmt=null;
static void guiBuilder(){
JFrame availabilityFrame=new JFrame("Check Stock");
availabilityFrame.setLocation(0, 0);
availabilityFrame.setSize(390,150);
availabilityFrame.setVisible(true);
availabilityFrame.setDefaultCloseOperation(availabilityFrame.DISPOSE_ON_CLOSE);
Availability availability=new Availability();
availabilityFrame.setContentPane(availability.mainPanel());
availability.mainPanel();
}
public JPanel mainPanel(){
JPanel availabilityPanel=new JPanel();
availabilityPanel.setLayout(null);
panelForMedicine=new JPanel();
//panelForMedicine.setLayout(null);
panelForMedicine.setSize(70, 40);
panelForMedicine.setLocation(30, 35);
availabilityPanel.add(panelForMedicine);
mediLabel=new JLabel("Medicine");
mediLabel.setLocation(0, 0);
mediLabel.setSize(70, 40);
panelForMedicine.add(mediLabel);
inputPanel=new JPanel();
inputPanel.setLayout(null);
inputPanel.setSize(150, 40);
inputPanel.setLocation(120,35);
availabilityPanel.add(inputPanel);
listCombo=new JComboBox();
listCombo.setSize(150, 30);
listCombo.setLocation(0, 0);
inputPanel.add(listCombo);
try{
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql="SELECT DISTINCT medi FROM paliative";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
listCombo.addItem(rs.getString("medi"));
//medicineField.setSelectedItem("attempt11");
}
}
catch(Exception e){System.out.println(e);}
dbButton=new JButton("GO");
dbButton.setSize(70, 30);
dbButton.setLocation(300, 35);
dbButton.addActionListener(this);
availabilityPanel.add(dbButton);
availabilityPanel.setOpaque(true);
return availabilityPanel;
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==dbButton){
System.out.println("Checking");
try{
System.out.println("Checking");
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql="SELECT * FROM paliative WHERE medi='"+listCombo.getSelectedItem().toString()+"'";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
}
}
catch (Exception ex) {
JFrame dialogFrame=new JFrame();
JOptionPane.showMessageDialog(dialogFrame, ex);
}
}
}
}
Upvotes: 0
Views: 123
Reputation: 39495
as @nicE cOw points out - you are calling availability.mainPanel()
twice in your guiBuilder()
method. This will change your reference to the dbButton
variable after it was added to your contentPane, which will make the if(e.getSource()==dbButton)
condition fail in your action listener
Solution: remove the second call to availability.mainPanel();
in guiBuilder()
Also: Please read up on Swing threading. You should not be doing long running actions (like database calls) in the Event Dispatch Thread. Try Concurrency In Swing for starters.
Upvotes: 1