i have this application that has a tab (box tab,voice tab , etc..), buttons , Jtable.. i also have an add button , whenever i click on the add button to add a user(this users info will be added to the JTable) but also a new tab(box) will be added , i don't want a new tab to be added to happen ,, i don't know how to solve this error.. import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; import java.sql.*; import java.io.*; import javax.swing.JPanel; import java.applet.*; import sun.audio.*; import java.net.*; import java.text.*;
//============================================================================= ====================== //Create the GUI:
void makeGUI() { c.setLayout(new BorderLayout()); tabs = new JTabbedPane(); customerPanel = new JPanel(new BorderLayout());
//1. Construct customers tab: //The data input section:
inputPanel = new JPanel(new GridLayout(4, 4)); id = new JTextField(20); name = new JTextField(20); address = new JTextField(20); phone = new JTextField(20); sex = new JTextField(20); dob = new JTextField(20); photo = new JTextField(20); audio = new JTextField(20);
//The buttons section: backPanel = new JPanel(); middlePanel = new JPanel(new BorderLayout()); buttonPanel1 = new JPanel(); buttonPanel2 = new JPanel(); search = new JButton("SEARCH"); update = new JButton("UPDATE"); clear = new JButton("CLEAR"); add = new JButton("ADD");
void updateTable() { ResultSet results = null; ResultSet results1 = null; try { //Get the number of rows in the table so we know how big to make the data array.. int rowNumbers = 0; int columnCount = 6;
if(rowNumbers == 0) rowNumbers = 1; tableData = new String[rowNumbers][columnCount];
//Initialize the data array with "" so we avoid possibly having nulls in it later.. for(int i =0;i<tableData.length;i++) { for(int j=0;j<tableData[0].length;j++) tableData[i][j] = ""; }
//Populate the data array with results of the query on the database.. int currentRow = 0; results1 = stat.executeQuery("SELECT * FROM CUSTOMER ORDER BY ID"); while (results1.next()) { for(int i = 0; i < columnCount; i++) tableData[currentRow][i] = results1.getString(i + 1); currentRow++; } //while //----------------------------------------------------------------------------- ------------------------ //Create the table model:
final String[] colName = { "CPR", "Name", "Address", "Phone", "Sex", "Date OF Birth" };
TableModel pageModel = new AbstractTableModel() { public int getColumnCount() { return tableData[0].length; } //getColumnCount
public int getRowCount() { return tableData.length; } //getRowCount
public Object getValueAt(int row, int col) { return tableData[row][col]; } //getValueAt
public String getColumnName(int column) { return colName[column]; } //getcolName
public Class getColumnClass(int col) { return getValueAt(0, col).getClass(); } //getColumnClass
public boolean isCellEditable(int row, int col) { return false; } //isCellEditable
public void setValueAt(String aValue, int row, int column) { tableData[row][column] = aValue; } //setValueAt //dataTable.setValue( new JScrollBar(JScrollBar.HORIZONTAL), 2,1 );
}; //pageModel
//----------------------------------------------------------------------------- ------------------------ //Create the JTable from the table model:
//scrollpane = new JScrollPane(dataTable); //scrollpane.setVisible(true);
if (inputPanel == null) makeGUI(); JScrollPane scrollpane = new JScrollPane(dataTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); dataTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// Create vertical box for second tab Box box = Box.createVerticalBox(); // Create component for top box.add(inputPanel); // Place glue between components box.add(Box.createVerticalGlue()); // Create component for middle box.add(middlePanel); // Place glue between components box.add(Box.createVerticalGlue()); // Create component for middle box.add(backPanel); // Place glue between components box.add(Box.createVerticalGlue()); // Create component for bottom box.add(scrollpane); // Add box to JTabbedPane tabs.add(box, "Box"); if (!tabsVisible) {
public void actionPerformed(ActionEvent e) { FileInputStream fis = null;
if (e.getSource() == add) //The ADD button. { //User has not populated all the input fields.
if(name.getText().equals("")|| address.getText().equals("")|| phone.getText().equals("")|| sex.getText().equals("")|| dob.getText().equals("")|| photo.getText().equals("")) { JOptionPane.showMessageDialog(null, "Please fill in all the fields","Missing Fields",JOptionPane.INFORMATION_MESSAGE); } else { // save the new customer:
updateTable(); } //try catch (Exception ee) { //The danger of putting creating the JOptionPane in here is that it will show the same message regardless of the error. JOptionPane.showMessageDialog(null, "Customers CPR already exits!!Please enter another CPR","Invalid",JOptionPane.INFORMATION_MESSAGE); System.out.println("Caught exception in add action: " + ee); } //catch } //if }//add button } //----------------------------------------------------------------------------- ------------------------