I want to add rows to a table depending on an ID i get earlier.
I have managed to do it in SWT but I do not know how to do it in javaFx.
Here is the code I wrote for SWT:
int availableDesings = designManagement.getNumberOfDesigns();
ArrayList<Design> designs = designManagement.getDesignArray();
for (int i = 0 ; i< availableDesings ; i++){
TableItem item = new TableItem(table, SWT.NONE);
item.setText (0, String.valueOf(designs.get(i).getId()));
item.setText (1, String.valueOf(designs.get(i).getPart_name()));
}
Can anyone help me translating the code to javaFx?
If you have a TableView in JavaFX and it is assigned to a ObservableList of items :
TableView<String> table = new TableView<>();
ObservableList<String> list = FXCollections.observableArrayList();
table.setItems(list);
Then you can add data to the Table, by adding data to the list :
table.getItems().add("New Item");
or, you can directly add data to the list :
list.add("New Item");
For more, information, check this link :
Adding New Rows
private ObservableList<Design> data = FXCollections.observableArrayList();
.....
....
data.addAll(designManagement.getDesignArray()); //all you items
TableView<Design> table = new TableView<>();
TableColumn<Design, String> column1 = new TableColumn<>();
column1.setCellValueFactory(new PropertyValueFactory<Design, String>("id"));
TableColumn<Design, String> column2 = new TableColumn<>();
column2.setCellValueFactory(new PropertyValueFactory<Design, String>("part_name"));
table.getColumns().add(column1);
table.getColumns().add(column2);
table.setItems(data);
you can actually add row to a tableview in javafx though i haven't tried using it. But i have done it with TableColumn.
This is some code that i have done for my small project
TableView<Person> personTable = new TableView<>();
TableColumn<Person,String> nameColumn=new TableColumn<>("Name");
nameColumn.setMinWidth(200);
nameColumn.setCellValueFactory(new PropertyValueFactory<>("Name"));
TableColumn<Person, Gender> genderColumn=new TableColumn<>("Gender");
genderColumn.setMinWidth(30);
genderColumn.setCellValueFactory(new PropertyValueFactory<>("gender"));
TableColumn<Person,String> mobileNumberColumn=new TableColumn<>("mobileNumber");
mobileNumberColumn.setMinWidth(150);
mobileNumberColumn.setCellValueFactory(new PropertyValueFactory<>("mobileNumber"));
TableColumn<Person, Blood> bloodColumn=new TableColumn<>("Blood Type");
bloodColumn.setMinWidth(30);
bloodColumn.setCellValueFactory(new PropertyValueFactory<Person, Blood>("blood"));
personTable.setItems(getPerson());//setting content retrieved from the database into the table
personTable.getColumns().addAll(nameColumn, genderColumn, mobileNumberColumn,bloodColumn);//adding all columns to table
so what it means is when i say
TableColumn mobileNumberColumn=new TableColumn<>("mobileNumber");
i mean mobileNumberColumn is using the class Person from which a string is assigned to the column. the mobile number in quotes is the name of the table.
mobileNumberColumn.setCellValueFactory(new PropertyValueFactory<>("mobileNumber"));
The above line say that the value of the column is the instance variable mobileNumber of the the class person.But for this to work you should have a getMobileNumber() method in person class. It has to be exactly like that for the tableView to work.
hope this helps
Related
This is my current Display board and I would like to add a tooltip on top of the Items menu. How do I do this since itemNameCol.setTooltip is undefined? My code is below.
TableView<AuctionItem> table = new TableView<AuctionItem>();
table.setEditable(true);
table.setMinSize(500, 510);
TableColumn<AuctionItem, String> itemNameCol = new TableColumn<>("Items");
itemNameCol.setCellValueFactory(
new PropertyValueFactory<AuctionItem, String>("name"));
Okey so my problem is next: I use web scraping to take some data from web page IMDB in this case, that data is titles of movies, and I already tried to print it in console and that works fine. My problem is that I can not save that titles in my table columns, I put all needed codes for this problem, and I cant find where I made mistake and why titles wont show in table columns. At the and, user need to pick one title and that title need to be stored in text field. Have someone any idea, please?
I have table:
TableColumn izborAuta = new TableColumn("Izbor auta");
TableColumn lokacijaPreuzimanja = new TableColumn("Lokacija
preuzimanja");
TableColumn lokacijaVracanja = new TableColumn("Lokacija Vracanja");
TableColumn cena = new TableColumn("Cena");
I have this code to setup columns:
izborAuta.setCellValueFactory(new PropertyValueFactory<Vozila, String>
("izborAuta"));
lokacijaPreuzimanja.setCellValueFactory(new
PropertyValueFactory<Vozila, String>("lokacijaPreuzimanja"));
lokacijaVracanja.setCellValueFactory(new
PropertyValueFactory<Vozila, String>("lokacijaVracanja"));
cena.setCellValueFactory(new PropertyValueFactory<Vozila, String>
("cena"));
tableView.setItems(Baza.baza.prikazBaze());
tableView.getColumns().addAll(izborAuta, lokacijaPreuzimanja,
lokacijaVracanja, cena);
I have this code, so when I pick one item from table that item need to be stored in textField:
tableView.setOnMouseClicked((e) -> {
Vozila v = (Vozila)
tableView.getSelectionModel().getSelectedItem();
txIzborAuta.setText(v.getIzborAuta());
txLokacijaPreuzimanja.setText(v.getLokacijaPreuzimanja());
txLokacijaVracanja.setText(v.getLokacijaVracanja());
txCena.setText(v.getCena());
});
And at the end I use web scraping to save items in table:
Document doc = Jsoup.connect("https://www.imdb.com/chart/top?
ref_=nv_mv_250").get();
Elements elems = doc.select("table.chart.full-width");
for (Element e : elems) {
String izborAuta = e.select(".titleColumn").text();
String lokacijaPreuzimanja = e.select(".titleColumn").text();
String lokacijaVracanja = e.select(".titleColumn").text();
String cena = e.select(".titleColumn").text();
Vozila v = new Vozila();
v.setIzborAuta(izborAuta);
v.setLokacijaPreuzimanja(lokacijaPreuzimanja);
v.setLokacijaVracanja(lokacijaVracanja);
v.setCena(cena + " " + "RSD");
Baza.insertVozila(v);
}
I'm currently developing an eclipse plugin and in that plugin, there is a form view as a design template. In that form view, I have added a Table and there should have two columns in width ratio of 1:2. And also I want that table to be responsive and dynamically change its column width in order to the formView page width.
The following code segment is the one I'm currently using.
Table table = new Table(parent, SWT.MULTI | SWT.H_SCROLL | SWT.BORDER);
fd = new FormData();
fd.height = 200;
fd.top = new FormAttachment(removeTestCaseButton, 5);
fd.left = new FormAttachment(1);
fd.right = new FormAttachment(99);
table.setLayoutData(fd);
table.setLinesVisible(true);
table.setHeaderVisible(true);
TableColumn column1 = new TableColumn(testCaseTable, SWT.CENTER);
column.setText("column One");
TableColumn column2 = new TableColumn(testCaseTable, SWT.CENTER);
column2.setText("column Two");
form.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
Rectangle area = form.getBody().getClientArea();
int width = area.width;
column1.setWidth(width / 3);
column1.setWidth(width * 2 / 3);
}
});
But here the problem is when I open the FormView it works fine. But my table is inside a Section. Once I expand or collapse the Section the table width is getting increased with appearing the horizontal scrollbar.
I just want a solid solution for this.
This is much easier to do using the JFace TableViewer with the TableColumnLayout and ColumnWeightData, but you will have to rework your code to use JFace style content and label providers for the table.
TableColumnLayout tableLayout = new TableColumnLayout();
// A separate composite containing just the table viewer is required
Composite tableComp = new Composite(parent, SWT.NONE);
tableComp.setLayout(tableLayout);
TableViewer viewer = new TableViewer(tableComp, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
TableViewerColumn col1 = new TableViewerColumn(viewer, SWT.LEAD);
col1.getColumn().setText("Column 1");
col1.setLabelProvider(.... label provider for column 1 ....);
// Weight for column
tableLayout.setColumnData(col1.getColumn(), new ColumnWeightData(60));
TableViewerColumn col2 = new TableViewerColumn(viewer, SWT.LEAD);
col2.getColumn().setText("Column 2");
col2.setLabelProvider(....... label provider for column 2 .....);
// Weight for column
tableLayout.setColumnData(col2.getColumn(), new ColumnWeightData(40));
viewer.getTable().setHeaderVisible(true);
viewer.getTable().setLinesVisible(true);
viewer.setContentProvider(ArrayContentProvider.getInstance());
viewer.setInput(.... input data for the viewer ....);
I'm working on a project where the TableView looks like this:
The datas are inside are a MySQL database, and I use a modell and an observableArrayList to get them.
I want to make an initialize method which read the whole selected Line and save it into the TextFields (like Name-Name, Address - Address etc). I tried with this, and now I can Select the Whole Line data, but can't split it into Strings to set the TextField texts:
ListViewerModell details = (ListViewerModell) table.getSelectionModel().getSelectedItem();
//Checking if any row selected
if (details != null) {
//??? - split
editName.setText("*Name*");
editAddress.setText("*Address*");
}
Tried with the modell getName(), getAdress() methods but I got NullPointerException and TablePos methods.
// create table
TableView<Person> table = new TableView<Person>();
// create columns
TableColumn<Person, String> nameCol = new TableColumn<Person, String>("Name");
nameCol.setCellValueFactory(new PropertyValueFactory("name"));
TableColumn<Person, Address> addressCol = new TableColumn<Person, String>("Address");
addressCol.setCellValueFactory(new PropertyValueFactory("address"));
// add columns
table.getColumns().setAll(nameCol, addressCol);
// get data from db, return object List<Person> from DB
ObservableList<Person> persons = getPersonsFromDB();
table.setItems(persons);
tableView.setOnMouseClicked((MouseEvent event) -> {
if (event.getClickCount() > 1) {
onEdit();
}
});
public void onEdit() {
// check the table's selected item and get selected item
if (table.getSelectionModel().getSelectedItem() != null) {
Person selectedPerson = table.getSelectionModel().getSelectedItem();
nameTextField.setText(selectedPerson.getName());
addressTextField.setText(selectedPerson.getAddress());
}
}
One way could be extracting the value of the columns of the selected row, like so :
Object selectedItems = table.getSelectionModel().getSelectedItems().get(0);
String first_Column = selectedItems.toString().split(",")[0].substring(1);
System.out.println(first_Column);
I have the following Node creator in my Java application:
private Node createWelcomePane() {
HBox hbox_accounts = new HBox();
this.getAccounts();
tableAccounts.setPrefSize(500,500);
final Label label = new Label("Address Book");
label.setFont(new Font("Arial", 20));
tableAccounts.setEditable(false);
TableColumn idCol = new TableColumn("ID");
idCol.setMinWidth(100);
idCol.setCellValueFactory(
new PropertyValueFactory<Account, Integer>("id"));
TableColumn typeCol = new TableColumn("Account Type");
typeCol.setMinWidth(100);
typeCol.setCellValueFactory(
new PropertyValueFactory<Account, String>("type"));
TableColumn balanceCol = new TableColumn("Balance");
balanceCol.setMinWidth(200);
balanceCol.setCellValueFactory(
new PropertyValueFactory<Account, Float>("balance"));
tableAccounts.setItems(accountList);
tableAccounts.getColumns().addAll(idCol, typeCol, balanceCol);
hbox_accounts.getChildren().add(tableAccounts);
hbox_accounts.setAlignment(Pos.CENTER);
return hbox_accounts;
}
Which is fine - it creates table. However, table does not have any data in it (however, I can click on first 4 rows - because I have 4 entires in my array).Any ideas why data is not visible?
I found out that I had to add getters and setters to my Account class.
Case solved!