hello.
i have a problem trying to get JCheckBox to work with JTextArea. what i expected to happen when i would click on a check box is that data from a table in a MS Access database would be displayed in the text area.
the program below compiles OK. when i run it a frame appears with check boxes, a text area and a button inside it. the text area is smaller than it should be. but when i press one of the check boxes, the following SQLExceptions appear in the jGRASP console. how can i get rid of these strange errors?
----jGRASP exec: java ViewProductDetails
java.sql.SQLException: Invalid handle
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(Unknown Source)
at ViewProductDetails$1.actionPerformed(ViewProductDetails.java:79)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.JToggleButton$ToggleButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
----jGRASP: process aborted by user.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class ViewProductDetails extends JFrame{
JPanel pnlBox, pnlBody, pnlFooter;
JCheckBox name;
JCheckBox ageCategory;
JCheckBox type;
JCheckBox year;
JButton returnToProductMenu;
JTextArea jta;
Container contentpane;
Connection db;
Statement statement;
public ViewProductDetails(){
super("View Product Details");
contentpane = getContentPane();
contentpane.setLayout(new BorderLayout());
pnlBox = new JPanel();
pnlBody = new JPanel();
pnlFooter = new JPanel();
jta = new JTextArea();
jta.setFont(new Font("Serif", Font.PLAIN, 12));
jta.setLineWrap(true);
jta.setWrapStyleWord(true);
jta.setEditable(false);
name = new JCheckBox("Name");
ageCategory = new JCheckBox("Age Category");
type = new JCheckBox("Type");
year = new JCheckBox("Year");
pnlBox.add(name);
pnlBox.add(ageCategory);
pnlBox.add(type);
pnlBox.add(year);
JScrollPane jsp = new JScrollPane(jta);
pnlBody.add(jsp, BorderLayout.CENTER);
returnToProductMenu = new JButton("Return To Product Menu");
pnlFooter.add(returnToProductMenu);
contentpane.add(pnlBox,BorderLayout.NORTH);
contentpane.add(pnlBody,BorderLayout.CENTER);
contentpane.add(pnlFooter,BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setVisible(true);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e){
System.out.println("Problem loading the driver");
}
String url = "jdbc:odbc:VideoLibrary";
try{
db = DriverManager.getConnection(url,"","");
statement = db.createStatement();
DatabaseMetaData dbmd = db.getMetaData();
ResultSet rs = dbmd.getTables(null, null, null, new String[]{"TABLE"});
}
catch(Exception e){
System.out.println("Could not initialise the database");
e.printStackTrace();
}
name.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
ResultSet rs1 = statement.executeQuery("SELECT * FROM Product ORDER BY name");
ResultSetMetaData rsmd1 = rs1.getMetaData();
for(int i = 1; i <= rsmd1.getColumnCount(); i++){
jta.append(rsmd1.getColumnName(i) + " ");
}
jta.append("\n");
while(rs1.next()){
for(int i = 1; i <= rsmd1.getColumnCount(); i++){
jta.append(rs1.getObject(i) + " ");
}
jta.append("\n");
}
}
catch(SQLException ea){
ea.printStackTrace();
}
}
});
ageCategory.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
ResultSet rs2 = statement.executeQuery("SELECT * FROM Product ORDER BY ageCategory");
ResultSetMetaData rsmd2 = rs2.getMetaData();
for(int i = 1; i <= rsmd2.getColumnCount(); i++){
jta.append(rsmd2.getColumnName(i) + " ");
}
jta.append("\n");
while(rs2.next()){
for(int i = 1; i <= rsmd2.getColumnCount(); i++){
jta.append(rs2.getObject(i) + " ");
}
jta.append("\n");
}
}
catch(SQLException eb){
eb.printStackTrace();
}
}
});
type.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
ResultSet rs3 = statement.executeQuery("SELECT * FROM Product ORDER BY type");
ResultSetMetaData rsmd3 = rs3.getMetaData();
for(int i = 1; i <= rsmd3.getColumnCount(); i++){
jta.append(rsmd3.getColumnName(i) + " ");
}
jta.append("\n");
while(rs3.next()){
for(int i = 1; i <= rsmd3.getColumnCount(); i++){
jta.append(rs3.getObject(i) + " ");
}
jta.append("\n");
}
}
catch(SQLException ec){
ec.printStackTrace();
}
}
});
year.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
ResultSet rs4 = statement.executeQuery("SELECT * FROM Product ORDER BY year");
ResultSetMetaData rsmd4 = rs4.getMetaData();
for(int i = 1; i <= rsmd4.getColumnCount(); i++){
jta.append(rsmd4.getColumnName(i) + " ");
}
jta.append("\n");
while(rs4.next()){
for(int i = 1; i <= rsmd4.getColumnCount(); i++){
jta.append(rs4.getObject(i) + " ");
}
jta.append("\n");
}
}
catch(SQLException ed){
ed.printStackTrace();
}
}
});
returnToProductMenu.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
setVisible(false);
}
});
try{
if(statement != null){
statement.close();
}
if(db != null){
db.close();
}
}
catch(Exception e){
System.out.println("Could not close the current connection");
e.printStackTrace();
}
}
public static void main(String[] args){
new ViewProductDetails();
}
}