How to resize the table length based on the number of rows in Java?

Hi,
I have a Java code that creates a UI and I create a table in this UI in the next tab that I have. Then I do some stuff and after it processes them,  I populate some information in this table. and I then I switch to this tab to see the table.  

Until this point everything seems to work fine. However, the data that I populate does not show up completely when I first switch the tab. I can only see the first line of the table which is already there. In order to  see the rest of the rows, I need to resize the window and then the rest of the rows show up.

In order to solve this problem I put a vertical scroll bar to the table. The scroll bar seems like it works but I can still see one row of data when I click on the arrow in the scroll bar because the table initially has only one row of height.

I am posting the code that I use to create the table.

Can you please help me either to expand the table automatically as the code adds more rows of data to it or to initially put some fixed number of data instead of one so that I can use the scroll bar meaningfully?


This is how I create the table:

        //This is the results table
        Table table = new Table(compTab3, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
        //GridData resultsGridData = new GridData(1, 100, false, false);
        table.setLayout(progressBarGridLayout);
        table.setHeaderVisible(true);
        table.getVerticalBar().setVisible(true);
        //String[] titles = { "Override", "Rerun", "Status", "Check", "Filename", "Existing Line Numbers", "New Line Numbers" };
        String[] titles = { "Status", "Check", "Filename", "Existing Line Numbers", "New Line Numbers" };
        
        for (int loopIndex = 0; loopIndex < titles.length; loopIndex++) {
            TableColumn column = new TableColumn(table, SWT.NULL);
            column.setText(titles[loopIndex]);
            column.setWidth(80);
        }
        
        toolBarListener.setTable(table,titles);
                 
         GridData data3 = new GridData(GridData.FILL_HORIZONTAL;
         data3.verticalAlignment = SWT.BOTTOM;
         data3.horizontalSpan = 2;
         table.setLayoutData(data3);

Open in new window


Actually, while I was writing this post, I tried something to expand the table without considering the length of the content and it worked. I basically changed line 18 like this:
         GridData data3 = new GridData(GridData.FILL_HORIZONTAL, GridData.FILL_VERTICAL);

Open in new window


This solves my problem partially, because this is not what I really want. In this case the table is so huge when I have just two rows and it looks weird. I think the best solution is to be able to expand the table based on the number of rows up to a point where it fills the window size.  

Can you please help me how to do it?

The code that populates the data in the table is like this:

    public static void populateResultsTable(List<CheckDetail> checkDetails, Shell shell, Table table, String[] titles){
    
        //ParseResultsXML parseResultsXML = new ParseResultsXML();
        
        //List<CheckDetail> checkDetails = parseResultsXML.getResultsXMLData();
        //CheckDetail checkDetail = new CheckDetail();
        
        boolean evenRow = true;
        for (CheckDetail checkDetail : checkDetails) {
            TableItem item = new TableItem(table, SWT.NULL);
            //item.setText(0, checkDetail.getStatus()); //Override
            //item.setText(1, checkDetail.getStatus()); //Rerun
            item.setText(0, checkDetail.getStatus()); //Status
            item.setText(1, checkDetail.getName()); //Check
            item.setText(2, checkDetail.getFileName()); //Filename
            item.setText(3, checkDetail.getExistingLineNumbers()); //Existing Line Numbers
            item.setText(4, checkDetail.getNewLineNumbers()); //New Line Numbers
            
            //This is to make shadow for every other line
            if (evenRow)
                item.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
            evenRow = !evenRow;
         }
    }

Open in new window

TolgarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zzynxSoftware engineerCommented:
I have no experience in java awt, but in java Swing you have to put your JTable in a JScrollPane.
So, I think you need to put your Table in a ScrollPane
0
TolgarAuthor Commented:
Actually, I use SWT not awt. Does anyone have any experience in swt?
0
zzynxSoftware engineerCommented:
Sorry, typo. I meant swt.
Can you try putting your Table in a ScrollPane?
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

TolgarAuthor Commented:
I can but can you please tell me how I will do it?
0
Amitkumar PSr. ConsultantCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TolgarAuthor Commented:
@amit_n_panchal: Sorry. I couldn't understand how I will implement this approach in my code.

By the way, I made some other changes to my populateResultsTable method and it is like this:

    public static void populateResultsTable(List<CheckDetail> checkDetails, Shell shell, Table table, String[] titles){
           
        table.removeAll();
        boolean evenRow = true;

        for(Iterator<CheckDetail> i = checkDetails.iterator();i.hasNext();) {
            CheckDetail checkDetail = i.next();
            TableItem item = new TableItem(table, SWT.NULL);
            item.setText(0, checkDetail.getStatus()); //Status
            item.setText(1, checkDetail.getName()); //Check
            item.setText(2, checkDetail.getFileName()); //Filename
            item.setText(3, checkDetail.getExistingLineNumbers()); //Existing Line Numbers
            item.setText(4, checkDetail.getNewLineNumbers()); //New Line Numbers
            
            //This is to make shadow for every other line
            if (evenRow)
                item.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
            evenRow = !evenRow;
            
            i.remove(); // clear
         }
    }

Open in new window


Can you please tell me how I can do it in my code?
0
zzynxSoftware engineerCommented:
>> I can but can you please tell me how I will do it?
Can you show me your code where you add your Table to your panel, window, container, whatever?
0
TolgarAuthor Commented:
This is how I put the table in one of the tabs (compTab3):

//This is the results table
        Table table = new Table(compTab3, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
        //GridData resultsGridData = new GridData(1, 100, false, false);
        //table.setLayoutData(resultsGridData);
        table.setHeaderVisible(true);
        table.getVerticalBar().setVisible(true);
        //String[] titles = { "Override", "Rerun", "Status", "Check", "Filename", "Existing Line Numbers", "New Line Numbers" };
        String[] titles = { "Status", "Check", "Filename", "Existing Line Numbers", "New Line Numbers" };
        
        for (int loopIndex = 0; loopIndex < titles.length; loopIndex++) {
            TableColumn column = new TableColumn(table, SWT.NULL);
            column.setText(titles[loopIndex]);
            column.setWidth(80);
        }
        
        toolBarListener.setTable(table,titles);
                 
         GridData data3 = new GridData(GridData.FILL_HORIZONTAL, GridData.FILL_VERTICAL);
         data3.verticalAlignment = SWT.TOP;
         data3.horizontalAlignment = SWT.LEFT;
         data3.horizontalSpan = 5;
         table.setLayoutData(data3);

Open in new window


And this is how I populate data in this table. (Some of the lines are commented out.)
    public static void populateResultsTable(List<CheckDetail> checkDetails, Shell shell, Table table, String[] titles){
    
        //ParseResultsXML parseResultsXML = new ParseResultsXML();
        
        //List<CheckDetail> checkDetails = parseResultsXML.getResultsXMLData();
        //CheckDetail checkDetail = new CheckDetail();
        table.removeAll();
        boolean evenRow = true;
        //for (CheckDetail checkDetail : checkDetails) {
        for(Iterator<CheckDetail> i = checkDetails.iterator();i.hasNext();) {
            CheckDetail checkDetail = i.next();
            TableItem item = new TableItem(table, SWT.NULL);
            //item.setText(0, checkDetail.getStatus()); //Override
            //item.setText(1, checkDetail.getStatus()); //Rerun
            item.setText(0, checkDetail.getStatus()); //Status
            item.setText(1, checkDetail.getName()); //Check
            item.setText(2, checkDetail.getFileName()); //Filename
            item.setText(3, checkDetail.getExistingLineNumbers()); //Existing Line Numbers
            item.setText(4, checkDetail.getNewLineNumbers()); //New Line Numbers
            
            //This is to make shadow for every other line
            if (evenRow)
                item.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
            evenRow = !evenRow;
            
            i.remove(); // clear
         }
         
//         for (int loopIndex = 0; loopIndex < titles.length; loopIndex++) {
//              table.getColumn(loopIndex).pack();
//         }
    }

Open in new window

0
zzynxSoftware engineerCommented:
I'm sorry I don't know SWT enough to help.
Maybe this code snippet helps you:
http://www.roseindia.net/tutorials/swt/create-table-swt.shtml
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.