How do i change the JavaFX TableCell color when it is selected? - colors

Well, I created a custom TableCell for my TableView.
This custom TableCell contains a Link and opens the browser when clicked.
Everything is working fine, what I want to do is change the text color of this TableCell when it is selected...
This is what I am trying to do:
callback = new Callback<TableColumn, TableCell>(){
#Override
public TableCell call(TableColumn param) {
return new TableCell<Test, String>(){
EventHandler handler = new EventHandler<MouseEvent>() {
final AM_RSS_FX RSS = AM_RSS_FX.this;
#Override
public void handle(MouseEvent param) {
try {
java.awt.Desktop.getDesktop().browse(new URI(RSS.link));
} catch (IOException | URISyntaxException ex) {
Logger.getLogger(AM_RSS_FX.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
#Override
public void updateItem(String item, boolean empty){
super.updateItem(item, empty);
if(!isEmpty()){
final AM_RSS_FX RSS = AM_RSS_FX.this;
this.setTextFill(Color.BLUE);
setText(item);
RSS.link = this.getText();
this.addEventHandler(MouseEvent.MOUSE_CLICKED, handler);
}
}
#Override
public void updateSelected(boolean arg0){
super.updateSelected(arg0);
if(isSelected()){
this.setTextFill(Color.AQUA);
}
}
};
}
};
I don't know which method i need to Override =/
I tried to Override the updateSelected, but didn't worked =/
Can someone help me?

1- You are adding a mouse event handler on TableCell instance and that event is firing when you click on it. However the table cell is still not being selected. Instead the table row cell selection is being fired. To enable cell selection do:
table.getSelectionModel().setCellSelectionEnabled(true);
2- No need to override updateSelected() to manage style, instead use CSS selectors from caspian.css:
.table-cell:selected {
-fx-background-color: lightgreen;
-fx-text-fill: green;
}

Related

Cannot properly set visible component

I'm trying to implement Menu with select box which sets to display or not component. I have this checkbox:
final CheckMenuItem toolbarSubMenuNavigation = new CheckMenuItem("Navigation");
toolbarSubMenuNavigation.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
DataTabs.renderTab = toolbarSubMenuNavigation.isSelected();
// call here the getter setter and send boolean flag
System.out.println("subsystem1 #1 Enabled!");
}
});
And I have this tabpane which I want to render only if I have selected the checkbox:
public static boolean renderTab;
public DataTabs()
{
}
public boolean isRenderTab()
{
return renderTab;
}
public void setRenderTab(boolean renderTab)
{
this.renderTab = renderTab;
}
// below this code
tabPane.setVisible(renderTab);
When I run the code it's not working. I also tested this:
DataTabs tabs = new DataTabs(); // instantiate first
tabs.setRenderTab(toolbarSubMenuNavigation.isSelected());
public static boolean renderTab;
TabPane tabPane = new TabPane();
public DataTabs()
{
}
public boolean isRenderTab()
{
return renderTab;
}
public void setRenderTab(boolean renderTab)
{
tabPane.setVisible(renderTab);
}
But again there is no result when I run the code and I check or uncheck the checkbox.
This is the complete source code:
http://pastebin.com/tkj4Fby1
Maybe I need to add listener or something else which I'm missing?
EDIT
Test 3
I also tested this code:
final CheckMenuItem toolbarSubMenuNavigation = new CheckMenuItem("Navigation");
toolbarSubMenuNavigation.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
DataTabs.toolbarSubMenuNavigation = toolbarSubMenuNavigation;
// call here the getter setter and send boolean flag
System.out.println("subsystem1 #1 Enabled!");
}
});
// class with tabs
public static CheckMenuItem toolbarSubMenuNavigation;
public static CheckMenuItem getToolbarSubMenuNavigation()
{
return toolbarSubMenuNavigation;
}
public static void setToolbarSubMenuNavigation(CheckMenuItem toolbarSubMenuNavigation)
{
DataTabs.toolbarSubMenuNavigation = toolbarSubMenuNavigation;
}
// below
abPane.visibleProperty().bind(toolbarSubMenuNavigation.selectedProperty());
I get NPE when I run the code.
You can easely tell to your tab to be visible when you check the box in one line
yourTab.visibleProperty().bind(yourCheckBox.selectedProperty());
And just with this line your tabpane will be visible only when it's checked

