Add horizontal scroll Libgdx - libgdx

I want to design my levels on a horizontal pane what i am doing is:
container = new Table(skin);
container.setBounds(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
levelselect = new Table(skin);
levelselect.setBounds(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
// list = new List(skin,"1");
scrollPane = new ScrollPane(levelselect);
scrollPane.layout();
levelselect.align(Align.left);
for(int i=1;i<20;i++){
levelnumber = new Label(" "+i,levelnumbertext);
levelselect.add(levelnumber);
}
container.align(Align.bottom);
container.add(scrollPane).width(400).height(400);
container.add(levelselect);
any suggestions?? thanks in advance.

Using a ScrollPane and Table for this sounds like a lot of unnecessary overhead, they have a lot going on in the background, dynamically calculating their own layouts etc, you might not need this if you just want a full screen side scroller. You might be best just positioning your Actors on the Stage, however far along they need to be and update the Camera position to suit. Stage has culling options which I think by default are enabled, so it won't be drawing any of your Actors that are off the screen.

Related

Jscrollpane in matlab

I am trying to use some java gui in my matlab code.
I want to create a Jpanel containing lots of buttons , and add this Jpanel to a JscrollPane to be able to scroll up and down, right and left through the Jpanel.
I tried using JavaComponent() function as described in : http://undocumentedmatlab.com/blog/javacomponent
here is my code:
[jpanel1, hpanel1] = javacomponent('javax.swing.JPanel');
[jButton1, hButton1] = javacomponent('javax.swing.JButton');
[jscroll, hscroll] = javacomponent('javax.swing.JScrollPane');
jButton1.setText('Click again!');
set(hButton1,'position',[5 5 50 50])
set(hpanel1,'position',[50 50 500 500],'BackgroundColor','white');
jpanel1.add(jButton1);
jscroll.add(jpanel1);
The panel and button are created but I can't find the scrollpane, tried setting the jscroll to visible with no results.
WHat am I missing out??
You only need to use javacomponent once, to display the outer-most java container, i.e. JScrollPane in your case. Just assemble your components inside JPanel container and then pass that to JScrollPane constructor.
Note that it is safer to create your objects with javaObjectEDT so that subsequent method calls run on EDT - otherwise you could face a deadlock / race condition.
Finally, note how you can use getpixelposition and 'normalized' units for the container created by javacomponent to make your JScrollPane fill the entire parent drawing area, and behave better on resizing.
jButton1 = javaObjectEDT('javax.swing.JButton', 'Button 1');
jButton2 = javaObjectEDT('javax.swing.JButton', 'Button 2');
jPanel = javax.swing.JPanel();
jPanel.add(jButton1);
jPanel.add(jButton2);
jScrollPane = javax.swing.JScrollPane(jPanel);
hFig = figure();
hParent = uicontainer('Parent',hFig);
parentPixelPos = getpixelposition(hParent);
pos = [1,1,parentPixelPos(3),parentPixelPos(4)]; % fill the parent uicontainer completely
[~, hContainer] = javacomponent(jScrollPane, pos, hParent);
set(hContainer, 'Units', 'normalized'); % better behavior on resizing
jscroll.add(jpanel1);
You should never add components to a scroll pane. A JScrollPane has its own custom layout manager to display the scrollbars and the viewport.
So instead you need to add the panel to the viewport:
jscroll.setViewportView( jpanel1 );
However, this may still not work as the following code looks like it is trying to set the size/location of the component which implies a null layout is being used:
set(hButton1,'position',[5 5 50 50])
Normally it is the responsibility of the layout manager to determine the size/location of a component and the scrollbars of the scrollpane will only be displayed is the preferred size of the panel is greater than the size of the scrollpane.
I don't know that the benefit of using MatLab is. I suggest you just use normal Swing. See examples from the Swing tutorial on Using Layout Managers.

Libgdx Can you set the bounds of a ScrollPane?

So recently i discovered Scrollpanes and have been trying implement one properly.
After a bit of searching i found this section of code
public void create() {
batch = new SpriteBatch();
skin = new Skin(Gdx.files.internal("data/uiskin.json"));
stage = new Stage();
gamelog = new Label("Start", skin);
gamelog.setAlignment(Align.center);
gamelog.setWrap(true);
Table scrollTable = new Table();
scrollTable.add(gamelog);
scrollTable.row();
scroller = new ScrollPane(scrollTable);
Table table = new Table();
table.setFillParent(true);
table.add(scroller).fill().expand();
Now for the most part this code works well. a scrollpane is actually created and i can dynamically add text to it. my issue however is the size of the scrollpane takes up the whole screen, and initial text is centered in the middle of the scrollpane(so in the middle of the screen) and only starts to scroll once enough text has been added to take up the whole height of the screen.
Rather than expanding on its own what i actually want is for the scrollpane to take up just a small section at the bottom of the screen.
I have tried setting the bounds of the tables and of the scrollpane but the closest i have come is merely moving the scrollpane downwards(meaning the last text added is off screen and not in view. Its s if it completely ignores any width or height set and just expands on its own.
I figure i could get the desired effect by simply using a set of labels at the bottom of the screen and change their text dynamically but then the scroll feature would be lost. so before i do this i was hoping someone could shed some light on wether or not it is possible to resize and set a scrollpanes bounds?
Table uses cells to size its components. When you add a component to a table it returns the table's cell which holds that component. You can then use the cell to set the size of the component.
In your original code try this:
table.add(scroller).size(460, 200);
And if you want to change the scrollpane size after adding it to the table, you can get the cell from the table and then set its size like this:
table.add(scroller);
...
table.getCell(scroller).size(460, 200);
And here is the API for Cell:
https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/ui/Cell.html
I stumbled across a solution with trial and error.
the following code works for me and allows the scrollpane to be resized and positioned. The main culprit seems to be the use of table.setFillParent(true); and the order in which you try to set the parent tables bounds.
batch = new SpriteBatch();
skin = new Skin(Gdx.files.internal("data/uiskin.json"));
stage = new Stage();
gamelog = new Label("Start", skin);
gamelog.setAlignment(Align.center);
gamelog.setWrap(true);
Table scrollTable = new Table();
scrollTable.add(gamelog);
scrollTable.row();
scroller = new ScrollPane(scrollTable);
Table table = new Table();
table.setBounds(10, 10, 460, 200);
table.add(scroller).fill().expand();
scrollTable.debug();
scroller.debug();
table.debug();
stage.addActor(table);

How to align table in scrollPane top?

I' am making achievements screen in my libgdx game. I want to align my table in scrollPane to top, now it is centred vertically and horizontally and I don't know why. Using .top() method when I've tried to create new row didn't worked.
stage = new Stage();
Gdx.input.setInputProcessor(stage);
outerTable = new Table();
innerTable = new Table();
innerTable.setFillParent(true);
for(int i=0;i<score_bound; i++){
if(i<score_state){
innerTable.row().width(achie_width).height(achie_height).top();
innerTable.add(new Image(ltm.assets.score_textures[i]));
}else{
innerTable.row().width(achie_width).height(achie_height).top();
innerTable.stack(new Image(ltm.assets.score_textures[i]),new Image(ltm.assets.kurtyna));
}
}
for(int i=0;i<letin_bound; i++){
if(i<letin_state){
innerTable.row().width(achie_width).height(achie_height).top();
innerTable.add(new Image(ltm.assets.letin_textures[i]));
}else{
innerTable.row().width(achie_width).height(achie_height).top();
innerTable.stack(new Image(ltm.assets.letin_textures[i]),new Image(ltm.assets.kurtyna));
}
}
scrollPane = new ScrollPane(innerTable);
outerTable.setPosition(0, 0);
outerTable.setSize(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
outerTable.debug();
outerTable.add(scrollPane).fill().expand();
stage.addActor(outerTable);
I run into the same issue today and came across here, so I might as well post an answer here in case somebody is still looking for an answer.
#peterSweter said top() does not work, what #Rara suggested is the same as calling top.
The only way to achieve something that looks like an align to the top - which only is relevant if the innerTable is smaller - is to increase the size by adding empty elements around it, so the table you pass to the ScrollPane has at least the same size as the scrollPane.
I just wrapped yet another Table around my innerTable reference.
(658 is the fixed height I use for my ScrollPane)
Table spacingTable = new Table();
spacingTable.setHeight(Math.max(innerTable.getHeight(), 658));
spacingTable.add(innerTable);
if (innerTable.getHeight() < 658) {
spacingTable.row();
spacingTable.add().expandY();
}
ScrollPane scroll = new ScrollPane(spacingTable, skin);
In order to achieve what you want, you can wrap your table inside another table and use that table for ScrollPane. Note that when adding your table to container table, you'll need to add some growable, empty cell at the bottom and right side in order to "push" it to top-left (although I believe you can also simply use containerTable.add(myTable).top().left() - but haven't tested it really).
This of course is a much better solution than second's solution, which uses hardcoded height. The thing is, you will usually want to add scroll pane as growable (.grow()), so fixed height/width is useless.

Is JPanel supposed to be added to a JFrame?

I have this code which I don't understand:
JFrame jframe = new JFrame("Risultati protocolli cercati");
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel body = new JPanel();
Container c = jframe.getContentPane();
body.setSize(100, 100);
body.setLayout(new GridLayout(1000, 1));
for (int i = 0; i < 1000; i++)
body.add(new JLabel("JLabel " + i));
JScrollPane jsp = new JScrollPane(body);
c.add(jsp);
jframe.setSize(100, 100);
//jframe.add(body);
jframe.setVisible(true);
If I leave the penultimate line commented then everything appears, both labels and scroll. Instead if I uncomment that line, I see nothing. Only the JFrame. Why does it happens? For the main window of my program I had to perform jframe.add(body)...
Instead if I uncomment that line, I see nothing. Why does it happens?
The below line
jframe.add(body);
internally calls
jframe.getContentPane().add(body);
JFrame by default uses BorderLayout and add(component) method directly add it in the center section, if you add again then last one is replaced with latest one.
You can use overloaded add() method of JFrame to add it in another section east, west, north and south as shown in below snapshot:
for e.g.:
frame.add(comp,BorderLayout.NORTH); // add in north section
frame.add(comp,BorderLayout.WEST); // add in west section
You can use other Layout Managers as well as per the design or your application:
It's worth reading How to Use BorderLayout
One more suggestion:
Use frame.pack() instead of frame.setSize() that fits the components as per component's preferred size.
Interesting problem caused by the layout managers.
A component can only have a single parent. First you add the "body" to the scrollpane but then the "body" gets removed from the scrollpane when you add it to the content pane of the frame (for the reasons mentioned by #braj). Not a big deal as it just means you won't see any scrollbars.
Since the component is directly added to the content pane you should still see the labels however they do not display and this is the confusing part. Change your code to use "100" for the GridLayout and the number of components you create in the loop. When the frame first displays the panel will be empty. Now, increase the height of the frame and you will see the components appear. What is happening is that you are trying to paint too many components in a small area and because of rounding issues the height of every component becomes 0, so there is nothing to paint. When you increase the height to at least 100 pixels every component can now be 1 pixel high so you get garbage.
The only solution is to keep the "body" panel in the scrollpane so that all components will be displayed at their preferred size. Then you can scroll through all the components as required.
A tip for when using a GridLayou. You can use:
body.setLayout(new GridLayout(0, 1));
Then means the grid will have unlimited rows and a single column.

Reusing objects app mobile

I am making a mobile application in as3. I'm optimizing.
I have a scroll containing 30 objects (buttons). All buttons are the same except its title.
This bucle is inside the container scroll:
for(var a:int=0; a<global.get_A.get_B.length; a++)
{
b = new ItemList(global.get_A.get_B[a]);
b.y = this.height;
addChild(b);
b.mouseChildren = false;
}
http://help.adobe.com/en_US/as3/mobile/WS4bebcd66a74275c3-576ba64d124318d7189-7ffe.html talking about the reusing objects, but not for interactive objects.
Is it possible?
You cannot reuse them if they need to be visible at the same time. Besides, if you're only creating them once (on init) there should be little performance benefit of reusing them. Just make sure you remove them from the display list when they are outside of the visible scroll area, which should improve performance at least a little.