I have a FlowPane that I would like to go to a new line every time I add a new child (another entry of text)
Text text1 = new Text("Here is some text");
Text text2 = new Text("This should be on a new line");
flowPane.getChildren().add(text1);
flowPane.getChildren().add(text2); //should go to the next line
This just keeps appending the text next to each other like so:
"Here is some textThis should be on a new line"
Does anyone out there know of a way to fix this? It seems like it should be a straight forward part of the API, but I can't find anything relevant to this problem.
From the api: A horizontal flowpane (the default) will layout nodes in rows, wrapping at the flowpane's width.. So basically, by default, all your components will be added in the same row until there's not enough space available.
You want to either change the orientation, or use another layout.
Related
I want to have an image, on top of which I can mark points (which I should be able to drag across the image).
The image is also going to be bigger than the canvas, so I want to be able to drag it as well, but while doing so keeping it in the background, and moving all the marks on it along with it.
I thought using a Group, disabling the controls of all objects and disabling selection for the Canvas would do the trick:
fabric.Object.prototype.hasControls = fabric.Object.prototype.hasBorders = false;
let fabricCanvasObj = new fabric.Canvas(canvas);
fabricCanvasObj.scene = new fabric.Group([new fabric.Image(img)]);
fabricCanvasObj.add(fabricCanvasObj.scene);
fabricCanvasObj.selection = false;
But now I want that on mouse:down a new point will be added, unless the mouse is over an already created point, so that when the user want to move that mark, no new mark is created in its place, and I want to move that individual point instead of the whole group.
But the event.target is the whole group, not the individual object in that group.
I've then followed the solution here: FabricJS Catch click on object inside group
Which suggested using
{subTargetCheck: true}
But this causes an exception to be thrown once I add an object to the group after it's been created.
How should I go about this?
I am new to javafx.
I am trying to build a messenger where the chatting panel show messages.
I want to align the messages like my own messages are on left and other messages are on right.
To display the message i am using TextFlow where I add Text. But the alignment isnt working.
TextFLow tf=new TextFlow();
Text t1= new Text("Hi");
Text t2= new Text("Hello");
t1.setTextAlignment(TextAlignment.RIGHT);
t2.setTextAlignment(TextAlignment.LEFT);
tf.getChildren().addAll(t1,t2);
But the alignment not working. Both the text are on left.
What should I do?
You can set the TextAlignement from the TextFlow but, i don't think we could have two alignment in the same container, however, you can use a tricky way to skirt this problem by using Labels & VBox:
private VBox Chat(){
VBox chat = new VBox();
chat.setPrefSize(400, 400);
chat.setStyle("-fx-background-color:#333333;");
Label txt1 = new Label("Text1");
txt1.setTextFill(Color.WHITE);
txt1.setPrefWidth(400);
txt1.setAlignment(Pos.CENTER_LEFT);
Label txt2 = new Label("Text2");
txt2.setTextFill(Color.WHITE);
txt2.setPrefWidth(400);
txt2.setAlignment(Pos.CENTER_RIGHT);
chat.getChildren().addAll(txt1,txt2);
return chat;
}
Why Label ? This node work in the same way as the Text node but the advantage is that wet can resize the background.
Why VBox ? is optional, even if i think TextFlow is better suited to textual nodes. It is also for the positioning, VBox is more suited for classified items from top to bottom chatting panel. Good luck !
I'm new to Unity and I started with the Catch Game Tutorial to learn to create a 2D game. Everything is working now but for my needs, I would like to add different textboxes to each of the fallen elements (in the tutorial the bowling balls) Those texts should move with the objects. I know I can change text dynamically in the code, but I couldn't figure out how to correctly add the element.
I tried to add a text object as a child of the gameobject and also tried to create a new gameobject which contains a text, but I can't position the element in front of the background and above my wished element (I can't choose a sorting layer for this)
Imagine this is the object which has to be collected and I would like to show a text like this:
My Questions are:
1. How can I add a text to be displayed in the correct position (GUIText, text in a gameobject, only text or something else?
2. How can I make this text dynamically move with the fallen object?
3. Can I set a background to my text as displayed above?
Thank you in advance!
I found a solution for my problem and did the following thing:
I created a Sprite with my wished background (black rectangle) and added it to the scene
I created a 3D Text and added it as a child to the created Sprite (and scaled it and positioned it)
I added a Script to the 3d Text with the following content:
void Start () {
this.gameObject.GetComponent<Renderer>().sortingLayerName = "[MyLayerName]";
this.gameObject.GetComponent<Renderer>().sortingOrder = 3;
}
I added the Sprite (with the text as child) to my gameObject (ananas)
A renewed the object in the Prefabs folder
Maybe my solution helps other people facing a similar problem.
I'm stuck while importing twowayview as module to my project.
.
Next button is not getting enabled, is it the correct way to do it, or there is another way.
please help.
I guess wanting to import the library TwoWayView is because you want to create lists and horizontal and vertical grids. If so I recommend you use the called RecyclerView, that can do everything you do with TwoWayView, i.e. can create lists and grids in horizontally and vertically.
Here you can get information about RecyclerView use in horizontal and vertical:
http://www.androidhive.info/2016/01/android-working-with-recycler-view/
Also I have to say I had some bugs with the code shown on the page AndroidHive.
It turns out that in the onCreate() method of MainActivity.java the method that is called prepareMovieData() is declared in last place, when it should be called right after declaring RecyclerView, ie, right after this line:
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
If this call is declared it in last place, a mistake appear that the object that we created (in the case of AndroidHive is the Movies class) will appear this to null, ie, it has no data.
Moreover, at the end of prepareMovieData() method the following line states:
mAdapter.notifyDataSetChanged();
For me personally, this line failure in the application that causes the App is closed, eliminating this code works perfectly provoked me. Actually, not that it serves this line, but I would appreciate if anyone knows why, to edit this entry explaining it.
Finally, if the intent of this post was to create a RecyclerView horizontally, then I leave the explanation for a RecyclerView is horizontal or vertical.
In the onCreate() method of file MainActivity.java we add a LayoutManager to RecyclerView with the following lines:
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
This Layout Manager is responsible to tell the RecyclerView as the data that we introduce will be displayed, ie, if the content is vertically, horizontally, in a list, etc .. So if our list is displayed horizontally only we need to edit the above lines by the following:
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(mLayoutManager);
If you want the list vertically, we leave the lines as they are.
Hope this can help you.
Color newColor = new Color(197,222,90);
JButton newButton;
newButton = new JButton(icon);
newButton.setBacgroundColor(newColor);
When it is pressed it changes color. How can I keep it from changing color? I have multiple buttons, so if there is solution in one or two rows please help me, and keep in mind that I'm beginner, writing some huge classes won't help me, because I have multiple buttons with different names to be affected with this.
EDIT: Solution in one line is:
UIManager.put("Button.select", newColor);
But it changes all button colors but I need another to have different a color.
EDIT2: After some research I figured out there isn't an easy solution (but it should be). How I see it I have 2 solutions, 1. is to break buttons to separate classes and set UIManager for them, and second is to make custom buttons. It is just too much work for button.
I've found nothing that can change that particular behavior on a normal JButton. The problem being, that whatever you write in your actionlistener for the button, will occur AFTER you've let go of the mousebutton, and not "while clicking".
There are workarounds, however.
My preferred choice is, to remove all graphics from the button, and then add your own images to the button's regular and pressed states. You could take a screenshot of your GUI, cut out the button, and set that image to be both states.
JButton myButton = new JButton();
// Sets button x, y, width, height. Make the size match the image.
myButton.setBounds(5, 30, 100, 30);
// Remove border-graphics.
myButton.setBorder(null);
// Remove default graphics from the button
myButton.setContentAreaFilled(false);
// Remove the focus-indicating dotted square when focused (optional)
myButton.setFocusPainted(false);
// Here, myImage is a simple BufferedImage object.
// You can set one like this, provided you have an "images" package,
// next to your main class (ex: com.somecompany.someprogram.images),
// that contains an image:
BufferedImage myImage = ImageIO.read(getClass().getResource("images/myImage.png"));
// Then we simply apply our image to both states for the button, and we're done.
myButton.setIcon(new ImageIcon(myImage));
myButton.setPressedIcon(new ImageIcon(myImage));
Obviously there are many ways to retain and load an image, but since that's not the issue here, I'll leave additional methods out of it.
There's no need to go through it all countless times, though. It should be pretty easy to write your own custom implementation of the JButton class, in which a custom constructor takes a single parameter, being the BufferedImage, and then the constructor sets it up accordingly (changes the icons). Then all you have to do when you create a new JButton, is to use your own class, and pass it an image:
JButton btn = new MyCustomJButton(myImage);
You could also easily get along with very few images. All you need is a HashMap which holds all the images, with a String as a key. Imagine you need 4 OK-buttons. You make a single image of a button with the text "OK" written on it. Then you put that image into the HashMap, like so:
myMap.put("OK", myImage);
Then you could do this when creating a button, over and over again if you'd like more:
JButton btn = new MyCustomJButton(myMap.get("OK"));
Alternatively:
Another way of achieving this, which is pretty elaborate, but probably considered "the right way", is to use ButtonUI, as presented in this answer to another post.
If the OP is referring to the temporary change of background colour on a button with an icon at the moment the mouse is pressed, the following statement does the trick:
button.setContentAreaFilled(false);
"If you wish to have a transparent button, such as an icon only button, for example, then you should set this to false."
This took me a long time to figure out. It seems to be a little known technique, perhaps since its name gives little clue as to its effect.
With only first lane we can still see that it is clicked. You need to combine those two:
button1.setContentAreaFilled(false);
button1.setEnabled(false);
and if you don't wanna in grey color you put another button under him.
panelname.add(button1,+5,+5); \\(first not clicable, not visible button, notice +5)
panelname.add(button2,-5,-5); \(-5,-5 means it is 5 points under panel)