Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Unable to use Buttons/CheckBoxes in List/TreeCell since JavaFX 8

cshMar 25 2014 — edited Apr 17 2014

Hi,

I've already filed a bug for this, but I want to also ask here for a workaround, since it's quite critical for us and probably the only issue, which is preventing us from migrating to Java 8.

The problem:

Since Java 8 we can't use ButtonBase elements in ListCells and TreeCells. E.g. when the cell's graphic has a Button or CheckBox and I click it, nothing happens. Instead the updateItem method is called again and renders again.

Is there any workaround or anything I am doing wrong? It worked well with JavaFX 2.2.

import javafx.application.Application;

import javafx.collections.FXCollections;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.control.CheckBox;

import javafx.scene.control.ListCell;

import javafx.scene.control.ListView;

import javafx.scene.layout.HBox;

import javafx.stage.Stage;

import javafx.util.Callback;

 

 

public class TestApp5 extends Application {

     public static void main(String[] args) {

         launch();

     }

 

     @Override

     public void start(final Stage stage) throws Exception {

 

         ListView<String> listView = new ListView<String>();

         listView.setItems(FXCollections.<String>observableArrayList("Item 1"));

         listView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {

             @Override

             public ListCell<String> call(ListView<String> stringListView) {

                 ListCell<String> listCell = new ListCell<String>() {

                     @Override

                     protected void updateItem(String item, boolean empty) {

                         super.updateItem(item, empty);

 

                         setGraphic(null);

                         if (item != null) {

                             Button button = new Button();

                             button.setOnAction(new EventHandler<ActionEvent>() {

                                 @Override

                                 public void handle(ActionEvent actionEvent) {

                                     System.out.println("Button clicked");

                                 }

                             });

                             HBox hBox = new HBox();

                             hBox.getChildren().addAll(button, new CheckBox());

 

                             setGraphic(hBox);

                         }

                     }

                 };

 

                 return listCell;

             }

         });

 

 

         Scene scene = new Scene(listView);

         stage.setScene(scene);

         stage.show();

     }

}

Comments

BluShadow
Not sure about a clean regular expression for it. I'm sure CD will be along soon to show us how... in the meantime, I managed to get it to deal with that specific format of string...
SQL> ed
Wrote file afiedt.buf

  1  with t as (select 'a, b, c (x, y, z)' as txt from dual)
  2  --
  3  select txt, replace(regexp_replace(txt, '(^,)*\(.*\)','\1'),',')||regexp_replace(txt,'.*(\(.*\))','\1') as new_txt
  4* from t
SQL> /

TXT
-----------------
NEW_TXT
--------------------------------------------------------------------------------
a, b, c (x, y, z)
a b c (x, y, z)
But if your format of string differs then it's gonna be more complex as far as I can tell.
cd_2
lurchmodeon You rang? lurchmodeoff

How about this?
WITH t AS (SELECT 'a, b, c (x, y, z)' col1 
             FROM dual)
SELECT t.col1
     , REGEXP_REPLACE(t.col1, '(\(.*\))|,', '\1') new_col
  FROM t
;               

COL1              NEW_COL
----------------- --------------------
a, b, c (x, y, z) a b c (x, y, z)
C.
564484

Hi cd and BluShadow,

Thanks very much for your valuable help!

One other question - what if I want to replace the non-() enclosed commas with another string - such as "#". I tried your suggestion and got this:

WITH t AS (SELECT 'a, b, c (x, y, z)' col1 
             FROM dual)
SELECT t.col1
     , REGEXP_REPLACE(t.col1, '(\(.*\))|,', '\1') cds_awesome_solution
     , REGEXP_REPLACE(t.col1, '(\(.*\))|,', '#\1') pdaddys_futile_attempt1
     , REGEXP_REPLACE(t.col1, '(\(.*\))|,', '\1#') pdaddys_futile_attempt2
  FROM t
;

Result:

"COL1"	             "CDS_AWESOME_SOLUTION"   "PDADDYS_FUTILE_ATTEMPT1"   "PDADDYS_FUTILE_ATTEMPT2"
"a, b, c (x, y, z)"    "a b c (x, y, z)"        "a# b# c #(x, y, z)"        "a# b# c (x, y, z)#"

Notice how the pound is place before the left paren in attempt1 and placed after the right paren in attempt2? I know I could nest another regexp_replace - but I was wondering if a single pattern could suffice...

Thanks - and sorry I didn't make my question clear at all in my original post...

You guys ARE geniuses

cd_2
Not sure about a single pattern, but at least without a 2nd regex_replace:
WITH t AS (SELECT 'a, b, c (x, y, z)' col1
             FROM DUAL)
SELECT t.col1,
       REPLACE(REGEXP_REPLACE(t.col1, '(\(.*\))|(,)', '\1\2\2'), ',,', '#') new_col
  FROM t;

COL1              NEW_COL
----------------- --------------------
a, b, c (x, y, z) a# b# c (x, y, z)
C.
564484
Thanks!

That works perfectly!

(though I don't know HOW it works :( - I don't understand how the \1 backreference gets the part of the string before the parentheses... )

oh well - sometimes you have to just have faith :) - seeing is believing.

Message was edited by:
PDaddy
cd_2
(though I don't know HOW it works :( - I don't
understand how the \1 backreference gets the part of
the string before the parentheses... )
You have to think of regular expressions as a parser. Each position of the searched string is compared to each available pattern. The first "()" search pattern sort of protects that string against the second pattern.
oh well - sometimes you have to just have faith :) -
seeing is believing.
Or you start thinking in regular expressions. ;-)

C.
nemecj
Excelent solution! You may consider to add a ? (non greedy, restrictive mode) to cover the case there are more parentheses in the string.

<pre>
WITH t AS (SELECT 'a, b, c (x, y, z), a, (xx, yy, zz), x,' col1
FROM dual)
SELECT t.col1
, REGEXP_REPLACE(t.col1, '(\(.*?\))|,', '\1') new_col
FROM t
;

COL1 NEW_COL
--------------------------------------
a, b, c (x, y, z), a, (xx, yy, zz), x, a b c (x, y, z) a (xx, yy, zz) x
</pre>
1 - 7
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on May 15 2014
Added on Mar 25 2014
13 comments
6,118 views