
I am working in netbeans and mysql.On selecting a check box, the data from database must be retrieved. i need the action preformance of check box...could nybody?....if could...!thanks

Hi Friend,
Try the following code:
import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class CheckBoxAction {
private static String des = "Deselected";
private static String sel = "Selected";
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JCheckBox checkBox = new JCheckBox(des);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
Vector columnNames = new Vector();
Vector data = new Vector();
AbstractButton abstractButton = (AbstractButton)actionEvent.getSource();
boolean selected = abstractButton.getModel().isSelected();
String newLabel = (selected ? sel : des);
abstractButton.setText(newLabel);
if(newLabel.equals("Selected")){
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st = con.createStatement();
ResultSet rs= st.executeQuery("Select * from employee");
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement( md.getColumnName(i) );
}
while (rs.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement( rs.getObject(i) );
}
data.addElement( row );
}
}
catch(Exception e){}
JTable table = new JTable(data, columnNames);
JScrollPane pane = new JScrollPane(table);
JFrame f=new JFrame();
f.add(pane);
f.setVisible(true);
f.pack();
}
}
};
checkBox.addActionListener(actionListener);
checkBox.setMnemonic(KeyEvent.VK_S);
Container contentPane = frame.getContentPane();
contentPane.add(checkBox, BorderLayout.NORTH);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.