Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Regular Contributor
Posts: 60
Registered: ‎02-24-2011
My Device: Not Specified

How to center field inside TableView

Hello, I'm developing a DataTemplate for a TableView inside my application, however I'm getting the following row:

 

ayuda.png

 

As you can see the second field is not centered... this is the code used by the tableView:

 

public Field[] getDataFields(int modelRowIndex) {
			Object[] data = (Object[])((TableModel)getView().getModel()).getRow(modelRowIndex);
			Field[] fields = new Field[4];
			//TODO: Change Status of icons
			fields[0] = new BitmapField(Bitmap.getBitmapResource("envelope.png"));
			LabelField dateField= new LabelField(data[1],DrawStyle.ELLIPSIS);
			dateField.setFont(listFont.derive(Font.ITALIC));
			fields[1] = dateField;
			fields[2] = new LabelField(data[2], DrawStyle.ELLIPSIS);
			fields[3] = new LabelField(data[3],DrawStyle.ELLIPSIS);
			return fields;
		}

 How can I make the second field to center in the row??

 

Thanks a lot.

Developer
Posts: 17,011
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to center field inside TableView

can you get the height of a row?
use (rowHeight-fieldHeight) / 2 and add that as a margin on the field.
(on a listfield you would have a y modifier)
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Regular Contributor
Posts: 60
Registered: ‎02-24-2011
My Device: Not Specified

Re: How to center field inside TableView

Yeah... I already tried to add margin before... however no luck. This time as you suggested I tried the following code:

 

int margin=(20-dateField.getHeight())/2;
			dateField.setMargin(margin, 0, margin, 0);

 However I got the same table... no results.

Regular Contributor
Posts: 60
Registered: ‎02-24-2011
My Device: Not Specified

Re: How to center field inside TableView

I have tried to apply margin, padding to the Field that's not being centered. However got no results... I can't understand de behaviour of the TableView in this case. Please, this is the entire code I have:

 

public WorkflowDetailListScreen(){
		super(NO_VERTICAL_SCROLL);
		setTitle(new LabelField("Workflow Details", Field.FIELD_HCENTER | Field.NON_FOCUSABLE)); 
		
		TableModel tableModel = new TableModel();
		TableView view = new TableView(tableModel);
		WorkflowDataTemplate template = new WorkflowDataTemplate(view);
		template.createRegion(new XYRect(0,0,1,1));
		template.createRegion(new XYRect(1,0,1,1));
		template.createRegion(new XYRect(2,0,1,1));
		template.createRegion(new XYRect(3,0,1,1));
		template.setColumnProperties(0, new TemplateColumnProperties(10,TemplateColumnProperties.PERCENTAGE_WIDTH));
		template.setColumnProperties(1, new TemplateColumnProperties(15,TemplateColumnProperties.PERCENTAGE_WIDTH));
		template.setColumnProperties(2, new TemplateColumnProperties(30,TemplateColumnProperties.PERCENTAGE_WIDTH));
		template.setColumnProperties(3, new TemplateColumnProperties(45,TemplateColumnProperties.PERCENTAGE_WIDTH));
		template.setRowProperties(0, new TemplateRowProperties(20));
		template.useFixedHeight(true);
		view.setDataTemplate(template);
		TableController controller=new TableController(tableModel, view);
		controller.setFocusPolicy(TableController.ROW_FOCUS);
		view.setController(controller);
		//controller.setCommand(new Command(new CallCommand()));
		//add(new LabelField("Who you gonna call?"));
		FontFamily fontFamily;
		try {
			fontFamily = FontFamily.forName("BBClarity");
			listFont = fontFamily.getFont(FontFamily.CBTF_FONT, 15);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		add(view);
				
		for(int i=0;i<items.size();i++){
			WorkflowItem item=(WorkflowItem)items.elementAt(i);
			tableModel.addRow(new Object[]{new Integer(item.getStatus()),item.getDate(),item.getTitle(),item.getContent()});
		}
		
		
	    
	 		
		
		
	}
	
	private class WorkflowDataTemplate extends DataTemplate {

		public WorkflowDataTemplate(TableView view) {
			super(view,1,4);

		}

		public Field[] getDataFields(int modelRowIndex) {
			Object[] data = (Object[])((TableModel)getView().getModel()).getRow(modelRowIndex);
			Field[] fields = new Field[4];
			//TODO: Change Status of icons
			fields[0] = new BitmapField(Bitmap.getBitmapResource("envelope.png"));
			LabelField dateField= new LabelField(data[1],DrawStyle.ELLIPSIS);
			dateField.setFont(listFont.derive(Font.ITALIC));
			int margin=(20-dateField.getHeight())/2;
			dateField.setPadding(10, 0, 0, 0);
			fields[1] = dateField;
			fields[2] = new LabelField(data[2], DrawStyle.ELLIPSIS);
			fields[3] = new LabelField(data[3],DrawStyle.ELLIPSIS);
			return fields;
		}

	}