Reputation: 123
I have a main class that uses a swing interface. There is a JScrollPane that i can get to show up. I have an actionlistener that fills the table and is supposed to add the table to the JScrollPane. however when i initialize the scrollPane i need to declare (table example) as an argument...cant figure out how to reference it after its been populated. heres the code:
JScrollPane callsScroll = new JScrollPane();
JPanel callsArea = new JPanel();
....
callsArea.add(callsScroll);
callsScroll.setPreferredSize(new Dimension(570, 400));
...
while (rs.next()) {
String columns[] = {"Employee ID", "Date", "Billing Type", "DR", "Call Time", "Trouble Code", "Invoice Amount", "Invoice Number"};
Object data[][]={
{rs.getString("EmployeeID"),rs.getString("Date"),rs.getString("BillingType"),
rs.getString("DR"),rs.getString("CallTime"),rs.getString("TroubleCode"),
rs.getString("InvoiceAmount"),rs.getString("InvoiceNumber")
},
};
JTable table = new JTable(data,columns);
gui.callsScroll.add(table);
gui.callsArea.add(gui.callsScroll);
}
i know the table is being populated because i can add it straight to the JFrame. but i cannot get it into the table first. i need to be able to reference the instance of the table after its been populated here JScrollPane callsScroll = new JScrollPane(right here);
******EDIT**************
Object[][] data=null;
while (rs.next()) {
data[][]={
{rs.getString("EmployeeID"),rs.getString("Date"),rs.getString("BillingType"),
rs.getString("DR"),rs.getString("CallTime"),rs.getString("TroubleCode"),
rs.getString("InvoiceAmount"),rs.getString("InvoiceNumber")},
};
}
Upvotes: 1
Views: 91
Reputation: 285405
Don't add components directly to the JScrollPane as you will replace the scrollpane's viewport, an essential component for scrollpane functionality. Instead you should add the JTable to the scrollpane's viewport:
myScrollpane.setViewportView(myJTable);
Note that when you add a component to a JScrollPane via its constructor, you're actually adding it to the viewport, and this can confuse folks who are starting out working with JScrollPanes.
Also, no need to re-add a component that's already been added to the GUI.
Upvotes: 4