LibGdx widget position - java

Hello I'm new in LibGdx ! How can I remove this space ! :Window
level = new Label( mapHelper.getVillage().getLevel(),skin);
leveltext = new Label("Level:",skin);
upgradeButton = new TextButton("incrase Level",skin);
// adding to my coustom Window
this.add(leveltext).align(Align.bottomLeft);
this.add(level);
this.add(upgradeButton);

The problem is that your window resizes to its content. And above your 3 actors, there is more content which stretches the width of the window causing the gap.
If you want all 3 actors aligned to the bottom left you could probably, just before adding the actors to the window, call:
this.defaults().align(Align.bottomLeft);
Another thing you can do is enable debug lines.
To do this, first call:
table.setDebug(true);
Then in your render method, after drawing the stage, call:
Table.drawDebug(stage);
This will display lines showing bounds, margins and spacing to help you understand what is going on more easily.
More on tables and debugging here.

Related

Changing view of JScrollPane so that a certain part of JEditorPane is visible

How do I make a certain line of text in a jEditorPane visible when it is in a JScrollPane?
private JEditorPane myEditorPane = new JEditorPane();
private JScrollPane myScrollPane = new JScrollPane(myEditorPane);
myEditorPane.setContentType("text/html");
myEditorPane.setText("<html>" + getMyString(x) + "</html>");
myEditorPane.repaint();
getMyString gets a long String with many lines, separated by \n.
The programme has 2 panels. The programme then goes down the lines in the above panel and for each one underlines the text in that line and displays a related image in another panel. Each is viewed for 1 second then moves on to underline the next line of text and show the next image. I've got it going down and underlining them in turn, displaying the relevant images for each underlined line of text. But the scrollpane jumps to the start every time.
I think I've got to use scrollRectToVisible on the viewport, but how do I find out what the rectangle is for part of the string in the JEditorPAne?
I've been able to manipulate scroll bar position by directly calling setValue on the scroll bar.
myScrollPane.getVerticalScrollBar().setValue()
The question is, what value to set? Can you assume that the lines of text are the same? If they wrap, things are going to get complicated. I'm going to assume they don't, and that the height of each line is the same because the font and size is the same. Then you'll need to know what index the current displayed line is, and the total count of lines.
Once you've got that, you convert the line index to editor Y position by multiplying the line index by editor height over line count and set accordingly.
myScrollPane.getVerticalScrollBar().setValue( (int) indexOfCurrentLine * myEditorPane.getHeight() / countOfLines );
Obviously haven't been able to test any of this without an SSCCE.

placement of dynamically added buttons in javafx

I have dynamically created a bunch of buttons. I gave a fixed width to each of them. I want it to be added until space is available to its parents and when it overlaps with some other nodes, it will automatically go to the next line in order to avoid collision. I want it to be responsive at different screen sizes.
for(Data i:datas){
Button btn=new Button(i.getName());
anchorPane.getChildren().addAll(btn);
}
I want to arrange the buttons in a new line only when the buttons in the first line have covered its total width. How can I accomplish it? Any help is appreciated.
James_D provided a great resource—there's a built in layout for anything you need and FlowPane has what you want here.
Using your example, just swap out your AnchorPane for FlowPane:
FlowPane flowPane = new FlowPane();
for(Data i:datas){
Button btn=new Button(i.getName());
flowPane.getChildren().addAll(btn);
}
To make it responsive (i.e. expand and contract with the parent container/stage), bind the FlowPane's width and height to those of its parent:
flowPane.prefWidthProperty().bind(stage.widthProperty());
flowPane.prefHeightProperty().bind(stage.heightProperty());

LibGDX how to make scene2D notification label scroll inside container

I am making a small game with LibGDX, and for this game I need a small container, which is a scene2D Stack, and inside that container is a label.
My problem is, that this label may only be on one line, and if the label text is longer than the container width, then it has to slowly move/scroll from right to the left, so the player can see the entire notification text. and when all the text has been shown, then the sliding should start over.
The best example I can come up with, for the behavior, is a digital text sign like in the image below
Hope anyone has some guides or can show me some code, to get me going :)
If it was me, I'd create a table (label container) and a label(text) and change the position of the label accordingly and then reset it. Respecting all of the label.getwidth and so on

