A
abenm613
I'm trying to use dispose() method for closing a popup window. But the
problem is that, after the window is closed and the action to open a
similar window (with another data) is performed, the new windows that
comes up is identical to the one that I have just disposed. But I
don't want that. What I want is the new data to come up in a new
window. How do I achieve this? Is there any other method similar to
dispose() but more effective, that completely removes the content of
the previous window. Here is my code:
public class listByRequest extends JFrame implements ActionListener{
private ScrollingPanel fields;
private JTextField output, input, input1, input2, input3;
private Connection connection;
private String searchKey, searchStart, query, listQuery, whatIsInput,
social;
private JList resultList;
private JPanel listPanel, topPanel, mainPanel;
ResultSet resultSet;
JTable table[] = new JTable[100];
JFrame frame;
boolean flag[] = new boolean[20];
int c = 0, contCount = 0;
private JLabel lname, fname, gnumber, pcode;
private JButton close;
Container[] cont = new Container[100];
public listByRequest(Connection c, ScrollingPanel f)
{
connection = c;
fields = f;
}
public void actionPerformed(ActionEvent e)
{
lname = new JLabel();
fname = new JLabel();
gnumber = new JLabel();
pcode = new JLabel();
topPanel = new JPanel();
mainPanel = new JPanel();
//mainPanel.setLayout(new BorderLayout());
close = new JButton("Close window");
try{
Statement statement = connection.createStatement();
query = "SELECT ssec, lastname, firstname, streetadress, city,
state, zipcode, groupnumber FROM students WHERE";
System.out.println("I'm about to start constructing query");
if(!fields.lastName.getText().equals("")){
query = query+" lastname = '"+fields.lastName.getText()+"'";
flag[0] = true;
if((!fields.firstName.getText().equals("")) ||
(!fields.groupNumber.getText().equals("")) ||
(!fields.programCode.getText().equals("")))
query = query+" AND";
//System.out.println(query);
lname.setText("Last name = "+fields.lastName.getText());
topPanel.add(lname);
}
if(!fields.firstName.getText().equals("")){
flag[1]=true;
System.out.println("The first name appears as
"+fields.firstName.getText());
query = query+" firstname = '"+fields.firstName.getText()+"'";;
if((!fields.groupNumber.getText().equals("")) ||
(!fields.programCode.getText().equals("")))
query = query+" AND";
fname.setText("First name = "+fields.firstName.getText());
topPanel.add(fname);
}
if(!fields.groupNumber.getText().equals("")){
flag[3]=true;
System.out.println("The group number is
"+fields.groupNumber.getText());
query = query+" groupnumber =
'"+fields.groupNumber.getText()+"'";
if(!fields.programCode.getText().equals(""))
query = query+" AND";
gnumber.setText("Group number =
"+fields.groupNumber.getText());
topPanel.add(gnumber);
}
if(!fields.programCode.getText().equals("")){
flag[4]=true;
System.out.println("The program code is
"+fields.programCode.getText());
query = query+" programcode = '"+fields.programCode.getText()+"'";
pcode.setText("Program code = "+fields.programCode.getText());
topPanel.add(pcode);
}
System.out.println(query);
if(query == "SELECT * FROM students WHERE"){
JOptionPane.showMessageDialog(null, "Invalid query", "Invalid
query", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
System.out.println("\nSending query: "+query+"\n");
//table = new JTable(12, 12);
//Container cont = getContentPane();
//cont.add(table);
resultSet = statement.executeQuery(query);
System.out.println("\nResults found...");
showList(resultSet);
System.out.println("\nQuery successful");
statement.close();
setSize(1050, 1050);
System.out.println("Looks okay so far");
}
catch(SQLException sqlex){
System.out.println("Something wrong with SQL finding the record");
sqlex.printStackTrace();
System.out.println(sqlex.toString());
}
}
public void showList(ResultSet rs){
contCount++;
cont[contCount] = getContentPane();
listPanel = new JPanel();
Vector columnHeads = new Vector();
Vector rows = new Vector();
cont[contCount].setLayout(new BorderLayout());
frame = new JFrame();
try{
boolean moreRecords = rs.next();
if(!moreRecords){
JOptionPane.showMessageDialog(this, "ResultSet contained no
records");
setTitle("No records to display");
return;
}
boolean nextExists;
ResultSetMetaData rsmd = rs.getMetaData();
for(int i=1; i<=rsmd.getColumnCount(); ++i){
columnHeads.addElement(rsmd.getColumnName(i));
//columnHeads.elementAt(i).setWidth(5);
}
do{
rows.addElement(getNextRow(rs, rsmd));
nextExists = rs.next();
//if(nextExists)
// System.out.println("Next exists");
//else
// System.out.println("No next");
}while(nextExists);
table[contCount] = new JTable(rows, columnHeads);
ExcelAdapter ea = new ExcelAdapter(table[contCount]);
listPanel.add(table[contCount]);
mainPanel.add(topPanel);
mainPanel.add(listPanel);
mainPanel.add(close);
close.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
}
);
cont[contCount].add(mainPanel);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
cont[contCount].setSize(40, 40);
cont[contCount].validate();
show();
}
catch(SQLException sqlex){
System.out.println("SQL Exception");
sqlex.printStackTrace();
}
}
public Vector getNextRow(ResultSet rs, ResultSetMetaData rsmd) throws
SQLException{
Vector currentRow = new Vector();
for (int i=1; i<=rsmd.getColumnCount(); ++i){
switch(rsmd.getColumnType(i)){
case Types.VARCHAR:
case Types.LONGVARCHAR:
currentRow.addElement(rs.getString(i));
break;
case Types.INTEGER:
currentRow.addElement(new Long(rs.getLong(i)));
break;
default:
//System.out.println("Type was: "+rsmd.getColumnTypeName(i));
}
}
return currentRow;
}
}
---------------------------------
As I'm running a main program that activates this class, the popup
window is supposed to come up with the result of my query. It comes up
correctly only for the first time. Then the data "saves" somewhere and
keeps coming up even as I try to make different searches. Can anyone
help me get out of this problem?
problem is that, after the window is closed and the action to open a
similar window (with another data) is performed, the new windows that
comes up is identical to the one that I have just disposed. But I
don't want that. What I want is the new data to come up in a new
window. How do I achieve this? Is there any other method similar to
dispose() but more effective, that completely removes the content of
the previous window. Here is my code:
public class listByRequest extends JFrame implements ActionListener{
private ScrollingPanel fields;
private JTextField output, input, input1, input2, input3;
private Connection connection;
private String searchKey, searchStart, query, listQuery, whatIsInput,
social;
private JList resultList;
private JPanel listPanel, topPanel, mainPanel;
ResultSet resultSet;
JTable table[] = new JTable[100];
JFrame frame;
boolean flag[] = new boolean[20];
int c = 0, contCount = 0;
private JLabel lname, fname, gnumber, pcode;
private JButton close;
Container[] cont = new Container[100];
public listByRequest(Connection c, ScrollingPanel f)
{
connection = c;
fields = f;
}
public void actionPerformed(ActionEvent e)
{
lname = new JLabel();
fname = new JLabel();
gnumber = new JLabel();
pcode = new JLabel();
topPanel = new JPanel();
mainPanel = new JPanel();
//mainPanel.setLayout(new BorderLayout());
close = new JButton("Close window");
try{
Statement statement = connection.createStatement();
query = "SELECT ssec, lastname, firstname, streetadress, city,
state, zipcode, groupnumber FROM students WHERE";
System.out.println("I'm about to start constructing query");
if(!fields.lastName.getText().equals("")){
query = query+" lastname = '"+fields.lastName.getText()+"'";
flag[0] = true;
if((!fields.firstName.getText().equals("")) ||
(!fields.groupNumber.getText().equals("")) ||
(!fields.programCode.getText().equals("")))
query = query+" AND";
//System.out.println(query);
lname.setText("Last name = "+fields.lastName.getText());
topPanel.add(lname);
}
if(!fields.firstName.getText().equals("")){
flag[1]=true;
System.out.println("The first name appears as
"+fields.firstName.getText());
query = query+" firstname = '"+fields.firstName.getText()+"'";;
if((!fields.groupNumber.getText().equals("")) ||
(!fields.programCode.getText().equals("")))
query = query+" AND";
fname.setText("First name = "+fields.firstName.getText());
topPanel.add(fname);
}
if(!fields.groupNumber.getText().equals("")){
flag[3]=true;
System.out.println("The group number is
"+fields.groupNumber.getText());
query = query+" groupnumber =
'"+fields.groupNumber.getText()+"'";
if(!fields.programCode.getText().equals(""))
query = query+" AND";
gnumber.setText("Group number =
"+fields.groupNumber.getText());
topPanel.add(gnumber);
}
if(!fields.programCode.getText().equals("")){
flag[4]=true;
System.out.println("The program code is
"+fields.programCode.getText());
query = query+" programcode = '"+fields.programCode.getText()+"'";
pcode.setText("Program code = "+fields.programCode.getText());
topPanel.add(pcode);
}
System.out.println(query);
if(query == "SELECT * FROM students WHERE"){
JOptionPane.showMessageDialog(null, "Invalid query", "Invalid
query", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
System.out.println("\nSending query: "+query+"\n");
//table = new JTable(12, 12);
//Container cont = getContentPane();
//cont.add(table);
resultSet = statement.executeQuery(query);
System.out.println("\nResults found...");
showList(resultSet);
System.out.println("\nQuery successful");
statement.close();
setSize(1050, 1050);
System.out.println("Looks okay so far");
}
catch(SQLException sqlex){
System.out.println("Something wrong with SQL finding the record");
sqlex.printStackTrace();
System.out.println(sqlex.toString());
}
}
public void showList(ResultSet rs){
contCount++;
cont[contCount] = getContentPane();
listPanel = new JPanel();
Vector columnHeads = new Vector();
Vector rows = new Vector();
cont[contCount].setLayout(new BorderLayout());
frame = new JFrame();
try{
boolean moreRecords = rs.next();
if(!moreRecords){
JOptionPane.showMessageDialog(this, "ResultSet contained no
records");
setTitle("No records to display");
return;
}
boolean nextExists;
ResultSetMetaData rsmd = rs.getMetaData();
for(int i=1; i<=rsmd.getColumnCount(); ++i){
columnHeads.addElement(rsmd.getColumnName(i));
//columnHeads.elementAt(i).setWidth(5);
}
do{
rows.addElement(getNextRow(rs, rsmd));
nextExists = rs.next();
//if(nextExists)
// System.out.println("Next exists");
//else
// System.out.println("No next");
}while(nextExists);
table[contCount] = new JTable(rows, columnHeads);
ExcelAdapter ea = new ExcelAdapter(table[contCount]);
listPanel.add(table[contCount]);
mainPanel.add(topPanel);
mainPanel.add(listPanel);
mainPanel.add(close);
close.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
}
);
cont[contCount].add(mainPanel);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
cont[contCount].setSize(40, 40);
cont[contCount].validate();
show();
}
catch(SQLException sqlex){
System.out.println("SQL Exception");
sqlex.printStackTrace();
}
}
public Vector getNextRow(ResultSet rs, ResultSetMetaData rsmd) throws
SQLException{
Vector currentRow = new Vector();
for (int i=1; i<=rsmd.getColumnCount(); ++i){
switch(rsmd.getColumnType(i)){
case Types.VARCHAR:
case Types.LONGVARCHAR:
currentRow.addElement(rs.getString(i));
break;
case Types.INTEGER:
currentRow.addElement(new Long(rs.getLong(i)));
break;
default:
//System.out.println("Type was: "+rsmd.getColumnTypeName(i));
}
}
return currentRow;
}
}
---------------------------------
As I'm running a main program that activates this class, the popup
window is supposed to come up with the result of my query. It comes up
correctly only for the first time. Then the data "saves" somewhere and
keeps coming up even as I try to make different searches. Can anyone
help me get out of this problem?