How can I make the list header more prominent?

In my previous question How do I add shortcuts to parameters in combobox and list box?

I asked about how to add a title to my summary! The answer was perfect, but I can't tell the difference between the titles and my options. Is it possible to undo everything that can be selected? Or make my headlines bold? My code is pretty much the best answer to my previous question. What...

All titles cannot be selected.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class ComboBoxWithSections extends Application {

@Override
public void start(Stage primaryStage) {
    ComboBox<ComboBoxItem> combo = new ComboBox<>();
    combo.getItems().addAll(
        new ComboBoxItem("Short Duration", false),
        new ComboBoxItem("Last Hour",      true),
        new ComboBoxItem("Last 2 hours",   true),
        new ComboBoxItem("Last 24 hours",  true),
        new ComboBoxItem("",               false),
        new ComboBoxItem("Long Duration",  false),
        new ComboBoxItem("Last Month",     true),
        new ComboBoxItem("Last Year",      true)            
    );

    combo.setCellFactory(listView -> new ListCell<ComboBoxItem>() {
        @Override
        public void updateItem(ComboBoxItem item, boolean empty) {
            super.updateItem(item, empty);
            if (empty) {
                setText(null);
                setDisable(false);
            } else {
                setText(item.toString());
                setDisable(! item.isSelectable());
            }
        }
    });

    BorderPane root = new BorderPane(null, combo, null, null, null);
    primaryStage.setScene(new Scene(root, 250, 400));
    primaryStage.show();
}

public static class ComboBoxItem {
    private final String name ;
    private final boolean selectable ;

    public ComboBoxItem(String name, boolean selectable) {
        this.name = name ;
        this.selectable = selectable ;
    }

    public String getName() {
        return name ;
    }

    public boolean isSelectable() {
        return selectable ;
    }

    @Override
    public String toString() {
        return name ;
    }
}

public static void main(String[] args) {
    launch(args);
}
}

      

+2


source to share


1 answer


You need to understand the cell factory. The updateItem method is where the cell is drawn.

        super.updateItem(item, empty);//does important things in super
        if (empty) {                  //what to do if cell is empty   
            setText(null);
            setDisable(false);        //reset the disabled state 
        } else {
            setText(item.toString()); //calls toString in your custom class
            setDisable(! item.isSelectable()); //what stops you from selecting
            setTextFill(item.isSelectable()
                    ?Color.BLUE:Color.BLACK); //changing the color
        }

      



You can add color or font to your own class and just use that instead of isSelectable () to define the color.

You can also use CSS to style disabled cells differently, but this may be less flexible.

0


source







All Articles