I'm trying to do a Swing thing where people can look up businesses
by any of the following:
Zone (collection of cities in a metro area)
City
General type of business (ie Auto related)
Specific type (Mufflers)
by selecting one or more of the above from combo boxes
The data that fills the combo boxes is stored in a mySQL database
along with individual businesses (addresses,phones, URLs,etc)
When the users select from one or more of the boxes, they click on a
button with a listener that generates the SQL (correctly, I am
getting the results I expect when I extract the statement) and puts
it into a JTable via a DefaultTableModel. When I call getValueAt()
on the table, I get the value which shows the table is loading, but I
never get the table to show in the scroll pane, as I need to do.
I have tried an internal class and a separate class with no success.
How do I get the table to show in the main frame after it gets loaded
by the listener?
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.lang.Object.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
import java.net.*;
/**
* Description of the Class
*
*@author JOHN
*@created April 27, 2003
*/
class businessMainGui
extends JFrame {
/**
* Constructor for the businessMainGui object
*/
String wTitle;
Vector cities;
Connection conn;
int aIndex = -1;
ActionListener cListen;
/**
* Constructor for the businessMainGui object
*
*@param Title Description of the Parameter
*/
businessMainGui(String Title) {
wTitle = Title;
}
/**
* Description of the Method
*/
void drawMain() {
try {
int wHeight = 0;
setDefaultCloseOperation
(WindowConstants.EXIT_ON_CLOSE);
setTitle(wTitle);
conn = doConnect();
Dimension sSize = getToolkit().getScreenSize
();
wHeight = (int) (sSize.height * .95);
Dimension logoSize = new Dimension
(sSize.width, (sSize.height / 10));
Dimension listSize = new Dimension
(sSize.width, 20);
Dimension labelSize = new Dimension
(sSize.width, (sSize.height / 20));
Dimension labelWidth = new Dimension
((sSize.width / 5),
labelSize.height);
Dimension go = new Dimension(sSize.width, 30);
System.out.println(labelSize.width + " w LS
h " + labelSize.height);
Dimension scrollSize = new Dimension
(sSize.width,
(7 * (sSize.height / 10)));
System.out.println(scrollSize.height + " " +
logoSize.height);
JPanel mainPanel = new JPanel(new GridLayout
(2, 1));
mainPanel.setPreferredSize(new Dimension
(sSize.width, (int) (sSize.height * .9)));
mainPanel.setBackground(new Color(255, 0,
255));
mainPanel.setLayout(new BoxLayout(mainPanel,
BoxLayout.Y_AXIS));
//JPanel logoPanel = new JPanel();
ImageIcon ii = new ImageIcon(new URL
("suhoff.ru/albums/general/09150010_G.thumb.jpg"));
//logoPanel.add(new JLabel(ii));
//logoPanel.setPreferredSize(new Dimension
(sSize.width, 113));
//logoPanel.setLayout(new BoxLayout
(logoPanel, BoxLayout.Y_AXIS));
//logoPanel.setBackground(new Color(255, 255,
255));
JPanel selectPanel = new JPanel();
selectPanel.setLayout(new BoxLayout
(selectPanel, BoxLayout.Y_AXIS));
JPanel labelPanel = new JPanel();
labelPanel.setLayout(new GridLayout(1, 5));
JLabel zone = new JLabel("Select by Zone");
zone.setPreferredSize(labelWidth);
labelPanel.add(zone);
JLabel city = new JLabel("Select by City");
city.setPreferredSize(labelWidth);
labelPanel.add(city);
JLabel business = new JLabel("Select by
business");
business.setPreferredSize(labelWidth);
labelPanel.add(business);
JPanel listPanel = new JPanel();
listPanel.setLayout(new BoxLayout(listPanel,
BoxLayout.X_AXIS));
JPanel top3 = new JPanel();
labelPanel.setPreferredSize(listSize);
labelPanel.setBackground(new Color(169, 209,
211));
listPanel.setPreferredSize(listSize);
listPanel.setBackground(new Color(0, 0, 200));
JComboBox cbArea = new JComboBox(getAreas
(conn));
cbArea.setPreferredSize(labelWidth);
aIndex = cbArea.getSelectedIndex();
System.out.println("index = " +
cbArea.getSelectedIndex());
JComboBox cbCities = new JComboBox(getCities
(conn));
cbCities.setPreferredSize(labelWidth);
JComboBox cbbusiness = new JComboBox
(getbusiness(conn));
cbbusiness.setPreferredSize(labelWidth);
JComboBox cbBusgrp = new JComboBox
(getbusinessGroup(conn));
cbBusgrp.setPreferredSize(labelWidth);
listPanel.add(cbArea);
listPanel.add(cbCities);
listPanel.add(cbBusgrp);
listPanel.add(cbbusiness);
selectPanel.add(labelPanel,
BorderLayout.NORTH);
selectPanel.add(listPanel,
BorderLayout.SOUTH);
JPanel goPanel = new JPanel();
goPanel.setBackground(new Color(25, 252, 25));
goPanel.setPreferredSize(go);
JButton bGo = new JButton("Get Your
Business");
bGo.setPreferredSize(new Dimension(160, 20));
goPanel.add(bGo);
Dimension dTop3 = new Dimension
(logoSize.width, 10);
top3.setBackground(new Color(130, 25, 25));
top3.setLayout(new BoxLayout(top3,
BoxLayout.Y_AXIS));
//top3.add(logoPanel);
top3.add(selectPanel);
top3.add(goPanel);
top3.setPreferredSize(dTop3);
mainPanel.add(top3);
JPanel ps;
ps = new JPanel();
ps.setBackground(new Color(255, 255, 255));
ps.setLayout(new BoxLayout(ps,
BoxLayout.Y_AXIS));
JScrollPane scrollThis = new JScrollPane(ps);
scrollThis.setPreferredSize(scrollSize);
scrollThis.setBackground(new Color(0, 255,
0));
bGo.addActionListener(new cbListener(
cbArea,
cbCities,
cbBusgrp,
cbbusiness,
scrollThis,
ps,
conn,
mainPanel
)
);
mainPanel.add(scrollThis);
getContentPane().add(mainPanel);
pack();
setVisible(true);
} catch (MalformedURLException mal) {
System.out.println
("http://www.cliffhouse.com/images/logonew1.gif");
}
}
/**
* Description of the Method
*
*@return Description of the Return Value
*/
Connection doConnect() {
Connection connection = null;
try {
Class.forName
("com.mysql.jdbc.Driver").newInstance();
System.out.println("no error");
//Connect to MySQL giving user id and password
String connectionURL
= "jdbc:mysql://localhost:3306/";
connection = DriverManager.getConnection
(connectionURL, "root", "");
return connection;
} catch (ClassNotFoundException e) {
// print trace of error
System.err.println(
"Couldn't find the the
MySQL " + "database driver: " +
e.getMessage());
return connection;
} catch (Exception e) {
e.printStackTrace();
return connection;
}
}
/**
* Gets the cities attribute of the businessMainGui object
*
*@param conn Description of the Parameter
*@return The cities value
*/
Vector getCities(Connection conn) {
Vector eCities = new Vector(1);
eCities.add(" ");
try {
Statement stmt = conn.createStatement();
String SQLStatement = "SELECT * FROM
eBusiness.ecities order by 2";
ResultSet rs = stmt.executeQuery
(SQLStatement);
// move to first row of result set
while (rs.next()) {
String loadThis = rs.getString
("City");
loadThis = loadThis.trim();
eCities.add(loadThis);
}
// tidy up and disconnect
rs.close();
stmt.close();
} catch (SQLException eSql) {
System.err.println(eSql.getMessage());
}
System.out.println("Vector eCities has " +
eCities.size() +
" cities");
return eCities;
}
/**
* Gets the business attribute of the businessMainGui object
*
*@param conn Description of the Parameter
*@return The business value
*/
Vector getbusiness(Connection conn) {
Vector ebusiness = new Vector(1);
ebusiness.add(" ");
try {
Statement stmt = conn.createStatement();
String SQLStatement = "SELECT bus_name FROM
eBusiness.ebusiness order by bus_name";
ResultSet rs = stmt.executeQuery
(SQLStatement);
// move to first row of result set
while (rs.next()) {
String loadThis = rs.getString
("bus_name");
loadThis = loadThis.trim();
ebusiness.add(loadThis);
}
// tidy up and disconnect
rs.close();
stmt.close();
} catch (SQLException eSql) {
System.err.println(eSql.getMessage());
}
System.out.println(
"Vector business has " +
ebusiness.size() + " business types");
return ebusiness;
}
/**
* Gets the areas attribute of the businessMainGui object
*
*@param conn Description of the Parameter
*@return The areas value
*/
Vector getAreas(Connection conn) {
Vector eAreas = new Vector(1);
eAreas.add(" ");
try {
Statement stmt = conn.createStatement();
String SQLStatement = "SELECT descr FROM
eBusiness.areas order by descr";
ResultSet rs = stmt.executeQuery
(SQLStatement);
// move to first row of result set
while (rs.next()) {
String loadThis = rs.getString
("descr");
loadThis = loadThis.trim();
eAreas.add(loadThis);
}
// tidy up and disconnect
rs.close();
stmt.close();
} catch (SQLException eSql) {
System.err.println(eSql.getMessage());
}
System.out.println("Vector areas has " + eAreas.size
() + " areas");
return eAreas;
}
/**
* Gets the businessGroup attribute of the businessMainGui
object
*
*@param conn Description of the Parameter
*@return The businessGroup value
*/
Vector getbusinessGroup(Connection conn) {
Vector ebusiness = new Vector(1);
ebusiness.add(" ");
try {
Statement stmt = conn.createStatement();
String SQLStatement = "SELECT descr FROM
eBusiness.Busgrp order by descr";
ResultSet rs = stmt.executeQuery
(SQLStatement);
// move to first row of result set
while (rs.next()) {
String loadThis = rs.getString
("descr");
loadThis = loadThis.trim();
ebusiness.add(loadThis);
}
// tidy up and disconnect
rs.close();
stmt.close();
} catch (SQLException eSql) {
System.err.println(eSql.getMessage());
}
System.out.println(
"Vector business has " +
ebusiness.size() + " business groups");
return ebusiness;
}
}
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.sql.*;
import javax.swing.table.*;
import java.lang.Object.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
/**
* Description of the Class
*
*@author JOHN
*@created May 1, 2003
*/
class cbListener
implements ActionListener {
/**
* Description of the Field
*/
public int aIndex;
/**
* Description of the Field
*/
public int cbIndex;
JComboBox cbArea;
JComboBox cbCity;
JComboBox cbType;
JComboBox cbBusiness;
JPanel inPanel;
JScrollPane iPane;
JPanel iPs;
JPanel mainPanel;
Connection conn;