javafx save layout after adding some nodes - java

simply i made little application .. and i attach button in gridpane so when im click the button generate text field .
#FXML
private void msgBtn(ActionEvent e) {
TextField newField = new TextField();
gp.getChildren().add(newField);
}
the question how i could save the text field generated for later open ?!
any way to save the generated node into xml file or some thing so i can recall it in the next start ?
i read a little about serialized but i didn't get how i approach to my goal through it
can any body help or give me some example ?

Related

JavaFX : After setting text in textArea, setting scroll to bottom in separate thread is not working

I created one JavaFX application where I'm updating log with one background process. So I'm setting log text in TextArea and setting scroll to bottom using logs.setScrollTop(Double.MAX_VALUE). but scrollbar is set to little bit up from the bottom.
I also tried TextFlow inside ScrollPan and setting scroll to bottom using logDisplay.setVvalue(1.0). It is also giving the same result.
Platform.runLater(() -> {
logs.setText([setting log text]);//TextArea logs
logs.setScrollTop(Double.MAX_VALUE));
});
//For TextFlow inside ScrollPane
Platform.runLater(() -> {
logs.setText([setting log text]);//Text logs
logDisplay.setVvalue(1.0);
});
I also tried to run code in separate thread like
new Thread() {
public void run(){
System.out.println("called set test");
logs.setText([setting log text]);//Text logs
logDisplay.setVvalue(1.0);
}
}.start();
But nothing is working :(
Can you help me what's wrong in this?
Thanks
--Edit--
Looks like the problem is because of threading issue. Scrollbar value is updating to the previous text value. Actually while retrieving scroll value it's not retrieving latest value but it's getting older value so scrollbar set to end of the previous message, not actual last line.
I don't know the actual problem of this issue, but I found an alternative solution.
I'm setting caret's position at end of text using length of text.
logs.setText(logText);
logs.positionCaret(logText.length());
It is working for me. :)

How to create controls dynamically in JFace Wizard

I have a jFace wizard, I am using this to create a new project type eclipse plugin. As you can see from image below, I have one treeviewer on left side, and a SWT group on right side. What I want is when ever user selects one of the item from treeviewer, I should be able to create dynamic controls on right side SWT Group. Say user selects Test One, one right side I should be able to create few controls like label, text and few radio buttons on right side, similarly if user selects Test Two I should be able to create dynamic controls on right side.
Currently I tried below code:
tree.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
for (int i = 0; i < selection.length; i++) {
String tempStr = selection[i].toString();
tempStr = tempStr.replaceAll("TreeItem \\{", "");
String finalStr = tempStr.replaceAll("\\}", "");
if (finalStr.equals("Test One")) {
Button btn = new Button(g2, SWT.NONE); //g2 is right side group
btn.setText("Blaaaa");
btn.setVisible(true);
container.redraw();
}
}
But when I run, I see no changes on right group. Can anyone guide me what I am doing wrong? Any pointers would be very appreciated, since I am new to Eclipse development and SWT.
You probably didn't set a layout on the g2 group. This is the common cause for controls not showing up. You can also try using g2.layout() to ensure that the new controls are correctly laid out after you create them.
Additionally you could look at using a StackLayout so that once you create a set of controls you can just hide them all at once instead of destroying when the selection changes. This is often useful so that if the user comes back to a previous selection, they will find the data they entered in the same state when they switched the selection. Here is an example.

jComboBox editor returns empty String

