This question already has answers here:
JavaFX print tableview on multiple pages
(3 answers)
Print contents of JavaFx TableView
(4 answers)
Closed 5 years ago.
I need help rating printing. in fact I would like to print all the contents of my tableview but the following code that I made prints that the data that appeared on the screen the others below do not appear.
printTestButton.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e){
Printer printer = Printer.getDefaultPrinter();
Stage dialogStage = new Stage(StageStyle.DECORATED);
PrinterJob job = PrinterJob.createPrinterJob(printer);
if (job != null) {
boolean showDialog =job.showPrintDialog(printTestButton.getScene().getWindow());
// boolean showDialog = job.showPageSetupDialog(dialogStage);
if (showDialog) {
tableview.setScaleX(0.50);
tableview.setScaleY(0.60);
tableview.setTranslateX(-210);
tableview.setTranslateY(-80);
boolean success = job.printPage(tableview);
if (success) {
job.endJob();
}
tableview.setTranslateX(0);
tableview.setTranslateY(0);
tableview.setScaleX(1.0);
tableview.setScaleY(1.0);
}
}
}
});
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 10 days ago.
Improve this question
I have TableView and my purpose is to have just one row at all [not included the col's title for each col]. And in every cell in the first row. I'll have ProgressIndicator.
Now, my purpose is everytime someone presses 'Add' it'll go to the OnAddButtonPressed function action and will add new col and in the first row in this specific col was just added a new progressIndicator will be added.
In my code right now, what is happening is everytime someone presses on 'ADD' what happens is all the ProgressIndicator were from earlier are moving to the new col and the num of rows gets incremented and all of the ProgressIndicator from the earalier columns gets removed and being "pushed" one by one to the new col.
#FXML
private void OnAddButtonPressed(ActionEvent event) {
int currentCPUTimeRequestedInteger;
String currentCPUTimeRequested = this.addTextField.getText();
if(currentCPUTimeRequested.isEmpty() || currentCPUTimeRequested.length() > 1 || !Character.isDigit(currentCPUTimeRequested.toCharArray()[0]) || currentCPUTimeRequested.toCharArray()[0] == '0')
{
PopAddButtonError();
return;
}
if(m_NumOfCols == MAX_NUM_OF_COLS - 1)
{
this.addButton.setDisable(true);
this.addTextField.setDisable(true);
this.enterCPUTimeLabel.setDisable(true);
}
if(firstTimePressedAdd == false)
{
this.firstTimePressedAdd = true;
this.startButton.setDisable(false);
this.listOfProcesses = new LinkedList<Process>();
}
currentCPUTimeRequestedInteger = Integer.parseInt(currentCPUTimeRequested);
this.addTextField.setText("");
AddNewColToTable(currentCPUTimeRequestedInteger);
}
private void AddNewColToTable(int CPUTimeRequested)
{
Process newProcess = new Process(CPUTimeRequested);
this.listOfProcesses.add(newProcess);
TableColumn newCol = new TableColumn("P" + newProcess.GetId());
newCol.setCellValueFactory(new PropertyValueFactory<Process, String>("progressIndicator"));
ObservableList<Process> data = FXCollections.observableArrayList();
data.add(newProcess);
this.tableOfProcesses.getColumns().add(newCol);
this.tableOfProcesses.getItems().addAll(data);
this.tableOfProcesses.refresh();
this.m_NumOfCols++;
}
My purpose is to have it the next way like in this photo:
enter image description here
Thank you for your help!
Treating this as an XY problem.
Here's how the sample image can mostly be achieved via an HBox:
class Progresses : Application() {
private var counter: Int = 0
override fun start(primaryStage: Stage) {
primaryStage.scene = Scene(createContent())
primaryStage.show()
}
private fun createContent(): Region = BorderPane().apply {
val hBox = HBox(20.0)
bottom = createButton(hBox)
center = hBox
padding = Insets(20.0)
minWidth = 400.0
minHeight = 150.0
}
private fun createButton(targetPane: Pane) = buttonOf("New Progress") { buttonAction(targetPane) }
private fun buttonAction(targetPane: Pane) {
targetPane.children += Label("P${counter++}").apply {
contentDisplay = ContentDisplay.BOTTOM
graphic = ProgressIndicator().apply {
progress = -1.0
object : AnimationTimer() {
var count = 0
override fun handle(now: Long) {
progress = (count++ / 5000.0).toDouble()
}
}.start()
}
}
}
}
fun main() = Application.launch(Progresses::class.java)
It's Kotlin, but you should be able to figure it out. It looks like this:
If the column heading styling is really required, you can style the Labels to give the same look and feel, or divide it into a VBox if that makes it easier.
I'm using the Jfoenix libary, and I have gotten it to show both the day of the year, and day of the month (after asking here for the first hint). They are both inside the setText line. I'm trying to figure out if I can add CSS to them individually so I can make the Day of the Year appear smaller, in the right corner and maybe a different color. I've googled quite a bit but not getting the answers I'm looking for. Thank you.
endDate.setDayCellFactory(p -> new DateCell() {
#Override
public void updateItem(LocalDate item, boolean empty) {
super.updateItem(item, empty);
if (item == null) {
setText("");
} else {
setText(Integer.toString(item.getDayOfYear()) + "\r\n" + Integer.toString(item.getDayOfMonth()));
}
}
});
You cannot style the text in the same Labeled differently, but you can use the graphic property instead to display the date:
DatePicker datePicker = new DatePicker();
datePicker.setDayCellFactory(p -> new DateCell() {
private final Text doy = new Text();
private final Text dom = new Text();
private final VBox graphic = new VBox(doy, dom);
{
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
doy.setFont(Font.font(8));
dom.setFont(Font.font(15));
VBox.setMargin(dom, new Insets(0, 0, 0, 10));
}
#Override
public void updateItem(LocalDate item, boolean empty) {
super.updateItem(item, empty);
if (item == null) {
setGraphic(null);
} else {
setGraphic(graphic);
doy.setText(Integer.toString(item.getDayOfYear()));
dom.setText(Integer.toString(item.getDayOfMonth()));
}
}
});
BTW: For java internal string handling usually \r is not required. This is only necessary, if you write the string to a file or use a class that depends on the line separator of the OS; only \n works just fine for the purpose you're using it for here. If you need to use different line separation on different OS, hardcoding \r\n is not a good idea; use System.lineSeparator() instead.
I have a TableView with a ComboBoxTableCell, when using the default implementation the user have to click three times to select a value from of the ComboBox's list.
I want when the user clicks on the cell to show the combo box list. I based my solution on this one:
JavaFX editable ComboBox in a table view
The cell does get into edit mode (startEdit() is called) but it takes another click to show the list of values, what am I missing?
table.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) ->
{
if (table.getEditingCell() == null)
{
TablePosition focusedCellPos = table.getFocusModel().getFocusedCell();
table.edit(focusedCellPos.getRow(), focusedCellPos.getTableColumn());
}
});
Thanks.
Interesting problem - bubbling up again after quite a while :)
Looks like the approach of the OP is indeed working (as of fx11, some bugs around its editing seem to be fixed) - with a little help from the combo cell:
start editing in a single click handler on the tableView (from OP)
extend ComboBoxTableCell and override its startEdit to open the dropDown
Code snippet:
// set editable to see the combo
table.setEditable(true);
// keep approach by OP
table.addEventHandler(MouseEvent.MOUSE_CLICKED, (e) -> {
TablePosition<Person, ?> focusedCellPos = table.getFocusModel()
.getFocusedCell();
if (table.getEditingCell() == null) {
table.edit(focusedCellPos.getRow(),
focusedCellPos.getTableColumn());
}
});
// use modified standard combo cell shows its popup on startEdit
firstName.setCellFactory(cb -> new ComboBoxTableCell<>(firstNames) {
#Override
public void startEdit() {
super.startEdit();
if (isEditing() && getGraphic() instanceof ComboBox) {
// needs focus for proper working of esc/enter
getGraphic().requestFocus();
((ComboBox<?>) getGraphic()).show();
}
}
});
Maybe not the cleanest solution to this problem, but I found a workaround to make the ComboBoxTableCells drop down its menu in just 1 click:
column.setCellFactory(new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
#Override
public TableCell<Person, String> call(TableColumn<Person, String> column) {
ComboBoxTableCell cbtCell = new ComboBoxTableCell<>(cbValues);
cbtCell.setOnMouseEntered(new EventHandler<Event>() {
#Override
public void handle(Event event) {
// Without a Person object, a combobox shouldn't open in that row
if (((Person)((TableRow)cbtCell.getParent()).getItem()) != null) {
Robot r = new Robot();
r.mouseClick(MouseButton.PRIMARY);
r.mouseClick(MouseButton.PRIMARY);
}
}
});
return cbtCell;
}
});
PS: I know that this topic is a bit old, but I also stumbled upon this problem recently and could not find any working solution to it online. As I sad, it's not the cleanest workaround, but at least it does its job. ;)
This question already has answers here:
Casting variables in Java
(5 answers)
Closed 5 years ago.
I have to get an ImageView from a GridPane.
I use this code, but it is for Node, while I'm trying to change an Image in an ImageView (inside GridPane).
private Node getNodeFromGridPane(GridPane gridPane, int col, int row) {
for (Node node : gridPane.getChildren()) {
if (GridPane.getColumnIndex(node) == col && GridPane.getRowIndex(node) == row) {
System.out.println(node);
return node;
}
}
return null;
}
Is there a solution?
I already searched to lots of posts, but there is not this problem solved.
Thank you
On caller side you may do the next:
final Node foundNode = getNodeFromGridPane(gridPane, col, row);
if (foundNode instanceof ImageView) {
//do something
}
instanceof will care for both ImageView and null.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
JavaFX show image near string in listview [closed]
(1 answer)
Closed 6 years ago.
#FXML
private TableColumn<Backup, Image>columnSucces;
columnSuccess.setCellValueFactory(new PropertyValueFactory<Backup, Image>("image"));
columnSucces.setCellFactory(new Callback<TableColumn<Backup, Image>, TableCell<Backup,Image>>(){
#Override
public TableCell<Backup, Image> call(TableColumn<Backup, Image> arg0) {
final ImageView imageview = new ImageView();
imageview.setFitHeight(50);
imageview.setFitWidth(50);
//Set up the Table
TableCell<Backup, Image> cell = new TableCell<Backup, Image>() {
public void updateItem(Backup item, boolean empty) {
if (item != null) {
imageview.setImage(new Image("x-mark-small.png"));
}
}
};
cell.setGraphic(imageView);
return cell;
}
});
I got nullpointerexception error when i am trying to add an image into a column for a table view. I cant understand where is my problem.
#edit. I have solved nullpointerexception, but now my image does not appear in the table column.
I received an warning that my updateItem method is never used.