1

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")},
             };







    }
derek
  • 123
  • 2
  • 4
  • 15

1 Answers1

4

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • @derek: As an aside, don't call `setPreferredSize()`; override `getPreferredScrollableViewportSize()`,as suggested [here](http://stackoverflow.com/a/16070046/230513). – trashgod May 05 '13 at 02:58
  • i figured the redundancy was unnecessary but i was just hoping to get something to work. Thank you for your response. That worked. however now i am having trouble referencing the 2d Object array created in the while loop. i tried initializing the variable prior to the loop but then it says illegal start of expression in the loop? ill append my OP to show. – derek May 05 '13 at 03:05
  • thank you trashgod. i am now on my third/fourth week learning java, im still learning all the recommended practices – derek May 05 '13 at 03:10
  • @derek: your object reference is a distinct and unrelated problem and probably should be asked in a new question. Quite possibly you have a problem of scope, where the 2D array is declared inside of a method or other block and thus is only visible within that method or block. – Hovercraft Full Of Eels May 05 '13 at 03:53