How to change the background color of a line when you have the selection of cells in tableview?

I have a tableview and I chose the selection of cells. How can I change the background color of a line, if I select a cell?

The usual example using:

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener.Change;
import javafx.collections.ObservableList;
import javafx.collections.ObservableSet;
import javafx.css.PseudoClass;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class RowHighlightedCellSelectionTableViewSample extends Application {

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

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        scene.getStylesheets().add(getClass().getResource("selected-row-table.css").toExternalForm());
        stage.setTitle("Table View Sample");
        stage.setWidth(450);
        stage.setHeight(500);

        final Label label = new Label("Address Book");
        label.setFont(new Font("Arial", 20));

        final TableView table = new TableView<>();
        final ObservableList data =
            FXCollections.observableArrayList(
                new Person("Jacob", "Smith", "[email protected]"),
                new Person("Isabella", "Johnson", "[email protected]"),
                new Person("Ethan", "Williams", "[email protected]"),
                new Person("Emma", "Jones", "[email protected]"),
                new Person("Michael", "Brown", "[email protected]")
        );

        table.getSelectionModel().setCellSelectionEnabled(true);
        table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

        final PseudoClass selectedRowPseudoClass = PseudoClass.getPseudoClass("selected-row");
        final ObservableSet selectedRowIndexes = FXCollections.observableSet();
        table.getSelectionModel().getSelectedCells().addListener((Change change) -> {
            selectedRowIndexes.clear();
            table.getSelectionModel().getSelectedCells().stream().map(TablePosition::getRow).forEach(row -> {
                selectedRowIndexes.add(row);
            });
        });

        table.setRowFactory(tableView -> {
            final TableRow row = new TableRow<>();
            BooleanBinding selectedRow = Bindings.createBooleanBinding(() ->
                    selectedRowIndexes.contains(new Integer(row.getIndex())), row.indexProperty(), selectedRowIndexes);
            selectedRow.addListener((observable, oldValue, newValue) ->
                row.pseudoClassStateChanged(selectedRowPseudoClass, newValue)
            );
            return row ;
        });

        TableColumn firstNameCol = new TableColumn<>("First Name");
        firstNameCol.setMinWidth(100);
        firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));

        TableColumn lastNameCol = new TableColumn<>("Last Name");
        lastNameCol.setMinWidth(100);
        lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));

        TableColumn emailCol = new TableColumn<>("Email");
        emailCol.setMinWidth(200);
        emailCol.setCellValueFactory(new PropertyValueFactory<>("email"));

        table.setItems(data);
        table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(label, table);

        ((Group) scene.getRoot()).getChildren().addAll(vbox);

        stage.setScene(scene);
        stage.show();
    }

    public static class Person {

        private final StringProperty firstName;
        private final StringProperty lastName;
        private final StringProperty email;

        private Person(String fName, String lName, String email) {
            this.firstName = new SimpleStringProperty(fName);
            this.lastName = new SimpleStringProperty(lName);
            this.email = new SimpleStringProperty(email);
        }

        public String getFirstName() {
            return firstName.get();
        }

        public void setFirstName(String fName) {
            firstName.set(fName);
        }

        public StringProperty firstNameProperty() {
            return firstName ;
        }

        public String getLastName() {
            return lastName.get();
        }

        public void setLastName(String fName) {
            lastName.set(fName);
        }

        public StringProperty lastNameProperty() {
            return lastName ;
        }

        public String getEmail() {
            return email.get();
        }

        public void setEmail(String fName) {
            email.set(fName);
        }

        public StringProperty emailProperty() {
            return email ;
        }
    }
}

And then the selected line - table.css:

.table-line-cell: {selected row

-fx-background-color: lightskyblue;

}

Tags: Java

