I'm adding data in table-view in javafx. I'm also setting the table columns as shown, they are working fine. Any comments on where I am wrong?
c1 = new TableColumn<String, String>("First Name"); //c1 is column
c1.setCellValueFactory(new PropertyValueFactory<String,String>("Name"));
table.setItems(value); //table is tableview
table.getItems().add("My name");
table.getColumns().addAll(c1);
count++;
Related
Receive my greetings everyone!I'm new to javafx.For my first project of Javafx ,I want to create a tableview which Tablecolumns changes according to an event.Suppose that first time,My tableview has 4 columns such as (name,age,email,address).After an event,I want to add the tablecolumn profession for my tableview to have now 5 columns (name,age,email,adress,profession).After an other event,I want to remove profession for my tableview to have again 4 tablecolumns.Thank you for your help.Excuse me for my english.
Your table view has four columns so you already know how to add columns to the table view. Adding another is no different,(unless you originally defined your columns using FXML). Regardless, the addition and removal of columns in code is as demonstrated below.
Create your new column (add appropriate generic type info and initialization code):
final TableColumn fifthColumn = new TableColumn("Alien resistance");
// initialize the column
When you receive an event, add the fifth column:
Button insurrection = new Button("Add"):
insurrection.setOnAction(e ->
tableView.getColumns().add(
fifthColumn
)
);
similarly for the action on a remove button:
Button failedCoup = new Button("Remove"):
failedCoup.setOnAction(e ->
tableView.getColumns().remove(
fifthColumn
)
);
Is it possible to take a value of table column from a event handler in Javafx?
The task is I need to click a button, calculate functions, and return the value to the column.
I have this working:
data.add(new Person(id, name));
I have this:
TableColumn nameCol = new TableColumn ("name");
nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
// And id as well.
TableColumn resultsCol = new TableColumn ("Results");
resultsCol.setCellValueFactory(new PropertyValueFactory<>("results"));
And this:
add.setOnAction (e -> {
// I don't know what to put
});
Because id and name column are already working, what should I do to separately add a new value to the column from a button?
Because your CellValueFactories are pulling values directly from your Person object you would need to update the variables in the particular row's Person object when your button is clicked. You can do this with simple setter methods.
You could also use a technique like this to create a table with cells that can be edited with a click: http://java-buddy.blogspot.com/2012/04/javafx-2-editable-tableview.html?m=1
I have successfully implemented this technique in my applications.
since my code is quite long, I'll try to only describe the issue first to see if someone has faced it before.
I have a TableView that i populate with various objects which works just fine. I created a context menu so whenever I right-click an Item i can edit certain columns. Everything works fine but when i maximize the window, I cant select any Items anymore by clicking on them.
Has anyone faced that issue before?
Thx for the help!
edit:
I added some lines of code. Im using Java 1.8.
anchorPane = new AnchorPane();
anchorPane.setMaxWidth(550.0);
tabPane = new TabPane();
tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);
tabPane.setTabMinHeight(22.0);
tabPane.setPrefWidth(this.navigationWidth);
homeTableView = new TableView<Player>();
homeTableView.setEditable(true);
homeTableView.setContextMenu(homeContextMenu);
List<Player> test= mainWindow.getHomeTeamPlayer();
ObservableList<Player> homeTeamPlayer = FXCollections.observableArrayList(test);
homeTableView.setItems(homeTeamPlayer);
homeTableView.setContextMenu(homeContextMenu);
Field[] fields = Player.class.getDeclaredFields();
for(Field field: fields){
// Get column information from Metadata
ColumnMetadata columnMetadata = field.getAnnotation(ColumnMetadata.class);
if(!columnMetadata.showInTable()) continue;
TableColumn<Player, String> HomeCol = new TableColumn<>(columnMetadata.displayTitle());
HomeCol.setCellValueFactory(new PropertyValueFactory<>(field.getName()));
if(columnMetadata.isEditable()){
HomeCol.setEditable(true);
}
homeTableView.getColumns().add(HomeCol);
}
homeTab = addTab("homename", homeTableView);
AnchorPane.setRightAnchor(tabPane, 0.0);
AnchorPane.setLeftAnchor(tabPane, 0.0);
AnchorPane.setTopAnchor(tabPane, 0.0);
AnchorPane.setBottomAnchor(tabPane, 0.0);
anchorPane.getChildren().addAll(tabPane);
public Tab addTab(String name, TableView tableView){
Tab tab = new Tab();
tab.setText(name);
tab.setContent(tableView);
tabPane.getTabs().addAll(tab);
return tab;
}
I'm playing around with JavaFX and wanted to add a tooltip that pops up with the data value when the mouse is hovering over the node. I found several links and answers out there describing CSS methods for doing it, or using Tooltip.install(node, tooltip) and while I could get a tooltip working on a dummy example, I was still having no luck with the chart, using code like so :
LineChart<Number, Number> chart = new LineChart(xaxis, yaxis);
ObservableList<Data<Number, Number>> data = FXCollections.observableArrayList();
Data<Number, Number> d1 = new XYChart.Data<Number, Number>(5, 15);
Tooltip tooltip = new Tooltip("15");
Tooltip.install(d1.getNode(), tooltip);
data.add(d1);
Data<Number, Number> d2 = new XYChart.Data<Number, Number>(10, 25);
Tooltip tooltip2 = new Tooltip("25");
Tooltip.install(d2.getNode(), tooltip2);
data.add(d2);
chart.setData(data);
//add chart to scene etc etc etc
After some digging, the issue here is that a data element (XYChart.Data) does not have a node created at construction time. chart.setData(data) will populate the node field - I believe this is to allow the user to create and set their own nodes if so desired. So d1.getNode() is actually returning null, and Tooltip.install() is silently failing.
Moving the Tooltip.install call after chart.setData solves the issue.
I am newbie on javafx. I already code in core java to set value to table. But now i am converting my project in javafx to maintain code and to improve design.
I am trying to set value to table view but i am not getting how to set value to tableview.
Here is my code which i am using for swing.
String testbedName = treePath.getPathComponent(1).toString();
TestBed currentTestbed = getTbF().get(testbedName);
propertyTable.getModel().setValueAt(currentTestbed.getTestBedName(), 0, 1);
propertyTable.getModel().setValueAt(currentTestbed.getTestBedFilePath() + "\\" + testbedName + ".xml", 1, 1);
propertyTable.getModel().setValueAt(currentTestbed.getTbElements().size(), 2, 1);
But i need to convert this code in javafx means from JTable to TableView. but tableview not find getModel() method.
I search but didn't get how to set value to table view.
Please give me reference or hint.
Thanks in advance.
In JavaFX you generally don't set the values cell by cell like that. In your case, for example, you would
place your TestBeds in the table:
TableView<TestBed> table = new TableView<>();
table.getItems().addAll(yourTestBeds);
you then create the columns you need, by providing appropriate cell value factories, for example (using Java 8 syntax):
TableColumn<TestBed, String> name = new TableColumn<>();
name.setCellValueFactory(c-> new SimpleStringProperty(c.getValue().getTestBedName()));
If you use Java 7:
name.setCellValueFactory(new Callback<CellDataFeatures<TestBed, String>, ObservableValue<String>>() {
#Override public ObservableValue<String> call(CellDataFeatures<TestBed, String> c) {
return new SimpleStringProperty(c.getValue().getTestBedName()));
}
});
Finally you add the columns to the table:
table.getColumns().addAll(name, someOtherColumn);
Suppose we have a table with two columns, one for a string, the other for a number. To set programmatically the i-th item or row of the table, we can do like this:
//Define the string
String s = "myString" ;
//Define the number
int value = 5;
//Synthesize the item = row
Item item = new Item(s, value);
//Set the i-th item
table.getItems().set(i, item);