I wrote a autocomplete combobox program in which I search for the words entered by the user inside a file. The program works fine, however, the combobox editor doesn't return anything when something is typed in it. I don't know why is that.. Here is the chunk of code that deals with the problem.
// in GUI class constructor
InstantSearchBox = new JComboBox();
InstantSearchBox.setEditable(true);
/*****/
KeyHandler handle = new KeyHandler();
InstantSearchBox.getEditor().getEditorComponent().addKeyListener(handle);
// Keylistener class (KeyPressed method)
try
{
dataTobeSearched = InstantSearchBox.getEditor ().getItem ().toString ();
// the string variable is empty for some reason
System.out.println ("Data to be searched " + dataTobeSearched);
}
catch (NullPointerException e)
{
e.printStackTrace ();
}
Regards
Don't use a KeyListener. The text typed has not beeen added to the text field when at the time a keyPressed event is generated.
The better way to check for changes to the text field is to add a DocumentListener to the Document of the text field. See the section from the Swing tutorial on How to Write a Document Listener for more information.
You should use dataTobeSearched = (String) InstantSearchBox.getSelectedItem();
Despite its name, for editable comboboxes, this method just returns what text is entered.
The editor is only used internally by JComboBox to temporarily capture the input as they are typing. Once they have typed, the editor is cleared down and the text transferred back to the combobox model.
This allows editors to be shared amongst multiple comboboxes all at once - they just jump in when they are needed, capture input, jump back out again and clear down when editing is finished.
Use InstantSearchBox.getSelectedItem() instead of InstantSearchBox.getEditor().getItem().

how to set nextline in textfield

so im creating a chat with a Gui that contains pictures of some men who has a textfield above them that will contain the text that the person chat.
This is by far the hardest project ive created and im quite proud of my accomplisments ive already created a multithreaded server and protocol for my chat client :) ive gotten a guy from Stackoverflow to help me resize my textfield if the text going into it is larger than the size of the textfield :) but now i have another problem when the textfield resizes it resizes only widght because my textfield doesnt change line
ive created the following code to try and make it change lines but it doesnt seem to work could anyone help me?
Send.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
String x = textField_chat.getText();
if (x.length() > 10) {
String oldLine = x.substring(0,5);
String newLineString = x.substring(5,x.length());
txt_ChatPerson1.setText(oldLine+"\n"+newLineString);
}else {
textField_chat.setText("");
txt_ChatPerson1.setVisible(true);
txt_ChatPerson1.setText(x);
}
Use a TextArea instead.
TextArea description from it's javadoc:
Text input component that allows a user to enter multiple lines of plain text. Unlike in previous releases of JavaFX, support for single line input is not available as part of the TextArea control, however this is the sole-purpose of the TextField control. Additionally, if you want a form of rich-text editing, there is also the HTMLEditor control.
I'm taking a shot in the dark here but if you are running this on Windows the new line character is "\r\n".
Try this:
txt_ChatPerson1.setText(oldLine+"\r\n"+newLineString);

Reloading a JTree during runtime

I create a JTree and model for it out in a class separate to the GUI class. The data for the JTree is extracted from a file.
Now in the GUI class the user can add files from the file system to an AWT list. After the user clicks on a file in the list I want the JTree to update. The variable name for the JTree is schemaTree.
I have the following code for the when an item in the list is selected:
private void schemaListItemStateChanged(java.awt.event.ItemEvent evt) {
int selection = schemaList.getSelectedIndex();
File selectedFile = schemas.get(selection);
long fileSize = selectedFile.length();
fileInfoLabel.setText("Size: " + fileSize + " bytes");
schemaParser = new XSDParser(selectedFile.getAbsolutePath());
TreeModel model = schemaParser.generateTreeModel();
schemaTree.setModel(model);
}
I've updated the code to correspond to the accepted answer. The JTree now updates correctly based on which file I select in the list.
From the Component.add API docs.
Note: If a component has been added to
a container that has been displayed,
validate must be called on that
container to display the new
component. If multiple components are
being added, you can improve
efficiency by calling validate only
once, after all the components have
been added.
You have called repaint and validate on a component that is not displayed, which will not be effective. You need to call those methods on the mainPanel after the add. Also revalidate tends to be better than validate as it effectively coalesces.
I not sure that I'm understanding your question, but I'll try...
The right thing to do should be, IMHO:
get the file
create a new TreeModel from your file
give the model to the JTree
In pseudocode, it would look like that:
File newContent = getSelectedByUser(...);
TreeModel newModel = new MyFileBasedTreeModel(newContent);
//this next part must be done in the EventDispatcherThread
myTree.setModel(newModel);
then the JTree would be updated, without any call to repaint, etc.
Hope it helps

Categories

Resources