Similar Questions

  • Digital background color works do not when you use the reference

    Hello

    I am trying to use a reference to change the background color of a digital indicator in a Subvi.  The sub - VI strives to change a digital indicator, but when I use a reference to another (reference Numeric.vi) VI, instead of doing all the background, the color I want, only the outline is the color I want, with the rest in white (natural color).    Is there a way to avoid this?

    You must use the NumericText.TextColors.BG property instead.

    Felix

  • How to add a background color to a line of text?

    Hello, Adobe Forums.

    I have a small question:


    Would it be possible to create a style where it has a background color that crosses the screen? (see attachment)

    At work, I was given some materials where this color was made. It was done in MS Word, but it was attached directly to the text. I want to do because I am creating a number of models, and the way I do now is not very convenient (contained in the text box and a text box to second, colorful, is placed under the it). It would be possible to do what I'm doing? Research lead me to highlight the text through the use of underscore + changes in weight and offset, but I need the coloring to go the length of the page.

    Thanks for your suggestions.

    Untitled.png

    Use a net of paragraph. Set the thickness and offset appropriately.

    Note that will work well with a paragraph on a single line.

    Bob

  • How to change the selectable languages for spell checking?

    Hallo community Mozzila

    After using Firefox to group every night 23 on Chakra Linux via and I think it has messed up my settings speller via Firefox sync.

    The problem of the pop-up on openSUSE and Chakra Linux but not in Kubuntu 12.04 (it has its own language packs).
    I want to have German and English GB and maybe Bosnian or Croatian spell checker, but I cannot select via right click - languages.
    Now, I have Chakra Linux and openSUSE 12.3 Firefox 20 by perhaps 30 default languages installed but I can not select the language I want.

    For example the Chakra Linux Firefox 20 I only selectable variants, German back Germans, German Belgium... and then, and I can't select another language via the module or about: config spellchecker.dictionary; so I need to edit the right click language selector to bring it to work, it stay alwas on a German Variant.

    2 example on openSUSE cannot select German and En_GB, but I can't select a bunch of other languages, that I did not need in the click languages Selector right.

    Both are Firefox 20 with openSUSE KDE changes and have installed the available spelling dictionary.

    So, how can I change the list of selectable languages on the languages right click?

    OK, this is now resolved.

    In Chakra hunspell English was missing and on openSUSE, a seller has changed of Mozilla and now it works very well.

    Sorry to the comminty boreder

  • How to change the selection in the layout of the text Mode

    Can I change the mode of selection in read-only, read-select and read - write.

    Concerning

    Varun Roulin

    http://www.vrathore.blogspot.com

    For a TextFlow which is achieved by assigning the interactionManager.

    textFlow.interactionManager ReadOnly = null;

    reading-select textFlow.interactionManager = new SelectionManager();

    read / write textFlow.interactionManager = new EditManager();

    Kind regards

    Richard

  • How to change the selected index of a list of a DG?

    It is probably simple and I'm in the right place. I have a datagrid. When I select an element of the DG, he fills out a form that can be updated. One of the form fields is a list control with a remote object values. I want this list to highlight/select the value that corresponds to that of the DG when the item is selected. For example if I have a list of numbers 1, 2, 3, 4, 5 and the question that I select in the DG contains a 4, then I want the list to automatically select 4 as a default in the list when I select the element of the DG.

    You must loop through the result of your RemoteObject looking for a match, after that you have added to the list. I posted a sort of model of example to show what I mean, yours will be different of course, but I think you can grasp the concept based on what I'm trying to show.

  • Can you view the contents of subfolders when you have a selected folder in windows Explorer?

    You have a folder called folder.

    You have a sub folder called folder B.

    In windows Explorer if you have the one selected folder is it possible to view the contents of the folder B then that file is still selected?

    Hi Goatberg,

    Unfortunately, there is no way to show contents of the folder when the parent folder is always selected.

  • How to change a background color of a picture

    Hello

    I have a black image and whilte.

    Now, I want to change black to a grey color, I found on another image, while they mix.

    How can I do this?

    I tried Image > adjust colors but no luck.

    Levels adjustment layer and drag the black output slider to the right.  You can see below I put a pair of color sampler points, one on each background, so I could see exactly which setting I got a perfect match

    Click to enlarge the screenshot below to see all of this, if it's the hidden part

  • How to change the selected part of the pdf

    I would like to create a contract in pdf format. Is it possible to create the ONLY part of the text allowed to edit? For example: on behalf of the company to fill the street and...
    How to do step by step?

    Use the form fields.

  • How to change the selected B &amp; W images

    I have a collection of say 20 color images. I want to apply the same processing black and white in every 20. When I'm done, I wish I had pictures of 20 original colors and their 20 black and white conversions in the same collection. Is this possible? And could you explain?

    Thank you

    Pat

    PS I'm new to Lightroom 3.3

    Select all,

    Command N (PC), command N (Mac) to create a new collection.

    Select everything in the collection, right click > create a virtual copy.

    Press V

    You should now have color and B & W images in a collection.

  • Hi there, I chooce to open wordpad, but it isn't good then I need to now how to change the selection to please, thank you

    That's what I now please, thank you

    You can click right on the document, and under "Open with", click on "choose program by default... "to select another program to open the file.  Be sure to focus on the checkbox "Always use selected program to open this type of file" near the bottom and decide if you want that all documents currently open in Wordpad to open in your new selection.  Also, you will need to have an another word processor installed that will open documents in RTF format.  If you do not there are now office and word processing free programs such as: http://www.openoffice.org/ and http://www.libreoffice.org/download/ be aware that these programs are more powerful but also more complicated than Wordpad.

  • How to change the selection mask and layer image together?

    I have a newbie question.

    On Photoshop CS6

    I have a picture with a layer mask, which is related, as evidenced by the string between the image and the mask. I make a choice and try to apply a scale in free form, but I can only to scale the image or layer mask, not the two together. Don't seem much sense to me, everyone knows the answer?

    I have to set up a demo'ing youtube video my problem to make things clearer, link here: http://www.YouTube.com/watch?v=-Z6E98FZzMo

    Any help would be great

    Hello

    You only select the layer and not the layer with mask.

    Try this:

    With nothing selected, select the layer in the layer panel and then free transform.

    This should affect the layer and mask.

  • How to change the controlbar in fullscreen?

    function fullscreenOnClicked(e:MouseEvent):void {
         stage.displayState = StageDisplayState.FULL_SCREEN;
    }
    
    function fullscreenOffClicked(e:MouseEvent):void {
         stage.displayState = StageDisplayState.NORMAL;
         
    }
    
    function onFullscreen(e:FullScreenEvent):void {
          if (e.fullScreen) {
              
              // switch fullscreen buttons
              mcVideoControls.btnFullscreenOn.visible = false;
              mcVideoControls.btnFullscreenOff.visible = true;
                             
              // bottom center align controls
              mcVideoControls.x = (Capabilities.screenResolutionX - 480) / 2;
              mcVideoControls.y = (Capabilities.screenResolutionY - 36);
              btnMidPlay.x = stage.stageWidth / 2;
              btnMidPlay.y = stage.stageHeight / 2;
                        
              // size up video display
              vidDisplay.height      = (Capabilities.screenResolutionY);
              vidDisplay.width      = vidDisplay.height * 4 / 3;
              vidDisplay.x          = (Capabilities.screenResolutionX - vidDisplay.width) / 2;
        } else {
              // switch fullscreen buttons
            mcVideoControls.btnFullscreenOn.visible = true;
              mcVideoControls.btnFullscreenOff.visible = false;
    
              // reset controls position
              mcVideoControls.x = 0;
              mcVideoControls.y = 325;
              btnMidPlay.x = 240;
                    btnMidPlay.y = 180;
              // reset video display
              vidDisplay.y = 0;
              vidDisplay.x = 0;
              vidDisplay.width = 480;
              vidDisplay.height = 360;
        }
    }
    

    my 4:3 Player

    above script is as below

    max_1.jpg

    but I want to like this

    max_2.jpg

    How to change or build my script?

    Normally, you have a class for the video controller, and the class has a function to handle the resizing.

  • It is difficult to see how to change the color of the scrollbar in gray on gray background?

    It is difficult to see how to change the color of the scrollbar in gray on gray background?

    I think you should probably use an extension or theme to replace the default colors.

    If you are not interested in all the changes that might come with a new theme, is the next extension compatible with Linux?

    https://addons.Mozilla.org/firefox/addon/noiascrollbars/

  • How to change the background of the playlist window colors in iTunes

    The background color of my playlist 'window' in iTunes is black, which is difficult to read.  How to change the background of all the playlists to a light color.

    All the indications are that the selected background colors are random and cannot be changed. If someone knows a way to change them please let us know.

