Using the code below I wanted to set low values for the row height of a JavaFx table. But to me there seems to be a limit of about 25, higher values will show up correctly, but not below about 25.
I could use setFixedCellSize(10) to set all rows of a table. I would be interested in doing it for individual rows, so I think this should be done in TableCell, right?
Is there a way to get like a row height of 15?
import java.util.stream.IntStream;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;
public class TableCellTest extends Application {
public static void main(String[] args) {
launch(args);
}
#Override public void start(Stage stage) throws Exception {
int numCols = 20;
int numRows = 20;
ObservableList<Integer> rows = FXCollections.observableList(IntStream.rangeClosed(1, numRows).boxed().toList());
TableView<Integer> tv = new TableView<>(rows);
for (int i = 0; i < numCols; i++) {
TableColumn<Integer, String> col = new TableColumn<>();
col.setCellFactory(ts -> new TableCell<>() {
{
setPrefHeight(10); //does not apply values below about 25
}
});
tv.getColumns().add(col);
}
Scene scene = new Scene(tv, 900, 600);
stage.setScene(scene);
stage.show();
}
}
Version 2:
Here I tried to use set prefHeight of a TableRow for selected rows, this sort of is the answer to my original question, but side effect is there is now empty space below the table, and those empty rows below the actual TableView even change their height if I drag columns to reorder them. I cannot access those rows as they are outside the range of the TableView. Not a satisfying situation.
import java.util.stream.IntStream;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;
public class TableCellTest3 extends Application {
public static void main(String[] args) {
launch(args);
}
#Override public void start(Stage stage) throws Exception {
int numCols = 20;
int numRows = 20;
ObservableList<Integer> rows = FXCollections.observableList(IntStream.rangeClosed(1, numRows).boxed().toList());
TableView<Integer> tv = new TableView<>(rows);
for (int i = 0; i < numCols; i++) {
TableColumn<Integer, String> col = new TableColumn<>();
int ii = i;
col.setCellValueFactory(cdf -> new SimpleStringProperty(String.valueOf(ii)));
col.setCellFactory(ts -> new TableCell<>() {
#Override public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
} else {
TableRow<Integer> row = getTableRow();
if (row != null) {
if (row.getItem() != null && row.getItem().intValue() < 10) {
row.setPrefHeight(15); //prefHeight of a row can be set low enough
} else {
row.setPrefHeight(25);
};
}
setText(item);
}
}
});
tv.getColumns().add(col);
}
Scene scene = new Scene(tv, 900, 600);
stage.setScene(scene);
stage.show();
}
}
Version 3
moving height setting code into TableRow's updateItem function instead of TableCell's updateItem function also produces empty space below the table, although this time those rows do not change their height when columns are dragged around.
Seems that whatever way, setting the prefHeight of a TableRow screws up the evaluation of how many filler rows are needed at the bottom of a table? Should this value really be dealt with at 'user level' ?
import java.util.stream.IntStream;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;
public class TableCellTest4 extends Application {
public static void main(String[] args) {
launch(args);
}
#Override public void start(Stage stage) throws Exception {
int numCols = 20;
int numRows = 20;
ObservableList<Integer> rows = FXCollections.observableList(IntStream.rangeClosed(1, numRows).boxed().toList());
TableView<Integer> tv = new TableView<>(rows);
for (int i = 0; i < numCols; i++) {
TableColumn<Integer, String> col = new TableColumn<>();
int ii = i;
col.setCellValueFactory(cdf -> new SimpleStringProperty(String.valueOf(ii)));
tv.getColumns().add(col);
}
tv.setRowFactory(tview -> new TableRow<Integer>() {
#Override public void updateItem(Integer item, boolean empty) {
super.updateItem(item, empty);
if (item != null && item < 10) {
setPrefHeight(15);
} else {
setPrefHeight(25);
}
}
});
Scene scene = new Scene(tv, 900, 600);
stage.setScene(scene);
stage.show();
}
}
Related
In a JavaFx/TableView, is it possible to get a TableCell with multiple colors for the text ? I've tried the following code using 'Text' where each character would be in RED/BLUE/RED/BLUE... but the cells remain black.
(...)
tableColumn.setCellFactory(tc -> new TableCell<MyObject, String>() {
#Override
protected void updateItem(final String item, boolean empty) {
super.updateItem(item, empty);
if(item==null) return;
this.setText(item);
final List<Text> L=new ArrayList<>(item.length());
for(int i=0;i< item.length();++i) {
final Text txt=new Text(String.valueOf(item.charAt(i)));
txt.setStroke(i%2==0?Color.RED:Color.BLUE);
L.add(txt);
}
this.getChildren().setAll(L);
}
});
(...)
Is there any way to achieve this ? Thanks.
Create a TextFlow to hold the Text instances and set it as the cell's graphic. Note also that you have a bug (that will become apparent if you remove items from the table's list, or possibly if you scroll): you need to clear the text and graphic if the cell is empty.
tableColumn.setCellFactory(tc -> new TableCell<MyObject, String>() {
final TextFlow textFlow = new TextFlow();
#Override
protected void updateItem(final String item, boolean empty) {
super.updateItem(item, empty);
if(item==null) {
setText(null);
setGraphic(null);
return ;
}
this.setText(item);
final List<Text> L=new ArrayList<>(item.length());
for(int i=0;i< item.length();++i) {
final Text txt=new Text(String.valueOf(item.charAt(i)));
txt.setStroke(i%2==0?Color.RED:Color.BLUE);
L.add(txt);
}
textFlow.getChildren().setAll(L);
setGraphic(textFlow);
}
});
Here's a SSCCE:
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;
public class TableWithAlternateColorText extends Application {
private final Random rng = new Random();
private final String[] bases = "ACGT".split("") ;
#Override
public void start(Stage primaryStage) {
TableView<StringProperty> table = new TableView<>();
TableColumn<StringProperty, String> col = new TableColumn<>("Sequence");
col.setCellValueFactory(CellDataFeatures::getValue);
col.setCellFactory(tc -> new TableCell<StringProperty, String>() {
final TextFlow textFlow = new TextFlow();
#Override
protected void updateItem(final String item, boolean empty) {
super.updateItem(item, empty);
if(item==null) {
setText(null);
setGraphic(null);
return ;
}
List<Text> texts = new ArrayList<>();
for(int i=0;i< item.length();++i) {
char base = item.charAt(i);
final Text txt=new Text(String.valueOf(base));
txt.setStroke(isPyrimidine(base) ? Color.RED : Color.BLUE);
texts.add(txt);
}
textFlow.getChildren().setAll(texts);
setGraphic(textFlow);
setPrefHeight(textFlow.prefHeight(-1));
}
});
table.getColumns().add(col);
for (int i = 0 ; i < 100 ; i++) {
table.getItems().add(new SimpleStringProperty(randomSequence(20)));
}
primaryStage.setScene(new Scene(table, 600, 600));
primaryStage.show();
}
private boolean isPyrimidine(char base) {
return base == 'C' || base == 'T' ;
}
private String randomSequence(int seqLength) {
return rng.ints(seqLength, 0, bases.length)
.mapToObj(i -> bases[i])
.collect(Collectors.joining());
}
public static void main(String[] args) {
launch(args);
}
}
I have a int value which I want to use for configuration. It can have 2 values - 0 for active and 1 for Blocked. I want to display this into friendly combo box:
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class MainApp extends Application
{
#Override
public void start(Stage stage) throws Exception
{
int state = 0;
ObservableList<String> options = FXCollections.observableArrayList(
"Active",
"Blocked"
);
ComboBox comboBox = new ComboBox(options);
BorderPane bp = new BorderPane(comboBox);
bp.setPrefSize(800, 800);
Scene scene = new Scene(bp);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
It's not clear for me how I have to implement this into JavaFX Combobox.
When I have 0 I want to display this as Active and when I have 1 I want to display Blocked and also when I change the ComboBox value to update also int state value.
There are different ways to solve this problem. I have listed three of the solutions below. You can use any one of the below solutions which you feel is apt for your scenario.
Using a custom class
Create a custom class KeyValuePair, for storing the string and its corresponding value. Exposed the getters for the required fields.
Later, I have used the setCellFactory() of the comboxbox to show the required data. Use StringConverter to show the key in place of the object.
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;
public class Main extends Application {
#Override
public void start(Stage stage) throws Exception
{
KeyValuePair keyValuePair1 = new KeyValuePair("Active", 0);
KeyValuePair keyValuePair2 = new KeyValuePair("Blocked", 1);
ObservableList<KeyValuePair> options = FXCollections.observableArrayList();
options.addAll(keyValuePair1, keyValuePair2);
ComboBox<KeyValuePair> comboBox = new ComboBox<>(options);
// show the correct text
comboBox.setCellFactory((ListView<KeyValuePair> param) -> {
final ListCell<KeyValuePair> cell = new ListCell<KeyValuePair>(){
#Override
protected void updateItem(KeyValuePair t, boolean bln) {
super.updateItem(t, bln);
if(t != null){
setText(String.valueOf(t.getKey()));
}else{
setText(null);
}
}
};
return cell;
});
comboBox.setConverter(new StringConverter<KeyValuePair>() {
#Override
public String toString(KeyValuePair object) {
return object.getKey();
}
#Override
public KeyValuePair fromString(String string) {
return null; // No conversion fromString needed.
}
});
// print the value
comboBox.valueProperty().addListener((ov, oldVal, newVal) -> {
System.out.println(newVal.getKey() + " - " + newVal.getValue());
});
BorderPane bp = new BorderPane(comboBox);
bp.setPrefSize(800, 800);
Scene scene = new Scene(bp);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
class KeyValuePair {
private final String key;
private final int value;
public KeyValuePair(String key, int value) {
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public int getValue() {
return value;
}
}
}
Without using an extra class
As suggested by #kleopatra, you can even do this without using an extra class.
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;
public class Main extends Application {
#Override
public void start(Stage stage) throws Exception {
ObservableList<Integer> options = FXCollections.observableArrayList();
options.addAll(1, 0);
ComboBox<Integer> comboBox = new ComboBox<>(options);
// show the correct text
comboBox.setCellFactory((ListView<Integer> param) -> {
final ListCell<Integer> cell = new ListCell<Integer>(){
#Override
protected void updateItem(Integer t, boolean bln) {
super.updateItem(t, bln);
if(t != null){
setText(t == 1 ? "Active" : "Blocked");
}else{
setText(null);
}
}
};
return cell;
});
comboBox.setConverter(new StringConverter<Integer>() {
#Override
public String toString(Integer object) {
return object == 1 ? "Active" : "Blocked";
}
#Override
public Integer fromString(String string) {
return null;
}
});
// print the value
comboBox.valueProperty().addListener((ov, oldVal, newVal) -> {
System.out.println("Changed from " + oldVal + " to " + newVal);
});
BorderPane bp = new BorderPane(comboBox);
bp.setPrefSize(800, 800);
Scene scene = new Scene(bp);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Using Bindings
You can also use Bindings if you don't want to take the pain of creating a new class and you will always have two elements i.e. Active and Blocked.
Just bind the valueProperty() of your combobox to the state, which is supposed to store the value i.e. 0 or 1.
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage stage) throws Exception {
IntegerProperty state = new SimpleIntegerProperty();
ObservableList options = FXCollections.observableArrayList("Active", "Blocked");
ComboBox<String> comboBox = new ComboBox<>(options);
state.bind(Bindings.when(comboBox.valueProperty().isEqualTo("Active")).then(0).otherwise(1));
BorderPane bp = new BorderPane(comboBox);
bp.setPrefSize(800, 800);
Scene scene = new Scene(bp);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Here is another solution:
declare state as BooleanProperty:
private BooleanProperty state = new SimpleBooleanProperty(false);
bind state property to the valueProperty of comboBox:
comboBox.valueProperty().bind(new When(state).then("Active").otherwise("Blocked"));
complete example:
public class ComboboxTest extends Application {
private BooleanProperty state = new SimpleBooleanProperty(false);
private Button button;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
ObservableList<String> options = FXCollections.observableArrayList(
"Active",
"Blocked"
);
ComboBox comboBox = new ComboBox(options);
button = new Button("false");
button.setOnAction(e -> setSate());
button.textProperty().bind(state.asString());
BorderPane bp = new BorderPane(comboBox);
StackPane stackpane = new StackPane(button);
stackpane.setAlignment(Pos.CENTER);
bp.setTop(stackpane);
bp.setPrefSize(800, 800);
Scene scene = new Scene(bp);
stage.setScene(scene);
stage.show();
comboBox.valueProperty().bind(new When(state).then("Active").otherwise("Blocked"));
}
public void setSate() {
if (state.get()) {
state.set(false);
} else {
state.set(true);
}
}
}
javaFX: TableView's cellvalue is not enough to display in columns, it will be clipped end of '...', how to show a tip on such cell?
I need a tip because sometimes i can't drag the colunm's head. So i can't see the whole content of the cell.
Below a subclass of TableCell is used to set the ToolTip for a cell. It does not distinguish between cells in which the text fits and those where the text is too large. It is used in the cell factory for the table column.
import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Callback;
public class SO extends Application {
static class XCell extends TableCell<String, String> {
#Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
this.setText(item);
this.setTooltip(
(empty || item==null) ? null : new Tooltip(item));
}
}
#Override
public void start(Stage primaryStage) throws Exception {
StackPane pane = new StackPane();
Scene scene = new Scene(pane, 300, 100);
primaryStage.setScene(scene);
ObservableList<String> list = FXCollections.observableArrayList(
"Short",
"This is quite long - almost very long");
TableView<String> tv = new TableView<>(list);
TableColumn<String, String> col = new TableColumn<>("Column");
col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<String,String>, ObservableValue<String>>() {
#Override
public ObservableValue<String> call(CellDataFeatures<String, String> param) {
return new ReadOnlyStringWrapper(param.getValue());
}
});
col.setMaxWidth(50);
col.setCellFactory(new Callback<TableColumn<String,String>, TableCell<String,String>>() {
#Override
public TableCell<String, String> call(TableColumn<String, String> param) {
return new XCell();
}
});
tv.getColumns().add(col);
pane.getChildren().add(tv);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The result will look like this:
This should ACTUALLY solve your problem.
column.setCellFactory(col -> new TableCell<Object, String>()
{
#Override
protected void updateItem(final String item, final boolean empty)
{
super.updateItem(item, empty);
setText(item);
TableColumn tableCol = (TableColumn) col;
if (item != null && tableCol.getWidth() < new Text(item + " ").getLayoutBounds().getWidth())
{
tooltipProperty().bind(Bindings.when(Bindings.or(emptyProperty(), itemProperty().isNull())).then((Tooltip) null).otherwise(new Tooltip(item)));
} else
{
tooltipProperty().bind(Bindings.when(Bindings.or(emptyProperty(), itemProperty().isNull())).then((Tooltip) null).otherwise((Tooltip) null));
}
}
});
I have a int value which I want to use for configuration. It can have 2 values - 0 for active and 1 for Blocked. I want to display this into friendly combo box:
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class MainApp extends Application
{
#Override
public void start(Stage stage) throws Exception
{
int state = 0;
ObservableList<String> options = FXCollections.observableArrayList(
"Active",
"Blocked"
);
ComboBox comboBox = new ComboBox(options);
BorderPane bp = new BorderPane(comboBox);
bp.setPrefSize(800, 800);
Scene scene = new Scene(bp);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
It's not clear for me how I have to implement this into JavaFX Combobox.
When I have 0 I want to display this as Active and when I have 1 I want to display Blocked and also when I change the ComboBox value to update also int state value.
There are different ways to solve this problem. I have listed three of the solutions below. You can use any one of the below solutions which you feel is apt for your scenario.
Using a custom class
Create a custom class KeyValuePair, for storing the string and its corresponding value. Exposed the getters for the required fields.
Later, I have used the setCellFactory() of the comboxbox to show the required data. Use StringConverter to show the key in place of the object.
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;
public class Main extends Application {
#Override
public void start(Stage stage) throws Exception
{
KeyValuePair keyValuePair1 = new KeyValuePair("Active", 0);
KeyValuePair keyValuePair2 = new KeyValuePair("Blocked", 1);
ObservableList<KeyValuePair> options = FXCollections.observableArrayList();
options.addAll(keyValuePair1, keyValuePair2);
ComboBox<KeyValuePair> comboBox = new ComboBox<>(options);
// show the correct text
comboBox.setCellFactory((ListView<KeyValuePair> param) -> {
final ListCell<KeyValuePair> cell = new ListCell<KeyValuePair>(){
#Override
protected void updateItem(KeyValuePair t, boolean bln) {
super.updateItem(t, bln);
if(t != null){
setText(String.valueOf(t.getKey()));
}else{
setText(null);
}
}
};
return cell;
});
comboBox.setConverter(new StringConverter<KeyValuePair>() {
#Override
public String toString(KeyValuePair object) {
return object.getKey();
}
#Override
public KeyValuePair fromString(String string) {
return null; // No conversion fromString needed.
}
});
// print the value
comboBox.valueProperty().addListener((ov, oldVal, newVal) -> {
System.out.println(newVal.getKey() + " - " + newVal.getValue());
});
BorderPane bp = new BorderPane(comboBox);
bp.setPrefSize(800, 800);
Scene scene = new Scene(bp);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
class KeyValuePair {
private final String key;
private final int value;
public KeyValuePair(String key, int value) {
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public int getValue() {
return value;
}
}
}
Without using an extra class
As suggested by #kleopatra, you can even do this without using an extra class.
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;
public class Main extends Application {
#Override
public void start(Stage stage) throws Exception {
ObservableList<Integer> options = FXCollections.observableArrayList();
options.addAll(1, 0);
ComboBox<Integer> comboBox = new ComboBox<>(options);
// show the correct text
comboBox.setCellFactory((ListView<Integer> param) -> {
final ListCell<Integer> cell = new ListCell<Integer>(){
#Override
protected void updateItem(Integer t, boolean bln) {
super.updateItem(t, bln);
if(t != null){
setText(t == 1 ? "Active" : "Blocked");
}else{
setText(null);
}
}
};
return cell;
});
comboBox.setConverter(new StringConverter<Integer>() {
#Override
public String toString(Integer object) {
return object == 1 ? "Active" : "Blocked";
}
#Override
public Integer fromString(String string) {
return null;
}
});
// print the value
comboBox.valueProperty().addListener((ov, oldVal, newVal) -> {
System.out.println("Changed from " + oldVal + " to " + newVal);
});
BorderPane bp = new BorderPane(comboBox);
bp.setPrefSize(800, 800);
Scene scene = new Scene(bp);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Using Bindings
You can also use Bindings if you don't want to take the pain of creating a new class and you will always have two elements i.e. Active and Blocked.
Just bind the valueProperty() of your combobox to the state, which is supposed to store the value i.e. 0 or 1.
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage stage) throws Exception {
IntegerProperty state = new SimpleIntegerProperty();
ObservableList options = FXCollections.observableArrayList("Active", "Blocked");
ComboBox<String> comboBox = new ComboBox<>(options);
state.bind(Bindings.when(comboBox.valueProperty().isEqualTo("Active")).then(0).otherwise(1));
BorderPane bp = new BorderPane(comboBox);
bp.setPrefSize(800, 800);
Scene scene = new Scene(bp);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Here is another solution:
declare state as BooleanProperty:
private BooleanProperty state = new SimpleBooleanProperty(false);
bind state property to the valueProperty of comboBox:
comboBox.valueProperty().bind(new When(state).then("Active").otherwise("Blocked"));
complete example:
public class ComboboxTest extends Application {
private BooleanProperty state = new SimpleBooleanProperty(false);
private Button button;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
ObservableList<String> options = FXCollections.observableArrayList(
"Active",
"Blocked"
);
ComboBox comboBox = new ComboBox(options);
button = new Button("false");
button.setOnAction(e -> setSate());
button.textProperty().bind(state.asString());
BorderPane bp = new BorderPane(comboBox);
StackPane stackpane = new StackPane(button);
stackpane.setAlignment(Pos.CENTER);
bp.setTop(stackpane);
bp.setPrefSize(800, 800);
Scene scene = new Scene(bp);
stage.setScene(scene);
stage.show();
comboBox.valueProperty().bind(new When(state).then("Active").otherwise("Blocked"));
}
public void setSate() {
if (state.get()) {
state.set(false);
} else {
state.set(true);
}
}
}
javaFX: TableView's cellvalue is not enough to display in columns, it will be clipped end of '...', how to show a tip on such cell?
I need a tip because sometimes i can't drag the colunm's head. So i can't see the whole content of the cell.
Below a subclass of TableCell is used to set the ToolTip for a cell. It does not distinguish between cells in which the text fits and those where the text is too large. It is used in the cell factory for the table column.
import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Callback;
public class SO extends Application {
static class XCell extends TableCell<String, String> {
#Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
this.setText(item);
this.setTooltip(
(empty || item==null) ? null : new Tooltip(item));
}
}
#Override
public void start(Stage primaryStage) throws Exception {
StackPane pane = new StackPane();
Scene scene = new Scene(pane, 300, 100);
primaryStage.setScene(scene);
ObservableList<String> list = FXCollections.observableArrayList(
"Short",
"This is quite long - almost very long");
TableView<String> tv = new TableView<>(list);
TableColumn<String, String> col = new TableColumn<>("Column");
col.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<String,String>, ObservableValue<String>>() {
#Override
public ObservableValue<String> call(CellDataFeatures<String, String> param) {
return new ReadOnlyStringWrapper(param.getValue());
}
});
col.setMaxWidth(50);
col.setCellFactory(new Callback<TableColumn<String,String>, TableCell<String,String>>() {
#Override
public TableCell<String, String> call(TableColumn<String, String> param) {
return new XCell();
}
});
tv.getColumns().add(col);
pane.getChildren().add(tv);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The result will look like this:
This should ACTUALLY solve your problem.
column.setCellFactory(col -> new TableCell<Object, String>()
{
#Override
protected void updateItem(final String item, final boolean empty)
{
super.updateItem(item, empty);
setText(item);
TableColumn tableCol = (TableColumn) col;
if (item != null && tableCol.getWidth() < new Text(item + " ").getLayoutBounds().getWidth())
{
tooltipProperty().bind(Bindings.when(Bindings.or(emptyProperty(), itemProperty().isNull())).then((Tooltip) null).otherwise(new Tooltip(item)));
} else
{
tooltipProperty().bind(Bindings.when(Bindings.or(emptyProperty(), itemProperty().isNull())).then((Tooltip) null).otherwise((Tooltip) null));
}
}
});