• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I get the JTable to show @#!$%  RSS feed

 
Tod Sterben
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ADVERTISEMENT


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
*
*@a... JOHN
*@c... 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
*
*@p... 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
("http://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
*
*@r... 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
*
*@p... conn Description of the Parameter
*@r... 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
*
*@p... conn Description of the Parameter
*@r... 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
*
*@p... conn Description of the Parameter
*@r... 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
*
*@p... conn Description of the Parameter
*@r... 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
*
*@a... JOHN
*@c... 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;

/**
* Constructor for the cbListener object
*
*@p... pScroll Description of the Parameter
*@p... cbArea Description of the Parameter
*@p... cbCity Description of the Parameter
*@p... cbType Description of the Parameter
*@p... cbBusiness Description of the Parameter
*@p... pPs Description of the Parameter
*@p... conn Description of the Parameter
*@p... mainPanel Description of the Parameter
*/
cbListener(JComboBox cbArea, JComboBox cbCity, JComboBox
cbType,
JComboBox cbBusiness, JScrollPane pScroll,
JPanel pPs,
Connection conn, JPanel mainPanel) {
this.cbArea = cbArea;
this.cbCity = cbCity;
this.cbType = cbType;
this.cbBusiness = cbBusiness;
this.conn = conn;
iPane = pScroll;
iPs = pPs;
this.mainPanel = mainPanel;
System.out.println("here is the constructor");
}

/**
* Description of the Method
*
*@p... e Description of the Parameter
*/
public void actionPerformed(ActionEvent e) {
aIndex = isSelected();
System.out.println("Action " + aIndex);
DefaultTableModel theBusiness = getBusinessaurants
(getSQLString());
JTable table = new JTable(theBusiness);
iPs.add(table);
System.out.println("Table row count " +
table.getRowCount());
iPane.add(iPs);
mainPanel.add(iPane);
mainPanel.repaint();
}

/**
* Description of the Method
*
*@r... The sQLString value
*/
public String getSQLString() {
String sqlSelect = "Select
eBusiness.name,eCities.city,eBusiness.bus_name ";
String sqlFrom = " from eBusiness.eBusiness,
eBusiness.eBusiness,eBusiness.eCities ";
String sqlWhere = " where eBusiness.idCity =
eCities.id and ";
String finalString = "";
sqlWhere = sqlWhere + " eBusiness.cuis_type =
eBusiness.id ";
String addString = ((String) cbArea.getSelectedItem()
+ (String) cbCity.getSelectedItem()
+ (String) cbType.getSelectedItem()
+ (String) cbBusiness.getSelectedItem
());
addString.trim();
if (addString.length() > 0) {
finalString = sqlAdded(sqlFrom, sqlWhere);
}
return (sqlSelect + finalString);
}

/**
* Description of the Method
*
*@p... sqlFrom Description of the Parameter
*@p... sqlWhere Description of the Parameter
*@r... Description of the Return Value
*/
public String sqlAdded(String sqlFrom, String sqlWhere) {
String addString = ((String) cbArea.getSelectedItem
());
addString = addString.trim();
if (addString.length() > 0) {
sqlFrom = sqlFrom + ", eBusiness.areas ";
sqlWhere = sqlWhere + " and areas.descr = '"
+ addString +
"' and areas.idArea =
eCities.idArea ";
}
addString = ((String) cbCity.getSelectedItem());
addString = addString.trim();
if (addString.length() > 0) {
sqlWhere = sqlWhere + " and eCities.city = '"
+ addString +
"' and eCities.id =
eBusiness.idCity ";
}
addString = ((String) cbType.getSelectedItem());
addString = addString.trim();
if (addString.length() > 0) {
sqlFrom = sqlFrom + ", eBusiness.cuisgrp g ";
sqlWhere = sqlWhere + " and g.descr = '" +
addString +
"' and g.idCuisGrp =
eBusiness.idCuisGrp ";
}
addString = ((String) cbBusiness.getSelectedItem());
addString = addString.trim();
if (addString.length() > 0) {
sqlWhere = sqlWhere + " and
eBusiness.bus_name = '" + addString +
"' and eBusiness.id =
eBusiness.cuis_type ";
}
System.out.println(sqlFrom + " " + sqlWhere);
return sqlFrom + " " + sqlWhere;
}

/**
* Gets the selected attribute of the cbListener object
*
*@r... The selected value
*/
public int isSelected() {
System.out.println("index" + cbIndex);
iPs = new JPanel();
iPs.setLayout(new BoxLayout(iPs, BoxLayout.Y_AXIS));
for (int x = 0; x < 100; x++) {
iPs.add(new JLabel("Here are the businesses"
+ x));
}
iPs.add(new JLabel((String) cbArea.getSelectedItem
()));
System.out.println(
cbArea.getSelectedItem() + " " +
iPane.getComponentCount());
iPane.add((JPanel) iPs);
return 0;
}
/**
* Gets the businesses attribute of the cbListener object
*
*@p... sqlStatement Description of the Parameter
*@r... The businesses value
*/
DefaultTableModel getBusinesses(String sqlStatement) {
ResultSet theBusinesses;
DefaultTableModel theBusTable;
Vector newRow;
try {
Statement stmt = conn.createStatement();
theBusinesses = stmt.executeQuery
(sqlStatement);
theBusTable = new DefaultTableModel
(theBusinesses.getFetchSize(), 3);
while (theBusinesses.next()) {
newRow = new Vector();
newRow.add(theBusinesses.getString
(1));
newRow.add(theBusinesses.getString
(2));
newRow.add(theBusinesses.getString
(3));
theBusTable.addRow(newRow);
}
System.out.println("B" +
theBusinesses.getFetchSize());
System.out.println("Table theBusinessTable
has " + theBusinessTable.getRowCount() + " Businesses");
stmt.close();
return theBusinessTable;
} catch (SQLException eSql) {
System.err.println(eSql.getMessage());
}
return null;
}
}

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!