Maybe you are looking for

  • Capacity lost to bookmark pages after recent updates

    Firefox has been working great until recent updates of the version 39. I have now lost my ability to bookmark a page. When I click on the star icon as I always got it, the Star turns bright blue and gives the appearance that the bookmark is transferr

  • Satellite P70-A01C in World of Warcraft frame rate is LOW

    Bought this laptop a few months as a game machine. Said he would go to more recent games with little drama, but I have problems with World of Warcraft. Even with a quad core processor, 16 GB of RAM and a 4 GB graphics card I sometimes 9-12 fps. This

  • Satellite A75-S231: error NFP LIST CORROMPRE - fails to load Windows

    I have a Toshiba Satellite A75-S231. I started the computer and it kept freezing a few minutes after the start, gives me an PFN_CORRUPT_LIST error. Now, he is unable to load even in windows, the computer blue screens during the loading screen and imm

  • Firefox on the triumph

    I'm not going to fault Motorola on this one--but maybe Google; the jury is not out yet - but I tried Firefox 6 and Firefox 7 on my triumph and two versions starting today, managed to the same behavior. Blocking issues. The browser crashes. Even resta

  • Internet Explorer will work only in administrator mode

    Original Title: Internet Explorer 8, Windows Vista Home Premium sp2 Internet Explorer runs only in administrator mode.  When I right click on the shortcut on the desktop or go to programs to load it, it loads then immediately stops working and freeze