lixtifur68
Programmer
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
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