How to add margins to the grid elements in grid manager layout of blackberry.I want it to appear exactly like it looks on a sqlite table with rows and columns having neat margins.Also i would like to add color to each row of the grid.The elements in the grid have been fetched from sqlite table string by string.
I have the following code:
GridFieldManager grid = new GridFieldManager(10,5,0);
grid.add(new LabelField("Date"));
grid.add(new LabelField("Bill"));
grid.add(new LabelField("Receipt"));
grid.add(new LabelField("Narration"));
grid.add(new LabelField("ID"));
grid.setColumnPadding(15);
grid.setRowPadding(20);
for (int i = 1; i < grid.getRowCount(); i++) {
System.out.println("Inside for first loops");
for (int j = 0; j < grid.getColumnCount() ; j++) {
System.out.println("Inside for second loops");
while(c.next()) {
System.out.println("Inside while");
Row r;
r = c.getRow();
for (int k = 4; k >=0; k--) {
System.out.println("Inside for loops");
if(k==0 || k==3) {
System.out.println("Retrieving date or narration");
grid.insert(new LabelField(r.getString(k)) {
public void paint(Graphics graphics) {
graphics.setColor(Color.BROWN);
super.paint(graphics);
}
},i,j);
} else {
System.out.println("Retrieving other values");
String p = "" + r.getInteger(k);
grid.insert(new LabelField(p) {
public void paint(Graphics graphics) {
graphics.setColor(Color.BROWN);
super.paint(graphics);
}
},i,j);
}
grid.setBackground(
BackgroundFactory.createLinearGradientBackground(
Color.GOLD,Color.CHOCOLATE,Color.GOLDENROD,Color.CORAL));
}
System.out.println("Exiting while");
}
System.out.println("Exiting sec for");
break;
}
System.out.println("Exiting first for");
break;
}
With the above code I am getting a linear gradient background color being applied to the whole screen. Also the elements of the grid excluding the headings are in brown color.
What I now want to achieve is separate colors for each row.
Anyone aware on the solution to this please share. Thanks.
To add color to the fetched rows you could use this code:
for (int k = 4; k >=0; k--)
{
System.out.println("Inside for loops");
//Check for whether column retrieved is date or naraation
if(k==0 || k==3)
{
System.out.println("Retrieving date or narration");
grid.insert(new LabelField(r.getString(k))
{
public void paint(Graphics graphics)
{
graphics.setColor(Color.GOLD);
super.paint(graphics);
}
},i,j);
}
else
{
//Check for whether column retrieved is bills,rec or id
System.out.println("Retrieving other values");
String p = "" + r.getObject(k);
//if(r.getString(k) != null)
//{
grid.insert(new LabelField(p)
{
public void paint(Graphics graphics)
{
graphics.setColor(Color.GOLD);
super.paint(graphics);
}
},i,j);
// }
}
Using it in if else manner may help your solution.
You can achieve the color part in two ways:
Override the grid manager paint method (difficult)
Add a custom VerticalFieldManager containing exactly one label field to each cell, instead of adding the labelfield directly. This is the easiest approach IMHO.
Related
i have a table full of object(type) when i click "liste des vihicules" i'de like to go to a table full of obeject(vehicule) with this condition:
vihicule.nom_type=type.nom_type
to do this i used this code
////go to vehicule list
#RequestMapping(value="/liste/{nom_type}")
public String listvih(#PathVariable(value = "nom_type") String nom_type, Model model) {
List<Vihicule> l = vihiculeRepository.findAll();
List<Vihicule> l1= null;
for(int i=0; i<l.size(); i++)
{ if(l.get(i).getNom_type()==nom_type)
{
l1.add(i, l.get(i));
}
}
model.addAttribute("v", l1);
return "liste";
}
but it always show an emty table. what could be the problem, please help
update: i did some changes in the code and now everything work fine
#RequestMapping(value="/liste/{nom_type}")
public String listvih(#PathVariable(value = "nom_type") String nom_type, Model model) {
List<Vihicule> l =vihiculeRepository.findAll();
List<Vihicule> l1 = new ArrayList<Vihicule>();
for(int i=0;i<l.size();i++)
{
if(l.get(i).getNom_type().equals(nom_type))
{int j=0;
l1.add(j, l.get(i));
j++;
}
}
model.addAttribute("v",l1);
return "liste";
}
heres my code below...
TableColumn tc = new TableColumn();
TableColumn[] tc2 = new TableColumn[10];
for(int i=0; i<5, i++){
tc.getColumns().add(tc2[i]);
}
and i try to override commit method for editing cells.
public void commit(Object val) {
// Get the table
TableView<MainTable> t = this.getTableView();
// Get the selected row/column
MainTable selectedRow = t.getItems().get(this.getTableRow().getIndex());
TableColumn<MainTable, ?> selectedColumn = t.getColumns().get(t.getColumns().indexOf(this.getTableColumn()));
// Get current property name
String propertyName = ((PropertyValueFactory) selectedColumn.getCellValueFactory()).getProperty();
// Create a method name conforming to java standards ( setProperty )
propertyName = ("" + propertyName.charAt(0)).toUpperCase() + propertyName.substring(1);
// Try to run the update
try {
// Type specific checks - could be done inside each setProperty() method
if(val instanceof Double) {
Method method = selectedRow.getClass().getMethod("set" + propertyName, double.class);
method.invoke(selectedRow, (double) val);
}
if(val instanceof String) {
Method method = selectedRow.getClass().getMethod("set" + propertyName, String.class);
method.invoke(selectedRow, (String) val);
}
if(val instanceof Integer) {
Method method = selectedRow.getClass().getMethod("set" + propertyName, int.class);
method.invoke(selectedRow, (int) val);
}
} catch (Exception e) {
e.printStackTrace();
}
// CommitEdit for good luck
commitEdit((String) val);
}
and i got ArrayIndexOutofBoundsException on console view.
so my question is
how can i select getcolumns added other column???
TableColumn<MainTable, ?> selectedColumn = t.getColumns().get(t.getColumns().indexOf(this.getTableColumn()));
i think this code has to be changed...
anyone got ideas??
Nested columns are not part of the TableView.columns list.
If you need the corresponding TableView column, just go up through the hierarchy until you reach a column without a parentColumn:
TableColumn<MainTable, ?> selectedColumn = this.getTableColumn();
TableColumn<MainTable, ?> c = selectedColumn;
while ((c = selectedColumn.getParentColumn()) != null) {
selectedColumn = c;
}
If you just need the column itself, simply use this.getTableColumn(), instead of finding the index of the column in the columns list and then accessing that index in the same list. (I guess the latter is what you need.)
Furthermore, if PropertyValueFactory returns properties of the item class, you could use this property to set the value instead of using reflection:
ObservableValue obs = selectedColumn.getCellObservableValue(this.getIndex());
if (obs instanceof WritableValue) {
((WritableValue) obs).setValue(val);
} else {
// reflecitive approach
}
Furthermore you shouldn't add null as a nested column, but you're doing it here:
TableColumn[] tc2 = new TableColumn[10];
for(int i=0; i<5, i++){
tc.getColumns().add(tc2[i]);
}
I'm trying to make editable a table view dynamic in javaFX, i already have the dynamic table loading values from a object and works perfect, but i can't make this editable.
i create the dinamic table with this
ArrayList<reportes> rs = BD.ConsultarVarios(selecionado);
for (int column = 1; column +1< rs.size(); column++) {
tablevarios.getColumns().add(
createColumn(column, rs.get(column).getNombre()));
}
// Add data to table:
ArrayList<ObservableList<StringProperty>> data = new ArrayList<>(FXCollections.observableArrayList());
for (int i = 0; rs.get(0).getValor().size() > i; i++) {
ObservableList<StringProperty> data2 = FXCollections.observableArrayList();
for (int k = 0; rs.size() > k+1; k++) {
data2.add(new SimpleStringProperty(rs.get(k).getValor().get(i)));
}
data.add(data2);
tablevarios.getItems().add(data.get(i));
}
but when i try to make editable with this
tablevarios.getColumns().get(i).setCellFactory(column -> new EditCell());
i have this error from the console
Error:(1687, 78) java: incompatible types: bad return type in lambda expression
sample.Main.EditCell cannot be converted to javafx.scene.control.TableCell,capture#1 of ?>
thanks for the help
Does your EditCell extends ListCell ?
I did it in my project like this:
class EditCell extends ListCell<?> {
// your cell properties here
#Override
protected void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
}
tablevarios.getColumns().get(i).setCellFactory(lv -> {
ListCell<?> cell = new EditCell();
//maybe add context menu to cell or other thins
return cell;
}
I'm creating line chart using Java FX. On update, I have a list of checkbox to select or deselect the series of linechart. When all are deselected but tick points or animated tick points are there and so on by doing the same thing again or adding new data for line chart it is same. I'm unable to add images to make it easier for you. Please do some help.
private static void printChartAnalysis(analysisResults r){
clearAnalsisChartData();
int [][]analysisResult=r.getAnalysisResult();
ArrayList<String> versionss=r.getVersions();
int k=0;
for(String x:versionss){
String z=new String();
for(int i=0;i<x.length();i++){
if(Character.isDigit(x.charAt(i))){
z+=x.charAt(i);
}
}
dataLOC.add(new XYChart.Data(z,(int)analysisResult[k][0]));
dataComm.add(new XYChart.Data(z,(int)analysisResult[k][1]));
dataCond.add(new XYChart.Data(z,(int)analysisResult[k][2]));
dataLoops.add(new XYChart.Data(z,(int)analysisResult[k][3]));
dataMem.add(new XYChart.Data(z,(int)analysisResult[k][4]));
k++;
}
seriesLOC.setData(dataLOC);
seriesCond.setData(dataCond);
seriesComm.setData(dataComm);
seriesLoop.setData(dataLoops);
seriesMem.setData(dataMem);
lineChart.getData().setAll(seriesLOC,seriesComm,seriesCond,seriesLoop,seriesMem);
lineChart.setTitle("Line Chart for "+r.getSoftwareName());
for (int i = 0; i < names.length; i++) {
final CheckBox cb = cbs[i];
cb.setSelected(true);
}
}
private static void printChartCost(cocomoResults est){
clearAnalsisChartData();
clearCostChartData();
for(COCOMO cost:est.getVersionsResult()){
String version=cost.getVersion();
String z=new String();
for(int i=0;i<version.length();i++){
if(Character.isDigit(version.charAt(i))){
z+=version.charAt(i);
}
}
dataEffort.add(new XYChart.Data(z,(int)cost.getEffort()));
dataDura.add(new XYChart.Data(z,(int)cost.getDuration()));
dataStaff.add(new XYChart.Data(z,(int)cost.getStaff()));
dataProd.add(new XYChart.Data(z,(int)cost.getProductivity()));
}
seriesEffort.setData(dataEffort);
seriesDura.setData(dataDura);
seriesStaff.setData(dataStaff);
seriesProd.setData(dataProd);
lineChart.getData().setAll(seriesEffort,seriesDura,seriesStaff,seriesProd);
}
private static void clearAnalsisChartData(){
lineChart.getData().removeAll(seriesLOC);
lineChart.getData().removeAll(seriesComm);
lineChart.getData().removeAll(seriesCond);
lineChart.getData().removeAll(seriesLoop);
lineChart.getData().removeAll(seriesMem);
dataLOC.clear();
dataLoops.clear();
dataComm.clear();
dataCond.clear();
dataMem.clear();
}
As far as I understood
cb.setOnAction(event -> {
if (lineChart.contains(*NAME_OF_YOUR_ELEMENT*))
lineChart.remove(*NAME_OF_YOUR_ELEMENT*)
else
lineChart.remove(*NAME_OF_YOUR_ELEMENT*)
});
I'm using tutorial_Form_Table.
The table has dynamically assigned values, something like:
for (col = 1; col <= 5; col++)
{
for (row = 1; row <= 5; row++)
{
if ((col == 2) || (col == 4))
{
if (row > 1)
table.cell(col,row).data(row*col);
else
table.cell(col,row).data("Text "+int2str(row*col));
}
else
table.cell(col,row).data("Text "+int2str(row*col));
}
}
I need to get the position of the cell when I enter new value in it, so I can update the corresponding table with the value entered.
Thank you.
Table has two properties: row() and column() which return values of current active cell.
public void activeCellChanged()
{
super();
info(strFmt('%1 %2 %3', Table.row(), Table.column(), Table.cell(Table.column(), Table.row()).data()));
}
On each of the controls you add to the table control you can override the modified method to see the new value you entered.
public boolean modified()
{
boolean ret;
ret = super();
info(strFmt('New data that we need to save: %1, %2 -> %3', Table.row(), Table.column(), Table.cell(Table.column(), Table.row()).data()));
return ret;
}
If you are working with a lot of data you should consider using some other control such as .NET grid control because of performance issues.