Toggle JToggleButton without clicking it, and unsetting styles - java

I have a few toggle buttons to add styling to text in the form of:
JToggleButton boldButton = new JToggleButton("Bold");
boldButton.addActionListener(new StyledEditorKit.BoldAction());
boldButton.setFocusable(false);
and I also have various styling functions (color, left/right/center align, etc.). What I'm wanting to do is when I call a function (in this case, I'm wanting to basically make a new document) the toggle button is toggled to the off state, and all styling returns to default.
Currently I just set the frame name to untitled (previously the name of the document I was working with), and set the pane text to "", but this keeps all of the styling I had set in the previous document. Is there a better way to do it?
Thanks

boldButton.setSelected(false)
should be all you need.

Related

Javafx, Is there a way to check state of buttons and do an if statement depending on its state?

In Javafx there is an option to set colors/styles for buttons e.g.,
button.setStyle("-fx-background-color: red");
Is there any syntax for checking a buttons style for witch color the button has?
Basically I want to do something like:
if (button.style("-fx-background-color: red")) {
something....
}
Yes, you can do it via .contains() or via regular expression (.matches()) by using getStyle() (for example, in your case, it would be button.getStyle().contains("-fx-background-color: red").
However, keep in mind that both setStyle() and getStyle() only refer to inline styles. Therefore, they will not include the styles that are passed via CSS selectors in attached CSS files.
Generally, using visual properties for determining semantic properties is not considered a good practice. If you have buttons that are supposed to exhibit a particular behavior, consider extending the Button class and adding those as proper properties instead.
Button button = new Button();
String[] styles = button.getStyle().split(";");
for(String style : styles){
if(style.contains("-fx-background-color")){
String color = style.split(": ")[1]; // the color of the button
}
}

JavaFX set Button text via CSS

I googled for this, but couldn't find anything. I know that I can style a Buttons text, background colour etc, but I would like to change the text itself.
This is what I've tried (of course the actual end result is more complex, but this will do):
Button#playButton {
-fx-background-color: #ddd;
-fx-text: "Play";
}
the background colour is correctly applied, but the text does not change.
Edit: The way to go seems to be a custom property, if someone has knowledge within that field I would love a concise explanation and implementation, that could save me hours of trial and error with the few sources I found!
No you cannot set the text of your button using css. But hey! In the other hand, you can still display an icon in your button using css!
You only need to add this to your css :
-fx-graphic : url("relative path to your icon");
In case your icon is repeated in the button, then add this as well :
-fx-background-repeat : no-repeat;

Display text over the icon in JXLabel

I'm using SwingX label for a image select/preview.
I need to show both text and icon together but when I set icon the text disappear!
How can I show text over the icon on a JXLabel?
Thanks
there no issue with that, there are two ways
1.correct way (implemented in the API, good habits, blablabla)
JLabel.setHorizontalTextPosition(JLabel.CENTER);
// search for equivalent in SwingX
2.crazy way to set proper LayoutManager for JXLabel and put there another JXLabel to the desired coordinates Location

NetBeans: How to select JLabel that resides inside JFrame?

I developed a small desktop application in Java. At some position in my UI I drag and drop a JLabel from Palette to JFrame. Later-on i remove its text by right click -> Edit Text. I did this because i am populating this label dynamically using setText() method. Now i need to reposition this Label, for this i first have to select that label and then drag n drop it to new location. But i am unable to do so because there is no text in that label and hence its not visible :(
Is there any way through which i select this label?
The easiest way is add a few spaces to a label instead of an empty string. You may also put a label inside a panel with layout like Flow or Grid (where you can set margin) and drag the panel instead. If you're using layout like Free Design, Absolute or Null, you may also manually rescale the label (selecting it through Inspector view).

Using <html><u> tags to make button text underlined, caused button to take up entire JToolBar

I need to have a button whose text is underlined and the only way I could find to do this in Java was to use and tags, but when I do this, it causes the button to take up as much room as is left in the JToolBar even though the text is short and it should only take up a small amount of space. Here is how I create the Button.
String buttonText = new String("<html><u>Lesson Plans</u></html>");
JButton lessonButton = new JButton(buttonText);
toolBar.add(lessonButton);
If I remove the tags then it takes up the right amount of space but if I have them in there is takes up the entire toolBar. Anyone know what's going on?
You might be able to fix the problem by using:
button.setMaximumSize( button.getPreferredSize() );
Otherwise you should be able to just change the font to use an underlined font. Darryl's Visual Font Designer shows how to add attributes to a font.
You can overwrite the paintComponent method of your JButton, and write on it with any style and font.
You forgot the closing "" and wrote "" instead... This may be the reason for your problems.

Categories

Resources