Modifying code so that JCheckBox can be used instead of JComboBox

hello.

how can I modify the following code so that JCheckBox can be used instead of JComboBox? i want users of my video library system to be able to view CD/DVD/Game information by name, age category, type and year. Users should be able to click on a check box (e.g. view by name, age category, type or year) and press a button. What would happen then is that data from the Product table would appear in the text area.

Thanks.

import javax.swing.*;

import java.sql.*;

import java.awt.*;

import java.awt.event.*;

publicclass Exercise32_6extends JApplet{

private JComboBox jcboTableName =new JComboBox();

private JTextArea jtaResult =new JTextArea();

private JButton jbtShowContents =new JButton("Show Contents");

private Statement stmt;

publicvoid init(){

initializeDB();

jbtShowContents.addActionListener(new java.awt.event.ActionListener(){

publicvoid actionPerformed(ActionEvent e){

jbtShowContents_actionPerformed(e);

}

});

JPanel jPanel1 =new JPanel();

jPanel1.add(new JLabel("Table Name"));

jPanel1.add(jcboTableName);

jPanel1.add(jbtShowContents);

this.getContentPane().add(jPanel1, BorderLayout.NORTH);

this.getContentPane().add(new JScrollPane(jtaResult), BorderLayout.CENTER);

}

privatevoid initializeDB(){

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

System.out.println("Driver loaded");

Connection connection = DriverManager.getConnection("jdbc:odbc:VideoLibrary");

System.out.println("Database connected");

stmt = connection.createStatement();

DatabaseMetaData dbMetaData = connection.getMetaData();

ResultSet rsTables = dbMetaData.getTables(null, null, null,new String[]{"TABLE"});

System.out.print("User tables: ");

while (rsTables.next()){

jcboTableName.addItem(rsTables.getString("TABLE_NAME"));

}

}

catch (Exception ex){

ex.printStackTrace();

}

}

privatevoid jbtShowContents_actionPerformed(ActionEvent e){

String tableName = (String)jcboTableName.getSelectedItem();

try{

String queryString ="select * from " + tableName;

ResultSet resultSet = stmt.executeQuery(queryString);

ResultSetMetaData rsMetaData = resultSet.getMetaData();

for (int i = 1; i <= rsMetaData.getColumnCount(); i++){

jtaResult.append(rsMetaData.getColumnName(i) +"");

}

jtaResult.append("\n");

while (resultSet.next()){

for (int i = 1; i <= rsMetaData.getColumnCount(); i++){

jtaResult.append(resultSet.getObject(i) +"");

}

jtaResult.append("\n");

}

}

catch (SQLException ex){

ex.printStackTrace();

}

}

publicstaticvoid main(String[] args){

Exercise32_6 applet =new Exercise32_6();

JFrame frame =new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setTitle("Exercise32_6");

frame.getContentPane().add(applet, BorderLayout.CENTER);

applet.init();

applet.start();

frame.setSize(380, 180);

frame.setLocationRelativeTo(null);

frame.setVisible(true);

}

}

[5949 byte] By [james-mcfaddena] at [2007-11-27 11:33:50]
# 1

Whats your problem? A check box supports an ActionListener the same as a combo box.

Read the JCheckBox API and click on the "How to Use Buttons" link to see how you can get the information on which check box was clicked. Hint, you would use the action command.

camickra at 2007-7-29 16:53:44 > top of Java-index,Desktop,Core GUI APIs...