Tolgar
asked on
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:
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:
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:
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);
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);
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;
}
}
ASKER
Actually, I use SWT not awt. Does anyone have any experience in swt?
Sorry, typo. I meant swt.
Can you try putting your Table in a ScrollPane?
Can you try putting your Table in a ScrollPane?
ASKER
I can but can you please tell me how I will do it?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@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:
Can you please tell me how I can do it 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
}
}
Can you please tell me how I can do it in my code?
>> 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?
Can you show me your code where you add your Table to your panel, window, container, whatever?
ASKER
This is how I put the table in one of the tabs (compTab3):
And this is how I populate data in this table. (Some of the lines are commented out.)
//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);
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();
// }
}
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
Maybe this code snippet helps you:
http://www.roseindia.net/tutorials/swt/create-table-swt.shtml
So, I think you need to put your Table in a ScrollPane