How to make checkbox or combobox readonly in JavaFX

How to make checkbox/combobox readonly in javaFX but not disabled.
I tried consuming onAction event but it didn't work.
checkBox.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
event.consume();
}
});
Consuming all events like in code below works but I don't think it's a good solution:
checkBox.addEventFilter(KeyEvent.ANY, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
event.consume();
}
});
checkBox.addEventFilter(MouseEvent.ANY, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEventevent) {
event.consume();
}
});
You can set the check box to disabled but set the the look of it using CSS. If you are using the default style you can make the check box look 'normal' by setting full opacity.
checkbox.setStyle("-fx-opacity: 1");
It is probably a similar deal with the combo box.
You can override method CheckBox#arm() with an empty one:
CheckBox cb = new CheckBox("hi") {
#Override
public void arm() {
// intentionally do nothing
}
};
If you do not want to overwrite the CheckBok class, you can use the selectedProperty.
CheckBox cb = new CheckBox("hi");
cb.selectedProperty().addListener(new NCL());
class NCL implements ChangeListener<Boolean> {
#Override
public void changed(ObservableValue<? extends Boolean> ov, Boolean old_val, Boolean new_val) {
cb.setSelected(false);
}
}

Implement drag-and-drop like in Scene Builder

I'm building an application in JavaFx 2.2 which consist in a splitpane with a panel of component on the left side and a working sheet on the right side. Basically what i would like to do is a simple wysiwyg editor where you drag component from the left to the right, then arrange them on the right side.
I've spent the last couple of days trying to implement the same drag-and-drop feature that has SceneBuilder, without luck..
Following the sample at http://docs.oracle.com/javafx/2/drag_drop/HelloDragAndDrop.java.html i've managed to get a drag-and-drop working but i can't find any way to change the default file icon appearing when you are dragging (and replace it with a snapshot of the component i'm dragging) and how to show the forbidden icon when you are over something you can't dropped on.
Any help (could be advice, code snippet, sample or else) would be greatly appreciated :)
Thanks !
[UPDATE]
Finally managed it myself:
/* The 'sceneRoot' object is the root Node of the scene graph
* stage.setScene(new Scene(sceneRoot, 1280, 1024));
*/
private ImageView dragImageView = new ImageView();
private Node dragItem;
_
rightPane.setOnMouseDragEntered(new EventHandler<MouseDragEvent>() {
public void handle(MouseDragEvent e) {
rightPane.setStyle("-fx-border-color:red;-fx-border-width:2;-fx-border-style:solid;");
e.consume();
}
});
rightPane.setOnMouseDragExited(new EventHandler<MouseDragEvent>() {
public void handle(MouseDragEvent e) {
rightPane.setStyle("-fx-border-style:none;");
e.consume();
}
});
rightPane.setOnMouseDragReleased(new EventHandler<MouseDragEvent>() {
public void handle(MouseDragEvent e) {
//TODO: add new instance of dragItem to rightPane
e.consume();
}
});
_
private void addGesture(final Node node) {
node.setOnDragDetected(new EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
SnapshotParameters snapParams = new SnapshotParameters();
snapParams.setFill(Color.TRANSPARENT);
dragImageView.setImage(node.snapshot(snapParams, null));
sceneRoot.getChildren().add(dragImageView);
dragImageView.startFullDrag();
e.consume();
}
});
node.setOnMouseDragged(new EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
Point2D localPoint = sceneRoot.sceneToLocal(new Point2D(e.getSceneX(), e.getSceneY()));
dragImageView.relocate(
(int)(localPoint.getX() - dragImageView.getBoundsInLocal().getWidth() / 2),
(int)(localPoint.getY() - dragImageView.getBoundsInLocal().getHeight() / 2)
);
e.consume();
}
});
node.setOnMouseEntered(new EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
node.setCursor(Cursor.HAND);
}
});
node.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
dragItem = node;
dragImageView.setMouseTransparent(true);
node.setMouseTransparent(true);
node.setCursor(Cursor.CLOSED_HAND);
}
});
node.setOnMouseReleased(new EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
dragItem = null;
dragImageView.setMouseTransparent(false);
node.setMouseTransparent(false);
node.setCursor(Cursor.DEFAULT);
sceneRoot.getChildren().remove(dragImageView);
}
});
}
Maybe late, but with the setDragView option, is more simpple by now :)
// Cursor Display for Drag&Drop
source.setOnMouseEntered(e -> source.setCursor(Cursor.OPEN_HAND));
source.setOnMousePressed(e -> source.setCursor(Cursor.CLOSED_HAND));
source.setOnMouseReleased(e -> source.setCursor(Cursor.DEFAULT));
// Manage drag
source.setOnDragDetected(event -> {
/* drag was detected, start a drag-and-drop gesture*/
Dragboard db = source.startDragAndDrop(TransferMode.MOVE);
// Visual during drag
SnapshotParameters snapshotParameters = new SnapshotParameters();
snapshotParameters.setFill(Color.TRANSPARENT);
db.setDragView(source.snapshot(snapshotParameters, null));
/* Put a string on a dragboard */
ClipboardContent content = new ClipboardContent();
content.putString(source.getText());
db.setContent(content);
event.consume();
});

