So i am working on an App and in menu bar, I would like to give Menu elements some cool looking separators. So I found a way that seems to work with HTML:
.menu
{
border-right: 2px dotted #666666;
}
so I just put "-fx-", before "border-right..." and that didn't work. After some more reaserch I found that I should separate width, style and colour, so it should look like this:
.menu
{
-fx-border-right-width: 2px;
-fx-border-right-style: dotted;
-fx-border-right-color: #666666;
}
But again it fails to work.
So is there a way to do it with border-right property of menu, or should I look for another way of doing this?
See the documentation. In JavaFX CSS, borders (and other properties) are specified either with a single value (representing the value for all sides), or four values delimited by whitespace (representing the values for top, right, bottom, and left.
So
.menu {
-fx-border-width: 0px 2px 0px 0px ;
-fx-border-color: #666666;
-fx-border-style: dotted;
}
should do what you are looking for.
Here is a quick test harness:
public class HelloApplication extends Application {
#Override
public void start(Stage stage) throws IOException {
BorderPane root = new BorderPane();
Region menu = new Region();
menu.getStyleClass().add("menu");
root.setCenter(menu);
BorderPane.setMargin(menu, new Insets(10));
Scene scene = new Scene(root, 800, 500);
scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
with style.css containing the CSS code above. This gives
Related
i am trying to show the opened tab text in left .i wrote that in stylesheet.but it is not working.
here is the stylesheet i used
QTabWidget QTabBar{
background-color: #373738;
height: 30px;
}
QTabWidget QTabBar::tab{
text-align: left;
background-color: #136ba2;
border-style: 1px rgb(67, 67, 67);
height: 30px;
width: 130px;
color: #136ba2;
padding: 0px 5px 0px 5px;
}
QTabWidget QTabBar::tab:selected{
background-color: #5A5B5C;
font-weight: bold;
color: #ffffff;
}
QTabWidget QTabBar::tab:!selected{
background-color:#353536;
color: #B4B4B4;
}
QTabWidget QTabBar::tab:hover{
background-color: #5A5B5C;
color: #ffffff;
}
here is an image
I think it is not possible with stylesheet, according to the doc :
text-align [...] This property is currently supported only by QPushButton and QProgressBar.
https://doc.qt.io/qt-5/stylesheet-reference.html#list-of-properties
This can be accomplished using the tab button widget to show the tab text.
Have a label, with left aligned text:
QLabel * button = new QLabel("text");
button->setAlignment(Qt::AlignLeft);
Set the tab text to empty string if necessary:
ui->tabWidget->tabBar()->setTabText(index, "");
Set the label as the tab button widget:
ui->tabWidget->tabBar()->setTabButton(index, QTabBar::LeftSide, button);
You can still style the label, adding a QLabel entry in the tab widget stylesheet:
QLabel{ color: white; }
Only problem here: no way to style the label text depending on selection (i.e. making the selected tab text bold) using CSS. But still feasible catching the tab widget currentChanged signal:
void Form::on_tabWidget_currentChanged(int index)
{
for(int i=0; i<ui->tabWidget->tabBar()->count(); i++)
{
QWidget * button = ui->tabWidget->tabBar()->tabButton(i, QTabBar::LeftSide);
if(button != nullptr)
{
QFont font = button->font();
font.setBold(i == index);
button->setFont(font);
}
}
}
One workaround(not clean method) I found is add some extra space at the end of the title;
QString tabTitle = "Page 1 ";
Will move the text towards left.
I wanna create a TabPane with some Tabs. If a tab is selected, it shows an image (in the header, not the content), but others will not show the image.
I use the setGraphic method to add an Image, here is my code:
TabPane tabpane = new TabPane();
Tab tab = new Tab();
tab.getStyleClass().add("ctab");
Label value = new Label();
value.getStyleClass().add("va");
tab.setGraphic(value);
tabpane.getTabs().add(tab);
Then I write the css to set its style:
.va {
-fx-pref-width: 120;
-fx-pref-height: 42;
-fx-font-size: 10pt;
-fx-text-fill: black;
}
.ctab:selected>.va {
-fx-pref-width: 120;
-fx-pref-height: 42;
-fx-background-image: url("background.jpg");
-fx-font-size: 10pt;
-fx-text-fill: black;
}
The tab is selected, but the image is not shown.
I'm so confused about it. Because when the image is in a button, the css works.
Button button= new Button("a");
button.getStyleClass().add("abtn");
Label value = new Label("b");
value.getStyleClass().add("value");
Label nest = new Label("s");
nest.getStyleClass().add("nest");
value.setGraphic(nest);
button.setGraphic(value);
root.getChildren().add(button);
and the css is:
.abtn {
-fx-pref-width: 120;
-fx-pref-height: 42;
-fx-font-size: 11pt;
-fx-text-fill: black;
}
.value {
-fx-pref-width: 120;
-fx-pref-height: 42;
-fx-background-color: #ffffff;
-fx-font-size: 10pt;
-fx-text-fill: black;
}
.abtn:hover > .value > .nest {
-fx-pref-width: 120;
-fx-pref-height: 42;
-fx-font-size: 20pt;
-fx-text-fill: black;
-fx-background-color: transparent;
}
When the mouse hover the button, the String "s" size is really changes.
What's the difference between two example? How can I make the tab's image change(use css)?
Your Label is not owned by the Tab directly. Try it out:
.ctab:selected > .tab-container > .tab-label > .va
As you can see, setGraphic is not always intended to set a child node.
JavaFX CSS Reference Guide helps you to figure out the structure of controls, but it doesn't have complete list of nodes (for example, TabPane's tab-container is not in the list).
To analyze the scene graph structure, CSS Analyzer in Scene Builder and ScenicView are useful.
Or simply the selector below also works.
.ctab:selected .va
I am in the process of styling buttons in my user interface, using the UI designer in QT Creator 3.0.1 (with QT 4.8). I am trying to have these buttons behave more like links on a website--without borders, and responding to mouse hovers. Here is the stylesheet I am currently using:
QPushButton {
border: none;
color: #a8a8a8;
}
QPushButton:hover {
color: #ffffff;
}
I thought it was pretty straightforward, but for some reason the color tag is not functioning on hovers. To test, I tried changing the button in other ways, such as changing the background color, and that worked flawlessly.
I also tried changing the selectors to something more specific, by including an ancestor (QWidget QPushButton:hover) and by using the ID (QPushButton#templateButton), but neither have worked.
Is this a problem with the color tag, or am I missing something obvious?
It is also possible to use QLabel to create clickable links. Create custom class ClickableLabel which inherits QLabel and handles mousePressEvents
class ClickableLabel : public QLabel
{
Q_OBJECT
public:
explicit ClickableLabel(QWidget *parent = 0);
signals:
void clicked();
protected:
void mousePressEvent(QMouseEvent * event) ;
};
And
void ClickableLabel::mousePressEvent(QMouseEvent * event)
{
Q_UNUSED(event);
emit clicked();
}
It is also probably possible to handle mouse hover events and change style of the label based on them. However, I have not tested it.
This solution has been copied from somewhere but I do not remember anymore the original source.
Try this :
#templateButton {
border: 0px;
color: #a8a8a8;
text-decoration: underline;
text-align: right;
}
#templateButton:hover {
color: #FFFFFF;
}
#Anthony Hilyard I show you some pictures so I post another answer here.
1.normal
2.hover
#pushButtonForgetPassword {
border: none;
color: #0066ff;
text-decoration: underline;
text-align: right;
}
#pushButtonForgetPassword:hover {
color: #FFFFFF;
}
pushButtonForgetPassword would be the ID of my link button
I'm learing JavaFx and I'm trying to change the css style of my window (Mastermind game) at runtime.
Currently it works with some objects like buttons, background, menu bar, labels and shapes.
But another object I'd like to change is the menu items (background and labels).
I've tried to use the next css code to acces it :
.context-menu {
-fx-background-color: linear-gradient(rgba(200,200,200,1),rgba(50,50,50,1) 80%);
}
It works on initalisation but not when changed at runtime.
For example, it works fine for the menu bar itself :
.menu-bar {
-fx-background-color: black;
-fx-selection-bar: #505050;
}
.menu-bar .label {
-fx-font-size: 12pt;
-fx-font-family: "Segoe UI Light";
-fx-text-fill: white;
-fx-opacity: 0.9;
}
In the FXMLcontroller I call the following code to change the skin :
#FXML private AnchorPane generalPane;
private final String themeNormal = getClass().getResource("/mastermindStyles/MasterMind_Normal.css").toExternalForm();
#FXML
void handleSkinNormal(ActionEvent event){
generalPane.getStylesheets().clear();
generalPane.getStylesheets().add(themeNormal);
}
How can I do it?
I've found it :
To use predefinite styles (like Modena and Caspian) :
Application.setUserAgentStylesheet(STYLESHEET_MODENA);
To use your own css stylesheet :
Application.setUserAgentStylesheet(null);
StyleManager.getInstance().addUserAgentStylesheet(
getClass().getResource("NewSkin.css").toExternalForm());
I have a CellTable with Columns. One of the column I align the text to the right programatically:
column.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
Further I want the cell to have a padding on the right. By cannot get it to work. This is what I tried:
column.setCellStyleNames("numberCell");
.numberCell {
padding-right: 5px !important;
margin-right: 5px !important;
color: red !important;
}
The text in the cell is rendered in red, but there is no padding.
As you applied style it should work. but let You try apply particular column style using getCellStyleNames. it may work.
Column<Data,String> noColumn=new Column<Data, String>(new TextCell()) {
#Override
public String getCellStyleNames(Context context, Data object) {
return "numberCell";
}
#Override
public String getValue(Data object) {
return "";
}
};