Scene2D UI not filling whole screen

In the level selection screen I'm currently working on I can't seem to make the Scene2D UI table fill the whole screen.
I've omitted a few bits from the code to reduce the length, but here is the relevant code:
Stage stage = new Stage(new ScreenViewport());
table = new Table();
table.setFillParent(true);
innerTable = new Table();
//Omitted: Add a button to innerTable for each level
ScrollPaneStyle scrollPanelStyle = new ScrollPaneStyle();
scrollPanelStyle.vScrollKnob = new BaseDrawable();
scrollPane = new ScrollPane(innerTable);
scrollPane.setScrollingDisabled(true, false);
scrollPane.setupOverscroll(30f, 30f, 150f);
//Temporarily a restart icon
Image backButton = new Image(backButtonTexture);
backButton.setScaling(Scaling.fit);
table.add(backButton).height(75f).width(75f).pad(20, 0, 20, 0).left();
table.row();
table.add(scrollPane);
It currently looks like this. The icon isn't as far to the left as I would think it would be with the current code. As far as I'm concerned it should be at 0 on the X axis.
The same thing is true for the innerTable with the ScrollPane containing all the levels, it doesn't fill the entire screen on the X axis, but it looks like I want it, so it's not really a problem there.
Answer:
Adding expandX() to my ScrollPane ended up doing the trick, resulting in this line: table.add(scrollPane).expandX();. A big thanks to flogy.
I guess the table is stretched properly to your viewport but there is some other misconfiguration. I recommend using the debug mode to get more details (see https://github.com/libgdx/libgdx/wiki/Table#debugging - also use on the back button etc). If you can't find a solution using this method by yourself, please update the screenshot, so we can investigate further. Thanks.
Final solution (see comments):
table.add(scrollPane).expandX();
The table was already filling the whole screen but the contained scroll panel was not. The table cells adapt to their content by default. By using expandX, we could finally expand the width of the cell that contains the scroll panel to the full table width.

Table control with sliding row headers in JavaFX

The album view in iTunes has a slick effect where the album title and cover art stay in view at all times. If you slide down the screen they stay pinned to the top of the screen until they bump into the next album, then they slide away.
Notice how the top album is still fully visible even though the user has scrolled down a ways.
What is this control or effect called? I'm coming up with blanks trying to Google for it.
How can I do this in JavaFX? I want to mimic this in my Java-based GUI. Can TableView do this, or maybe some third-party control?
The easiest way to do this is with a ScrollPane. Inside your ScrollPane you define your rows and their layouts (probably each row is an HBox containing an ImageView and a TableView which is set to the height of the ImageView). Then, the TableViews inside your ScrollPane need to let the ScrollPane override their scrolling - that is, their onScroll bubbles up to the ScrollPane.
Then you override the onScroll behavior for your ScrollPane. The algorithm for the scrolling could go like this:
There are two modes.
1) Scrolling IN an album scrolls the TableView in that row. If the scrolling goes beyond the boundaries of the TableView's scrollHeight (the range between 0 and scrollHeight), then the mode switches to scrolling TO an album.
2) Scrolling TO an album scrolls the ScrollPane an amount up to the height of the current row. Scrolling an amount greater than the current row's height moves to the next album and switches the mode back to Scrolling IN that album.
3) Edge Cases: Scrolling within the ScrollPane beyond the boundaries of the ScrollPane's scrollHeight (the range between 0 and scrollHeight) immediately moves to the next album and switches the mode back to Scrolling IN that album.
I'd give a code example, but I've never actually seen anybody try to do this. I just know you CAN do it.
I would recommend you to take a look to the SpreadsheetView in ControlsFX
The SpreadsheetView allow you to fix at the top of the screen any number of line. So you would have the first part of your behavior.
Regarding the fact of bumping into another, it would be more difficult but not impossible with the SpreadsheetView.
Anyway, if you want to implement that behavior in the TableView, you will find very useful tricks in the SpreadsheetView code.
This is how a section title acts as default in a UITableView.
https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UITableView_Class/index.html
This should help you on how to create and use UITableView

Categories

Resources