onClick and setImagResource [Android] API 10

I'm trying to understand why it's occuring me the following problem.
I have an ImageView and i set an image to it, then i setup a onClickListener to it, so when you click to image it change the image (to a new image) by image01.setImageResource(R.drawable.newImage). After that i call a method where i check a condition, if it is true i change the image again to the default one.
But I can't see the change because it change immediately. I also insert a sleep to make it slower.
(By default in the xml code i setted the image to oldImage)
Ok... maybe it's not clear.. so let's see the CODE:
private void myMethod(){
ImageView image01 = (ImageView) findViewById(R.id.image01);
image01.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//When you click on image it change!
image01.setImageResource(R.drawable.newImage);
checkImg(image01);
}
});
}
private void checkGame(ImageView img){
try{
Thread.sleep(1000);
if(condition)
img.setImageResource(R.drawable.oldImage);
}catch (Exception e) {
e.printStackTrace();
}
}
I saw immediately the oldImage. What's the problem?
Is it possible that the view change are not applied in myMethod() until all methods inside it will terminate?
Thanks in advance
Using Thread.sleep() method, you are actually making wait to main UI thread. The main UI thread's methods are not synchronized. Be aware of that.
Please go through the developers.android site for using of threads painlessly... before seeing the your useful code.
private void myMethod(){
ImageView image01 = (ImageView) findViewById(R.id.image01);
image01.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//When you click on image it change!
image01.setImageResource(R.drawable.newImage);
checkGame(image01);
}
});
}
private void checkGame(ImageView img){
try{
// Thread.sleep(1000);
if(condition)
image01.postDelayed(new Runnable() {
#Override
public void run() {
image01.setImageResource(R.drawable.oldImage);
}
}, 2000);
}catch (Exception e) {
e.printStackTrace();
}
}

GXT Problem with method override

Using GXT I would like to override the 'drop' in the 'Drag & Drop Grid' example on the GXT examples webpage.
Does anyone have an idea as to the method I should override? The API is so vast and verbose that is is a near impossibility to find anything in this mess!
To set a grid to do drag and drop is simply adding it as a Drag source as below
GridDragSource gridDragSource = new GridDragSource(yourGrid);
You then can add your own listener to handle drag
gridDragSource.addDNDListener(new DNDListener() {
public void dragMove(DNDEvent e) {
}
public void dragStart(DNDEvent e) {
}
public void dragEnter(DNDEvent e) {
}
public void dragLeave(DNDEvent e) {
}
public void dragDrop(DNDEvent e) {
}
}

Resources