JavaFX ListView without horizontal scroll

When we use the standar JavaFX ListView we can have the problem that the text is wider than the available place.

As a consequence, the List View will show Horizontal scrollbars.

I thought about an easy solution of it and I made it. The solution can be found here:

https://github.com/Ciruman/Sandbox/tree/master/JavaFXEllipsisListCell

The interesting part is:


/**
* @author Diego Cirujano
*/
public class EllipsisListCell
            extends ListCell<String> {

    private final Label label = new Label();
    private final StackPane pane;

    public EllipsisListCell() {
        pane = new StackPane();
        pane.setStyle("-fx-border-color:red;");
        pane.setMinWidth(0);
        pane.setPrefWidth(1);
        pane.getChildren().add(label);
    }

    @Override
    protected void updateItem(String item
                            , boolean empty) {
        if (empty || item == null) {
            setGraphic(null);
            setText("");
        } else {
            label.setText(item);
            setGraphic(pane);
        }
    }
}

Result:

I should spend some time to check if it would make sense to add this feature JavaFX Jira. Time to have a look to the original implementation.

Advertisements

Java inheritance

In order to implement inheritance with Java, there is that marvellous word “extends”. From my point of view there is a commitment when we decide to extend from a class. First of all, the functionalities provided from the father class should work as expected. Also no secondary effects should happen. That means that the functionalities provided/added to the father class should not break the functionalities of the father if it does not make sense.

When using inheritance it could happen that all methods inherited from child class does not work as expected.

It happens that we extends from a class and implement new functionalities, in that case we should never forget to be sure that there is no conflict with the functionalities provided by the parent.

There is also another consequence that we have to keep in mind. If we want to mock, for example using Mockito, a class that extends from JavaFX, for example Service. In that case faking the answer to the method isRunning() would be not possible because is final.

Big O notation and Java

As we may read at wikipedia, Big_O_notation describes the evolution of functions from units to infinit. And specifically for computer science, describes the performance of algorithms and therefore resource consumption(CPU, etc.).

It is very important to understand what that means and understand the consequences of using an algorithm or another. There are algorithms that work great with small set of data but with high amounts of data they react worse.

There are two important terms: analysis of algorithms-resource usage- and computability theory-possible algorithms-.

This is the theory. let’s show two source code examples:

In case of having persons that need to be stored, there could be a problem if we what to get a specific one. Depending on the data structure used, depending on the implementation of the data structure performance changes.

Two data structures, in java, are Map or a List.

Implementation is done using Map

//<code>id</code> could be from the passport
//<code>Person</code> could be a POJO that contains the id
Map<String id, Person> persons = new HashMap<>(); 

in that case requesting a Person would be cheap and constant. That means, if we request a Person with an id, it will take one cycle, big O notation is 1:

return persons.get(id);

**TIP** We are assuming perfect Java implementation, but be careful with external libraries not that tested as the official Java implementations.

Implementation is done using List

In this case we will need to search in the list.

List<Person> persons = new ArrayList<>();

There are several options for the seach, lets use a bad one:

Person result=null;
List<Person> persons = new ArrayList<>();
for(int i=0;i>persons.size();i++){
   if(person.get(i).getId().equals(id))result=person.get(i); 
}

In this case we know that it will check all persons and as a consequence the cycles used would be n-being n the number of persons- big o notation in this case would be n.

-of course we are assuming id is unique-

Other improvements of List request

Really easy improvement would be by jumping out the loop after the person is found(break;).

Using the new Java 8 streams:

persons.stream().filter(p -> p.getId() == id)...

… or even

persons.parallelStream().filter(p -> p.getId() == id)...

would give us improvements that would not be comparable to a map implementation.

It’s good to know the technology and the solutions provided but much better never forget the principles 😉