Creating CSS style rules for aligning table column headers in JavaFXML

Is it possible to style columns individually in a tableview?

I have been attempting to make some column headers left-aligned and others right-aligned, as shown below:
| hdr1 | hdr2 | hdr3 | hdr4 |

In my css stylesheet, I experimented with the following code:

#colHeadingLeft .column-header{
  -fx-alignment: CENTER_LEFT;
}

#colHeadingRight .column-header{
  -fx-alignment: CENTER_RIGHT;
}

Despite setting id attributes for each column header tag in SceneBuilder using Java FX CSS, all headers end up being center aligned like this:

| hdr1 | hdr2 | hdr3 | hdr4 |

I have tried numerous solutions without success. Could it be that styling columns individually in a tableview is not supported?

Answer №1

To adjust the alignment of the child label within a column header, consider utilizing style classes for better control.

Below is an example using a ComboBox to select the alignment of a TableColumn column:

column.getStyleClass().add("center");

ComboBox<String> styleChoice = new ComboBox<>();
styleChoice.getItems().addAll("left", "center", "right");
styleChoice.valueProperty().addListener((o, oldValue, newValue) -> column.getStyleClass().set(column.getStyleClass().size() - 1, newValue));
.column-header.center .label {
    -fx-alignment: center;
}

.column-header.left .label {
    -fx-alignment: center-left;
}

.column-header.right .label {
    -fx-alignment: center-right;
}

The use of the combobox above demonstrates how to programmatically change alignment. You can also assign style classes directly to the TableColumns:

column1.getStyleClass().add("left");
column2.getStyleClass().add("left");
column3.getStyleClass().add("right");
column4.getStyleClass().add("right");

In this case, avoid using ids since the same style is applied to multiple columns, which would necessitate additional CSS selectors for each individual column.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to remove existing API requests in selenium?

There is a single button that, when clicked in older versions, sends a request to /api/accept. After upgrading, the same request is sent. However, in the latest version, the API request changes to /api/acceptconfirm. Is there a way to clear previous API r ...

Tips for updating sass import files as the body class changes

Is there a way to dynamically change the SCSS file with color variables based on the changing body class? For example, if my HTML includes: <body class="custom"> ... </body> I would like to load in style.scss: @import 'custom&a ...

Positioning icon/image beside the input box

Currently, I have a form with two buttons in a row and an icon (paper clip) that I am attempting to align next to the buttons using UIKit. However, I am facing challenges in getting the alignment right without affecting the size of the elements. Below is a ...

Using the swiper carousel on WordPress results in an unexpected horizontal scrolling issue

Running an Elementor website, I need to incorporate various image carousels within my post content. Initially, I created a template for each carousel using Elementor. However, I have now decided to switch to utilizing a shortcode that leverages Elementor&a ...

Are there specific mathematical algorithms that lend themselves well to creating responsive HTML designs?

Tired of constantly guessing the percentage values to use with a specific number of divs and other elements in my design. I am looking to understand the mathematical calculations that determine the scaling required for different elements in order to main ...

When a container is styled with 'overflow-y: auto', it will display additional horizontal and vertical scrolling bars

I have two files, one HTML and one CSS. The HTML file contains the following code: <div class="app"> <div class="header"></div> <div class="main"> <div class="container_1"> ...

Only referring to a CSS class within a particular element

I have defined the following CSS: .grade a{ color: white; } It is working, but maybe too well... Now, I have an HTML structure like this: <li class="grade"> <a><i class="fa fa-star fa-fw"></i></a> My Text < ...

Issue with Bootstrap tab display of content

I'm having trouble with the tabs in my page. When I click on each tab, the content doesn't display correctly. Here is my code: <div class="w470px exam" style="border: 1px solid #ddd; margin-top: 30px;"> <ul id="myTab" class="nav nav ...

Display a specific section of an image as the background in a div, with the image scaled and positioned perfectly

Utilizing a 1900 x 1080 image within a div as the background <!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8" /> <style> html,body { height:100%; } #imageHolder{ ...

Utilizing Boolean Operators in JavaScript with Thymeleaf: A Guide

When incorporating Boolean conditions in JavaScript with Thymeleaf using th:inline="javascript", an exception is thrown which appears as follows: org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 22; The entity name must immediately follow the ...

What is the best way to retrieve the size of a JSON object that is stored in the local project directory

I am working on a spring-boot project with the following file structure... -src -target -pom.xml -uploaded_json -- example.json Here is the content of my example.json file: [ { "Username": "U1", "Password" ...

Various results can be produced based on the .load() and .resize() or .scroll() functions despite using the same calculation methods

I'm currently working on developing my own custom lightbox script, but I've hit a roadblock. For centering the wrapper div, I've utilized position: absolute and specified top / left positions by performing calculations... top: _center_ver ...

Utilizing WordPress' WTI Like Post Feature on Your Android Device

Let's start by discussing a plug-in on my WordPress site called "WTI Like Post." This plug-in allows users to vote posts using a thumbs up system... If you're curious, you can check out its GitHub repository: By clicking on these thumbs, an aja ...

What is the proper way to manage errors before clicking a button in WebDriver?

What is the appropriate method for handling exceptions when clicking a button using Webdriver? This is my code snippet: public void clickSearchButton() throws Exception{ try{ app.safeClickOnElement(SEARCH_BUTTON); }catch (Exception e){ ...

Is there a way to retrieve an element based on its position within the webpage?

Issue at Hand: My current challenge involves extracting phone numbers from a webpage. Each phone number is concealed behind a button labeled "show contact info". The actual numbers are not visible in the dom until after the button is clicked. Upon clicking ...

Populate the div with an image that stretches to the full height

I'm facing an issue with my website's front page design, where a div is divided into two sections using the Twitter Bootstrap grid system. When the second grid (span9) is taller than the first (span3), the image perfectly fills up the span9 area ...

The webpage failed to show the hamburger menu

I'm trying to create a hamburger menu and have written the following code: <header class="header"> <nav class="flex flex-jc-sb flex-ai-c"> <a href="/" class="header__logo"> <img src="im ...

Is there a unique shape element, such as a true circle, available in HTML5?

There doesn't seem to be any documentation on this, suggesting they may not exist. However, it's always worth investigating. I am in search of a perfectly circular (or any polygon shape other than a rectangle) element. I can create a circle in c ...

The Java for loop consistently evaluates to true

I encountered an issue with the following code. Despite having two failed scenarios, the result always shows as pass. List<WebElement> elementsList = driver.findElements(By.className("android.widget.TextView")); for (WebElement element : elementsL ...

Choosing the Right Alignment for Your Selection Box

How can I adjust the alignment of the select box to make it inline with the others? I also need to apply this alignment to the three option input boxes. CSS: #newwebsiteSection, #websiteredevelopmentSection, #otherSection{ display:none; } #newwebsite ...