swing component not fully functioning into javafx application - java

I have two separate based on Swing and Javafx. Now i need to open Swing application inside Javafx Tab pane by below code
SyntaxTester ob = new SyntaxTester(filepath);
SwingNode swingnode = new SwingNode();
JComponent jcomp = new JComponent() {
};
jcomp.add(ob.getContentPane());
swingnode.setContent(jcomp);
BorderPane borderpane = new BorderPane(swingnode);
tab.setContent(borderpane);
Basically this Swing application is JEditorPane based editor. Swing application is added and working successfully inside Tab pane but the issue is that, there is hinting feature and after selecting text from hint, editor looses it's cursor and user again manually click on the editor. Although Swing application separately working fine. Please help me with resolve it. Thanks in advance.
This is what happening when user select from hint list.
if (jLstItems.getSelectedIndex() >= 0) {
result = jLstItems.getSelectedValue().toString();
} else {
result = jTxtItem.getText();
}
char pressed = evt.getKeyChar();
if (pressed != '\n') {
result += (pressed == '\t') ? ' ' : pressed;
}
setVisible(false);
target.replaceSelection(result);

Related

JavaFX ToggleButton is not visible but registered as Children

im creating a JavaFX based Discord Bot where you can choose which Guilds are allowed to listen to Commands, and it creates as many ToggleButtons as the amount of Servers the Bot is connected to.
Here is my Method:
public void generateButtons() {
int y = 14;
discordVBox = new VBox();
JFXToggleButton tglBtn;
discordVBox.setSpacing(30);
for (final Guild g : DiscordBot.jda.getGuilds()) {
if (g == null || g.getTextChannels().isEmpty() || !DiscordBot.isDiscordBotOnline()) {
PrintConsole.printError("Error creating Toggle Buttons.");
return;
} else {
tglBtn = new JFXToggleButton();
tglBtn.setText(g.getName());
tglBtn.setStyle("-jfx-toggle-color: #d35400;");
tglBtn.setLayoutX(14);
tglBtn.setLayoutY(y);
tglBtn.setPrefHeight(56);
tglBtn.setPrefWidth(141);
discordVBox.getChildren().add(tglBtn);
y += 63;
System.out.println("DISABLED: " + tglBtn.isDisabled());
System.out.println("VISIBLE: " + tglBtn.isVisible());
System.out.println("PARENT: " + tglBtn.getParent());
tglBtn.setVisible(true);
}
}
System.out.println("VBOX PARENT: " + discordVBox.getParent());
System.out.println("VBOX CHILDREN: " + discordVBox.getChildren());
}
Ignore all the outputs, i was trying to debug all of them and per code it seemed fine, but no visible to the eye.
The Scene itself is mostly via FXML and im using a Custom Library for JavaFX called Jfoenix
The Method "generateButtons" is executed as soon the Discord Bot goes online.
It's actually the NEW VBox you created that is not visible.
If you loaded the scene with FXMLLoader, all of the scene contents were created for you and references were provided to the loaded objects. Use the VBox that you specified with the FXML code instead of creating a new one.
You can remove line 3 of your sample code and this should run.
discordVBox = new VBox(); // <- don't need this, there is already an VBox instantiated

How to make the text in an SWT Link widget selectable

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

OpenSuse 12.3 + Java Dual Display

I have a Java application that displays two JFrames on two separate monitors. On Ubuntu and Windows the application displays just fine. I can configure the JFrames to display on the monitors with the specified screen ID. However on openSUSE it keeps displaying on the same monitor regardless of the setting. What is different to openSUSE?
Here is some of the code that I use to determine on which monitor the JFrame must display:
GraphicsDevice[] screens = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
for (int s = 0; s &lt screens.length; s++) {
GraphicsConfiguration configuration = null;
for (int c = 0; c &lt screens[s].getConfigurations().length; c++) {
if (AWTUtilities.isTranslucencyCapable(screens[s].getConfigurations()[c])) {
configuration = screens[s].getConfigurations()[c];
break;
}
}
if (configuration == null) {
configuration = screens[s].getDefaultConfiguration();
}
if (screens[s].getIDstring().equals[frame1_id]) {
frame1 = new JFrame("Frame 1", configuration);
frame1.setResizable(false);
frame1.setUndecorated(true);
frame1.setBounds(configuration.getBounds());
frame1.setVisible(true);
}
if (screens[s].getIDstring().equals[frame2_id]) {
frame2 = new JFrame("Frame 2", configuration);
frame2.setResizable(false);
frame2.setUndecorated(true);
frame2.setBounds(configuration.getBounds());
frame2.setVisible(true);
}
}
The OpenSuse implementation of GraphicsEnvironment may depend on the choice of a particular window manager. You'll have to experiment to find the optimal one.
Addendum: #bouncer comments, "I have used the Gnome window manager, which caused the problem. After switching to KDE, the problem was solved." See also 10 things to do after installing openSUSE 12.3.

Programatically open a menu from menubar(Simulate the menu click programmatically for UI automation test)

MenuManager and MenuContribution items has been already created.
For the input Menu Item id/label, I need to problematically drop-down/open/display a menu item from menubar in Eclipse. I think I may need to fire some event.
This is requirement for UI Automation that Menu should be drop down automatically.
Can you please help at the earliest. I'm trying following, but here not sure how to set the x & y co-ordinates where mouse click event should be fired.
Code:
String toCompare = "File";
Menu menu = window.getShell().getMenuBar();
if(menu!=null && !menu.isDisposed()){
MenuItem[] items = menu.getItems();
for(int i=0;i<items.length;i++){
String menuText = LegacyActionTools.removeMnemonics(items[i].getText());
if(toCompare.equalsIgnoreCase(menuText)){
Event event = new Event();
event.doit = true;
event.widget = items[i];
event.type = SWT.MouseDown;
event.button = 1;
boolean success = items[i].getDisplay().post(event);
System.out.println("Could we generate the event ? "+success);
}
}
}
Why don't you use dedicated tools for UI testing, such as SWTBot. It seems typicall matche what you would do

Getting my scrollPane to scroll under programatic control

I have a Groovy app which uses a scrollPane built via swing builder:
BinsicWindow(def controller)
{
controlObject = controller
swinger = new SwingBuilder()
mainFrame = swinger.frame(
title: "Binsic is not Sinclair Instruction Code",
size:[640, 480],
show:true,
defaultCloseOperation: WindowConstants.DISPOSE_ON_CLOSE){
scrollPane(autoscrolls:true) {
screenZX = textArea(rows:24, columns:32) {visble:true}
}
screenZX.setFont(new Font("Monospaced", Font.PLAIN, 18))
}
}
I add text to the textArea programatically (i.e. no user input) and I would like the textArea to scroll down automatically as content is added. But the view remains fixed at the top and I can only see the bottom (once the screen is more than full) by dragging the mouse.
Can I fix this? I have been searching for an answer to this for a wee while now and getting nowhere. Apologies if it's a simple answer.
The following lines should scroll your textarea to the last text position:
rect = screenZX.modelToView(screenZX.getDocument().getLength() - 1);
screenZX.scrollRectToVisible(rect);

Categories