I am creating a checkbox button with text in SWT.
org.eclipse.swt.widgets.Button button= new Button(composite, SWT.CHECK);
button.setText(Messages.AnswerDialog_answer);
button.setSelection(true);
In messages.properties, I have the value
AnswerDialog_answer=Answer
How can i show the text(ie. Answer) of this button in BOLD?
You can convert the currently assigned font for the button to be bold using something like:
FontData [] data = button.getFont().getFontData();
for (FontData datum : data) {
datum.setStyle(SWT.BOLD);
}
Font boldFont = new Font(button.getDisplay(), data);
button.setFont(boldFont);
Note: You must call boldFont.dispose() when you are done with the font (such as when the dialog closes).
I'm not sure that all platforms support changing the button font. The above code works on macOS.
Related
I try to make a label to display "You Win" and add it to winStage, but I cannot manage to display the label correctly on winStage. Some words don't show on the screen, and if I downscale further than 0.044f, the String disappears. when I add font.setUseIntegerPositions(false) to my code, the font shows correct, but the wrong label also shows. Is there a way to get this work? (hide the wrong label or get the label shows correctly), code uses Kotlin, but very similar to java
YUW N is the wrong label I want to remove, the correct "You Win" only shows when I add font.setUseIntegerPositions(false)
here's the relevant code part
private val winViewport = StretchViewport(mainStage.width, mainStage.height)
val winStage = Stage(winViewport, winBatch)
var font = BitmapFont()
var labelStyle = Label.LabelStyle()
labelStyle.font = font
labelStyle.fontColor = Color.RED
var label2 = Label("YOU WIN", labelStyle)
label2.setBounds(0f, winStage.height*2/3, winStage.width*1, winStage.height/6)
label2.setFontScale(0.07F) // 0.07f
font.setUseIntegerPositions(false)
label2.setWrap(false)
winStage.addActor(label2)
winStage.act(delta)
winStage.draw()
I have a text in the Link SWT widget created as follow:
Link message = new Link(parent, SWT.WRAP);
message.setText(myMessage);
I want the text (in myMessage variable) be selectable, to grant users to copy it.
How can I do this?
I have used Link widget because I need hyperlinks in the text to be clickable.
The SWT Link widget is not selectable. To work around this I can think of either
provide a context menu for the Link with a Copy menu item that copies the text to the clipboard
place a Copy (tool) button next to the Link that copies the text to the clipboard
use a Browser widget which is selectable but harder to layout and requires extra work to trigger the functinality when the link is selected
if you don't mind the extra dependency to org.eclipse.ui.forms, use the FormText. The FormText can show hyperlinks and allows to select and copy text
Why not using a StyledText to allow text selection ?
String string = "This is sample text with a link and some other link here.";
final StyledText styledText = new StyledText (shell, SWT.MULTI | SWT.BORDER);
styledText.setText(string);
String link1 = "link";
String link2 = "here";
StyleRange style = new StyleRange();
style.underline = true;
style.underlineStyle = SWT.UNDERLINE_LINK;
int[] ranges = {string.indexOf(link1), link1.length(), string.indexOf(link2), link2.length()};
StyleRange[] styles = {style, style};
styledText.setStyleRanges(ranges, styles);
styledText.addListener(SWT.MouseDown, new Listener() {
#Override
public void handleEvent(Event event) {
// It is up to the application to determine when and how a link should be activated.
// In this snippet links are activated on mouse down when the control key is held down
if ((event.stateMask & SWT.MOD1) != 0) {
try {
int offset = styledText.getOffsetAtLocation(new Point (event.x, event.y));
StyleRange style = styledText.getStyleRangeAtOffset(offset);
if (style != null && style.underline && style.underlineStyle == SWT.UNDERLINE_LINK) {
System.out.println("Click on a Link");
}
} catch (IllegalArgumentException e) {
// no character under event.x, event.y
}
}
}
});
Full example here
#FXML
Private void handleItemBackAction(ActionEvent eve)
{
java.awt.Color color=JColorChooser.showDialog(null,"Select a color",java.awt.Color.CYAN);
String hex = Integer.toHexString(color.getRGB() & 0xffffff);
hex="#"+hex;
Text.setText(hex);
ShortcutButton.setStyle("-fx-background-color: " + hex + ";");
}
When I run this window and click on button at first time color chooser goes behind my actual pane.
When I click on button second time while running it shows at top of all other pane which is correct and so on it works properly.
Then why color chooser not shows in front first time on button click?
The first argument to JColorChooser.showDialog is the parent component of the dialog. You told that method to show the dialog with no parent, so it doesn't know about your other windows.
Instead of using JColorChooser.showDialog, you'll need to embed a JColorChooser instance inside a JavaFX dialog or window:
JColorChooser colorChooser = new JColorChooser(java.awt.Color.CYAN);
SwingNode colorChooserNode = new SwingNode();
colorChooserNode.setContent(colorChooser);
Alert dialog = new Alert(Alert.AlertType.NONE);
// Guarantees dialog will be above (and will block input to) mainStage.
dialog.initOwner(mainStage);
dialog.setTitle("Select a color");
dialog.getDialogPane().setContent(colorChooserNode);
dialog.getDialogPane().getButtonTypes().setAll(
ButtonType.OK, ButtonType.CANCEL);
Optional<ButtonType> response = dialog.showAndWait();
if (response.filter(r -> r == ButtonType.OK).isPresent()) {
int rgb = colorChooser.getColor().getRGB();
String hex = String.format("#%06x", rgb & 0xffffff);
Text.setText(hex);
ShortcutButton.setBackground(new Background(
new BackgroundFill(Color.valueOf(hex), null, null)));
} else {
System.out.println("User canceled");
}
Of course, you're probably better off using ColorPicker in your main window, so you don't have to create an explicit dialog at all:
final ColorPicker colorPicker = new ColorPicker(Color.CYAN);
colorPicker.setOnAction(e -> {
Color color = colorPicker.getValue();
String hex = String.format("#%02x02x02x",
(int) (color.getRed() * 255),
(int) (color.getGreen() * 255),
(int) (color.getBlue() * 255));
Text.setText(hex);
ShortcutButton.setBackground(
new Background(new BackgroundFill(color, null, null)));
});
myLayoutPane.getChildren().add(colorPicker);
As an aside, Java variable names should always start with a lowercase letter, to make them easy to distinguish from class names. Consider changing Text to text, and ShortcutButton to shortcutButton.
Below code only showing the textfield, but I want to include textfield and textarea. Need Help
form1 = new Form("Mobile");
tb2 = new TextField("To: ", "", 30, TextField.ANY);
TextBox tb3 = new TextBox("Message: ", "", 256, TextField.ANY);
form1.append(tb2);
// form1.append(tb3);
form1.addCommand(submitCommand);
display.setCurrent(tb3);
display.setCurrent(form1);
What you call textarea is an lcdui object TextBox; it can not be shown at the same screen as TextField.
If you're interested, refer to 'lcdui' tag info for more details on why is that (there are links to API reference, tutorials, popular libraries etc).
For the code snippet you posted, first thing that comes to mind would be to just replace TextBox to TextField, like
// ...initialization of Form and tb2
TextField tb3 = new TextField("Message: ", "", 256, TextField.ANY);
// above, TextBox has been replaced with TextField
form1.append(tb3); // show "Message" textfield above "Mobile"
form1.append(tb2);
form1.addCommand(submitCommand);
display.setCurrent(form1);
There is no such thing as TextArea in J2ME. You can show either Form with TextField[s] or TextBox, because TextBox is a Displayable. You can show only one Displayable at a time.
I have the following code that displays a JDialog, it shows a text field, I assume it's a JTextField.
How do I set the text in it at the beginning when the dialog opens?
JOptionPane pane = new JOptionPane("", JOptionPane.QUESTION_MESSAGE);
pane.setWantsInput(true);
JDialog dialog = pane.createDialog(null, "Test");
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
dialog.setVisible(true);
Use:
pane.setInitialSelectionValue("foo");
to set the input value that is initially displayed as selected to the user.