Hello
The ActionPerformed will not unload the viewPort to load the new viewPort that holds a table.
is there a way to reload the table?
The first actionPerformed supplies the table but the next will not remove table and add the new one.
package view.content.panels.guide;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import model.dao.ConnectAdminDAO;
import model.utils.CachingResultSetTableModel;
import model.utils.ResultSetTableModel;
import model.utils.ScrollingResultSetTableModel;
import view.ViewUtils;
public class AdminPanel extends JPanel implements ActionListener {
//public JScrollPane displayScrollPane;
public ResultSetTableModel model;
//public JComboBox tableNamesCB;
public JButton nextButton;
public JButton previousButton;
public ResultSet rs;
//public ResultSet mrs;
public Connection conn;
public Statement stmt;
//public ResultSet mrs;
public static boolean SCROLLABLE = false;
///////////////////////////
public ConnectAdminDAO cad;
public String thisUser;
public boolean profile;
public boolean student;
public boolean instructor;
public boolean admin;
public boolean payee;
public String fName;
public String lName;
public String mName;
public String gender;
public Date startDate;
public Date endDate;
public int age;
public String address;
public String state;
public int zip;
public int areaCode;
public int phone;
public String payeeUid;
public String lessonLocation;
public Double payRate;
public int bookNum;
public String stuUid;
public int rate;
//public ResultSetMetaData rsmd;
//public ResultSet rs;
public String[] record;
public int recordCount;
public JTable table;
public String[] types;
//public JScrollPane displayScrollPane;
// set to true if your database supports scrolling cursors
public AdminPanel() throws FileNotFoundException, IOException, SQLException {
initComponents();
setThisUser(ViewUtils.getThisUser());
setProfile(ViewUtils.isAdmin());
setSize(800, 800);
/* find all tables in the database and add them to
a combo box
*/
//tableNamesCB = new JComboBox();
cad = new ConnectAdminDAO();
conn = (Connection) cad.allTables();
DatabaseMetaData meta = conn.getMetaData();
if (meta.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE)) {
SCROLLABLE = true;
System.out.println("in personalDataTable: Supports TYPE_SCROLL_INSENSITIVE");
} else {
SCROLLABLE = false;
System.out.println("in personalDataTable; No support for Supports TYPE_SCROLL_INSENSITIVE");
}
if (SCROLLABLE) {
String[] types = {"TABLE"};
stmt = (Statement) conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
DatabaseMetaData md = conn.getMetaData();
// mrs = md.getTables(null, null, null,new String[]{"TABLE"});
ResultSet mrs = md.getTables(null, null, null, types);
while (mrs.next()) {
tableNamesCB.addItem(mrs.getString(3));
//mrs.close();
}
} else {
stmt = (Statement) conn.createStatement();
DatabaseMetaData md = conn.getMetaData();
ResultSet mrs = md.getTables(null, null, null,
new String[]{"TABLE"});
while (mrs.next()) {
tableNamesCB.addItem(mrs.getString(3));
//mrs.close();
}
}
tableNamesCB.addActionListener(this);
System.out.println("to add combobox: ");
//add(tableNamesCB);
}
[b]
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == tableNamesCB) { // show the selected table from the combo box
if (displayScrollPane != null) {
remove(displayScrollPane);
}
try {
String tableName = (String) tableNamesCB.getSelectedItem();
if (rs != null) {
rs.close();
}
String query = "SELECT * FROM " + tableName;
rs = stmt.executeQuery(query);
if (SCROLLABLE) {
model = new ScrollingResultSetTableModel(rs);
} else {
model = new CachingResultSetTableModel(rs);
}
JTable table = new JTable(model);
displayScrollPane = new JScrollPane(table);
displayScrollPane.setViewportView(table);
displayPanel.add(displayScrollPane);
//add(displayScrollPane);
doLayout();
} catch (FileNotFoundException ex) {
Logger.getLogger(AdminPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(AdminPanel.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException e) {
System.out.println("Error " + e);
}
}
}
[/b]
public boolean isProfile() {
return profile;
}
public void setProfile(boolean profile) {
this.profile = profile;
}
public String getThisUser() {
return thisUser;
}
public void setThisUser(String thisUser) {
this.thisUser = thisUser;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getAreaCode() {
return areaCode;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
public int getZip() {
return zip;
}
public void setZip(int zip) {
this.zip = zip;
}
public void setAreaCode(int areaCode) {
this.areaCode = areaCode;
}
public int getBookNum() {
return bookNum;
}
public void setBookNum(int bookNum) {
this.bookNum = bookNum;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public boolean isInstructor() {
return instructor;
}
public void setInstructor(boolean instructor) {
this.instructor = instructor;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public String getlessonLocation() {
return lessonLocation;
}
public void setlessonLocation(String lessonLocation) {
this.lessonLocation = lessonLocation;
}
public String getmName() {
return mName;
}
public void setmName(String mName) {
this.mName = mName;
}
public Double getPayRate() {
return payRate;
}
public void setPayRate(Double payRate) {
this.payRate = payRate;
}
public boolean isPayee() {
return payee;
}
public void setPayee(boolean payee) {
this.payee = payee;
}
public String getPayeeUid() {
return payeeUid;
}
public void setPayeeUid(String payeeUid) {
this.payeeUid = payeeUid;
}
public int getPhone() {
return phone;
}
public void setPhone(int phone) {
this.phone = phone;
}
public int getRate() {
return rate;
}
public void setRate(int rate) {
this.rate = rate;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getStuUid() {
return stuUid;
}
public void setStuUid(String stuUid) {
this.stuUid = stuUid;
}
public boolean isStudent() {
return student;
}
public void setStudent(boolean student) {
this.student = student;
}
public Connection getConn() {
return conn;
}
public void setConn(Connection conn) {
this.conn = conn;
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
masterPanel = new javax.swing.JPanel();
headerPanel = new javax.swing.JPanel();
displayPanel = new javax.swing.JPanel();
displayScrollPane = new javax.swing.JScrollPane();
controlPanel = new javax.swing.JPanel();
cbPanel = new javax.swing.JPanel();
tableNamesCB = new javax.swing.JComboBox();
masterPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
headerPanel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
javax.swing.GroupLayout headerPanelLayout = new javax.swing.GroupLayout(headerPanel);
headerPanel.setLayout(headerPanelLayout);
headerPanelLayout.setHorizontalGroup(
headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 424, Short.MAX_VALUE)
);
headerPanelLayout.setVerticalGroup(
headerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 34, Short.MAX_VALUE)
);
masterPanel.add(headerPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, 430, 40));
displayPanel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
displayScrollPane.setBorder(javax.swing.BorderFactory.createEtchedBorder());
displayPanel.add(displayScrollPane);
masterPanel.add(displayPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, 430, 390));
controlPanel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
cbPanel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
cbPanel.add(tableNamesCB);
javax.swing.GroupLayout controlPanelLayout = new javax.swing.GroupLayout(controlPanel);
controlPanel.setLayout(controlPanelLayout);
controlPanelLayout.setHorizontalGroup(
controlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(controlPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(cbPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
.addContainerGap())
);
controlPanelLayout.setVerticalGroup(
controlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(controlPanelLayout.createSequentialGroup()
.addComponent(cbPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(474, Short.MAX_VALUE))
);
masterPanel.add(controlPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 10, 210, 531));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(masterPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(masterPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(22, Short.MAX_VALUE))
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JPanel cbPanel;
private javax.swing.JPanel controlPanel;
private javax.swing.JPanel displayPanel;
private javax.swing.JScrollPane displayScrollPane;
private javax.swing.JPanel headerPanel;
private javax.swing.JPanel masterPanel;
private javax.swing.JComboBox tableNamesCB;
// End of variables declaration
}