hi .. i have a JFrame that has a table ... i need a vertical scrollbar for that table, because when i add users to the table i want to go down the table by the scrollbar to see their info without the scrollbar i can't see their info..
this is part of the code but when i run my application the scrollbar will not appear...
class UtilityMethods extends JFrame implements ActionListener { JPanel customerPanel; JPanel inputPanel; JTable dataTable; static String[][] tableData; JScrollPane scrollpane; void makeGUI() { c.setLayout(new BorderLayout()); customerPanel = new JPanel(new BorderLayout()); } Update the customer table from the database:
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[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] = 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: