Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JTable -Please help

Status
Not open for further replies.

lixtifur68

Programmer
Jan 28, 2004
1
US
I am trying to create a JTable in Java that gets its results from a query. The JTable uses the DefaultTableModel as in:

String[] colHeads ={"Name","Street","City","State"};
private DefaultTableModel model = new DefaultTableModel(colHeads, 0);
private JTable table = new JTable(model);

private JScrollPane sPane = new JScrollPane(table);
-------------
then I set the rows and columns = 2 as in:
model.setRowCount(2);
model.setColumnCount(2);
This should give me a grid of 2 columns and 2 rows with a scrollbar to scroll through the available rows and columns in the model.

I then loop through the ResultSet and add each row to the model as in:
public void actionPerformed(ActionEvent e){
String userName;
String password;
String sql = "select e.*, s1.external_trade_status_name, s2.external_trade_state_name, src.external_trade_src_name from exch_tools_trade e, external_trade_status s1, external_trade_state s2, external_trade et, external_trade_source src where e.external_trade_oid = et.oid and et.external_trade_status_oid = s1.oid and src.oid = et.external_trade_source_oid and s2.oid = et.external_trade_state_oid";

if (e.getSource()==goButton){
userName = userField.getText();
password = userPass.getText();


try{
Class.forName ("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection conn=DriverManager.getConnection("jdbc:microsoft:sqlserver://167.206.83.98:1433;User=" + userName +";Password="+password+";DatabaseName=stamprod_sql");
loginOK();
//create a statement for the connection
stmt = conn.createStatement();
ResultSet rsData = stmt.executeQuery(sql);

int i=0;
//-----------
while (rsData.next() ){
i++;
Object[] cells={rsData.getString(1),rsData.getString(2),rsData.getString(3),rsData.getString(4)};
model.addRow(cells);


}
loginOK();
}
catch (Exception e1){
loginFailed();
loggedFailed = true;
System.out.println("test "+e);
}

}
}
-------------
When I set rows =2 and cols =2, how come there is no scrollbars allowing the user to scroll through the columns and there are 2 blank rows that are displayed before any other rows. I want the JTable to be a 2X2 grid with scrollbars to scroll through the other cells in the model.

Thank you
 
The table (with 2x2 cells) should not be large enough to require scroll.... ScrollPane knows the size of the item in it... If the tabel grows beyond the edges of the pane, the scroll bars should appear arround the table.
Your calls to setRowCount and setColum count don't work the way you think they do:
setRowCount
public void setRowCount(int rowCount)Sets the number of rows in the model. If the new size is greater than the current size, new rows are added to the end of the model If the new size is less than the current size, all rows at index rowCount and greater are discarded.


See Also:
setColumnCount(int)
So, your call is adding 4 cells (2 rows 2 colums) before, then you query your database and fill the rows bellow it (thus adding rows and collums) expanfing the number of colums. Sounds like what you need to play with is the JTable, after you do the propagation, not the model.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top