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!
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"));
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 created a class Cart and inside is a JTable and two ArrayLists. For some reason, my JTable is not displaying.
Here is my Cart Class:
class Cart {
ArrayList<Product> products = new ArrayList<>(); // Holds the products themselves
ArrayList<Integer> quantities = new ArrayList<>(); // Holds the quantities themselves
JTable prdTbl = new JTable(); // The GUI Product Table
DefaultTableModel prdTblModel = new DefaultTableModel(); // The Table Model
Object[] columns = {"Description","Price","Quantity","Total"}; // Column Identifiers
DecimalFormat fmt = new DecimalFormat("$#,##0.00;$-#,##0.00"); // Decimal Format for formatting USD ($#.##)
Cart() {
setTableStyle();
}
void renderTable() {
// Re-initialize the Table Model
this.prdTblModel = new DefaultTableModel();
// Set the Table Style
setTableStyle();
// Create a row from each list entry for product and quantity and add it to the Table Model
for(int i = 0; i < products.size(); i++) {
Object[] row = new Object[4];
row[0] = products.get(i).getName();
row[1] = products.get(i).getPrice();
row[2] = quantities.get(i);
row[3] = fmt.format(products.get(i).getPrice() * quantities.get(i));
this.prdTblModel.addRow(row);
}
this.prdTbl.setModel(this.prdTblModel);
}
void setTableStyle() {
this.prdTblModel.setColumnIdentifiers(columns);
this.prdTbl.setModel(this.prdTblModel);
this.prdTbl.setBackground(Color.white);
this.prdTbl.setForeground(Color.black);
Font font = new Font("Tahoma",1,22);
this.prdTbl.setFont(font);
this.prdTbl.setRowHeight(30);
}
JTable getTable() {
renderTable(); // Render Table
return this.prdTbl;
}
}
Note: some methods have been removed such as addProduct() and removeProduct(), as I feel they aren't necessary. If you need to see them, please ask.
Here is my initialize() method for the Swing Application Window:
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 590, 425);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Cart cart = new Cart();
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
frame.getContentPane().add(tabbedPane, "cell 0 0,grow");
JPanel cartPanel = new JPanel();
tabbedPane.addTab("Cart", null, cartPanel, null);
cartPanel.setLayout(new MigLayout("", "[grow]", "[][grow]"));
JScrollPane scrollPane = new JScrollPane();
cartPanel.add(scrollPane, "cell 0 1,grow");
table = new JTable();
scrollPane.setViewportView(table);
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String[] item = {"Macadamia", "Hazelnut", "Almond", "Peanut", "Walnut", "Pistachio", "Pecan", "Brazil"};
Double[] price = {2.00, 1.90, 1.31, 0.85, 1.12, 1.53, 1.25, 1.75};
int choice = (int) (Math.random() * item.length);
Product p = new Product(item[choice], price[choice]);
cart.addProduct(p);
table = cart.getTable();
}
});
cartPanel.add(btnAdd, "flowx,cell 0 0");
JButton btnRemove = new JButton("Remove");
cartPanel.add(btnRemove, "cell 0 0");
JButton btnClear = new JButton("Clear");
cartPanel.add(btnClear, "cell 0 0");
}
I'm not sure if I'm missing something here? It has worked fine like this in the past? I've also tried printing out values at table = cart.getTable();, and it seems to be receiving the values fine, so it leads me to believe it has something to do with the Swing initialize() rather than my Cart class, but just in case I posted the Cart class as well.
Are you sure you're adding the right table? Your code shows:
table = new JTable();
scrollPane.setViewportView(table);
I cannot see where's table declared, further more table contains nothing, no rows no columns, while inside actionListener you initialize table with a new instance:
table = cart.getTable();
but the scrollPane holds another instance of JTable.
It looks like you never associate your cart with your cartPanel; I think your problem is here:
JPanel cartPanel = new JPanel();
you make the new panel but never hook your cart to it. Looks good otherwise.
Good luck!
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
Could anybody help me?
I have a table in SWT and simply I want to show some table items but the table only shows one item and the others with vertical scroll. I want to show everything, without scrolling. I tried with the option SWT_NO_SCROLL but is not working. I have a method that creates the table and other that populates it creating a new table item, they´re working good, the problem is that only shows the first item and the other are scrolling.
My code is:
private void createTable(Composite parent){
table = new Table (parent, SWT.BORDER);
TableColumn tcFile = new TableColumn(table, SWT.LEFT);
TableColumn tcStatus = new TableColumn(table, SWT.LEFT);
tcFile.setText("File");
tcStatus.setText("Status");
tcFile.setWidth(500);
tcStatus.setWidth(500);
table.setVisible(true);
table.setHeaderVisible(true);
}
private void populateTable(String file, String status){
TableItem item = new TableItem(table, SWT.LEFT);
item.setText(new String[] { file, status});
}
Composite top = new Composite(parent, SWT.WRAP);
GridLayout layout = new GridLayout();
layout.marginHeight = -5;
layout.marginWidth = 0;
top.setLayout(layout);
Composite banner = new Composite(top, SWT.WRAP);
banner.setLayoutData(new GridData(GridData.FILL, GridData.VERTICAL_ALIGN_BEGINNING, false, false));
layout = new GridLayout();
layout.marginHeight = 0;
layout.marginWidth = 10;
layout.numColumns = 5;
banner.setLayout(layout);
createTable(top);
You haven't specified any layout data for the Table, try something like:
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
table.setLayoutData(data);
If you don't have anything else that is setting the dialog/window size you may need to specify a table height hint:
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
data.heightHint = 200; // Vertical size for table
table.setLayoutData(data);