Bob Bender

Greenhorn
+ Follow
since Feb 26, 2004
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Bob Bender

Bedtime for Bonzo!
18 years ago
Would this be like showing an empty scrollpane? Or, how would I show an empty one?
Here is my source.

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;
public class REB_mdbBrowser {
// Listener class needs declared here, as static fields
private static JavaFilter javaFilter = new JavaFilter();
private static JFrame f;
private static JMenu j1, j2, j3;
private static JMenuBar jb;
private static JMenuItem jmi, j1mi1, j3mi1, j3mi2;
private static JScrollPane scrollPane;
private static JTable table ;
private static JTextArea jta;
private static ResultSetTableModel model;
private static Connection conn;
private static Statement stmt;
private static DatabaseMetaData meta;
private static ResultSet rs;
private static String query = "";
private static int debugCtr = 0;
public static void main(String[ ] args) throws Exception {
String title="My Access .mdb Browser";
f = new JFrame(title);
f.setSize(300,500);
f.getContentPane().setLayout(new GridLayout(2,1));
// set up the menu bar.
jb = new JMenuBar();
j1 = new JMenu("Database");
j2 = new JMenu("Tables");
j3 = new JMenu("Actions");
// 1st menu has 1 items: Open
j1mi1 = new JMenuItem("Open");
j1.add(j1mi1);
// 2nd menu must be populated with the names of the tables
getTableNames(openFile());
// 3rd menu has 2 items: Execute and Clear
j3mi1 = new JMenuItem("Execute");
j3.add(j3mi1);
j3mi2 = new JMenuItem("Clear");
j3.add(j3mi2);
jb.add(j1);
jb.add(j2);
jb.add(j3);
f.setJMenuBar(jb);
// top half of the JFrame will contain a TextArea
// bottom half is initially empty,
jta = new JTextArea();
f.getContentPane().add(jta);
f.show();
// set up event handling
ActionListener a = new MyActionListener();
j1mi1.addActionListener(a);
j3mi1.addActionListener(a);
j3mi2.addActionListener(a);
}
// inner class -- it has access to all fields of the outer class
// must be declared static because it's referenced in main
static class MyActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// Figure out menu item was selected by the user
String sel = e.getActionCommand();
// need to execute FileChooser
if (sel.equals("Open"))
{
getTableNames(openFile());
}
// need to execute query typed in JTextArea
else if (sel.equals("Execute"))
{
query = jta.getText();
try
{
ResultSet rs2 = stmt.executeQuery(query);
model = new MyResultSetTableModel(rs2);
table = new JTable(model);
if (scrollPane != null)
f.getContentPane().remove(scrollPane);
scrollPane = new JScrollPane(table);
f.getContentPane().add(scrollPane);
f.show();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
else if (sel.equals("Clear"))
{
jta.setText("");
if (scrollPane != null)
{
f.getContentPane().remove(scrollPane);
scrollPane = new JScrollPane(table);
f.getContentPane().add(scrollPane);
}
f.show();
}
else
{
//else it's a table name
try
{
query = "Select * from " + sel;
ResultSet rs3 = stmt.executeQuery(query);
ResultSetMetaData rm = rs3.getMetaData();
model = new MyResultSetTableModel(rs3);
table = new JTable(model);
// Add the table to a scrolling pane
if (scrollPane != null)
f.getContentPane().remove(scrollPane);
scrollPane = new JScrollPane(table);
f.getContentPane().add(scrollPane);
f.show();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
}
}

/**
Prompts the user for a database and gets the database metatable info
*/
public static void getTableNames(String file)
{
String table_name = "";
try
{
conn = getConnection(file);
stmt = conn.createStatement();
meta = conn.getMetaData();
// rs is a "metatable" that contains information about each table in the db
rs = meta.getTables(null, null, null, null);
//clear the menu out and populate
j2.removeAll();
while (rs.next())
{
if (rs.getString("TABLE_TYPE").equals("TABLE"))
{
jmi = new JMenuItem(rs.getString("TABLE_NAME"));
j2.add(jmi);
ActionListener a = new MyActionListener();
jmi.addActionListener(a);
}
}
}
catch(SQLException e)
{
e.printStackTrace();
}
}

/**
Use a JFileChooser in Open mode to select files
to open. Use a filter for FileFilter subclass
to select for *.mdb files.
*/
public static String openFile()
{
JFileChooser fc = new JFileChooser();
fc.setDialogTitle("Open File");
// Choose only files, not directories
fc.setFileSelectionMode( JFileChooser.FILES_ONLY);
// Start in current directory
fc.setCurrentDirectory(new File("."));
// Set filter for Java source files.
fc.setFileFilter(javaFilter);
// Now open chooser
int result = fc.showOpenDialog(jta);
if (result == JFileChooser.APPROVE_OPTION)
{
return (fc.getSelectedFile().toString());
}
else
{
return "";
}
}

/**
Gets a connection from the filename specified
@return the database connection
*/
public static Connection getConnection(String dbName) throws SQLException
{
try
{
Class.forName ("com.inzoom.jdbcado.Driver");
}
catch (ClassNotFoundException e)
{
System.out.println(e);
}
String url = "jdbc:izmado rovider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbName;
return DriverManager.getConnection(url);
}
/*
//ALTERNATE CONNECTION CODE NOT USING THE IZMADO DRIVER
//Gets a connection from the properties specified in the file database.properties
//@return the database connection
public static Connection getConnection() throws SQLException, IOException
{
Properties props = new Properties();
String fileName = "airlines.properties";
FileInputStream in = new FileInputStream(fileName);
props.load(in);
String drivers = props.getProperty("jdbc.drivers");
if (drivers != null)
System.setProperty("jdbc.drivers", drivers);
String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");
return DriverManager.getConnection(url, username, password);
}
PROPERTIES FILE DEFINITIONChange "JavaClass" in line 2 to match your ODBC definition
jdbc.drivers=sun.jdbc.odbc.JdbcOdbcDriver
jdbc.url=jdbc dbc:JavaClass
jdbc.username=PUBLIC
jdbc.password=PUBLIC
*/
/*
Debugging routines for System.out.println
*/
public static void debugPrint(int opt, String desc)
{
if (opt == 1)
System.out.println(desc + "..." + debugCtr);
}
}

abstract class ResultSetTableModel extends AbstractTableModel
{
/**
Constructs the table model.
@param aResultSet the result set to display.
*/
public ResultSetTableModel(ResultSet aResultSet)
{
rs = aResultSet;
try
{
rsmd = rs.getMetaData();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public String getColumnName(int c)
{
try
{
return rsmd.getColumnName(c + 1);
}
catch(SQLException e)
{
e.printStackTrace();
return "";
}
}
public int getColumnCount()
{
try
{
return rsmd.getColumnCount();
}
catch(SQLException e)
{
e.printStackTrace();
return 0;
}
}
/**
Gets the result set that this model exposes.
@return the result set
*/
protected ResultSet getResultSet()
{
return rs;
}
private ResultSet rs;
private ResultSetMetaData rsmd;
}

class MyResultSetTableModel extends ResultSetTableModel
{
public MyResultSetTableModel(ResultSet aResultSet)
{
super(aResultSet);
try
{
cache = new ArrayList();
int cols = getColumnCount();
ResultSet rs = getResultSet();
/**
Place all data in an array list of Object[] arrays
We don't use an Object[][] because we don't know
how many rows are in the result set
*/
while (rs.next())
{
Object[] row = new Object[cols];
for (int j = 0; j < row.length; j++)
row[j] = rs.getObject(j + 1);
cache.add(row);
}
}
catch(SQLException e)
{
System.out.println("Error " + e);
}
}
public Object getValueAt(int r, int c)
{
if (r < cache.size())
return ((Object[])cache.get(r))[c];
else
return null;
}
public int getRowCount()
{
return cache.size();
}
private ArrayList cache;
}
/*
*/
class JavaFilter extends javax.swing.filechooser.FileFilter
{
public boolean accept(File f)
{
return f.getName().toLowerCase().endsWith(".mdb") || f.isDirectory();
}
public String getDescription()
{
return "Access files (*.mdb)";
}
}
20 years ago
Gosh, no luck so far..... What fun this language is1
20 years ago
Okay, I'll bite. How would I do that "call Container.validate"?
20 years ago
I have a JFrame that is split in 2 pieces (top and bottom, and should
never have more than 2 panes)...

JFrame f = new JFrame(title);
f.setSize(300,500);
f.getContentPane().setLayout(new GridLayout(2,1));

Top part is a text area defined as...
JtextArea jta = new JTextArea();
f.getContentPane().add(jta);
f.show();

Bottom is filled by adding a ScrollPane from a database after removing
the existing one....
ResultSet rs2 = stmt.executeQuery(query);
MyResultSetTableModel model = new MyResultSetTableModel(rs2);
JTable table = new JTable(model);
if (scrollPane != null)
f.getContentPane().remove(scrollPane);
JScrollPane scrollPane = new JScrollPane(table);
f.getContentPane().add(scrollPane);
f.show();
NOTE: I can click on this table and select cells/rows,etc

Next, I try clearing out the 2 areas using ...
jta.setText("");
if (scrollPane != null)
f.getContentPane().remove(scrollPane);
f.show();
Results.....
The text area clears off, but the scrollpane is still viewable. But,
I cannot click on the scrollpane with the mouse, so it must be gone
from memory, but not off the screen physically.

What am I doing wrong that I can still see the pane, but it is not there??
Bob
20 years ago