LibGDX Change my ScrollPane's startposition - java

My scrollpane is fully working if you want to scroll from top to bottom. My problem is when I call a setScreen command I want my camera/screen to start at a specific section on the scrollpane, for example at
table.add(menuButton1).row();
Or at the bottom of my ScrollPane. I have read through the Libgdx ScrollPane document, but I can't find a solution to my problem.
My scrollpane at the moment:
camera = new OrthographicCamera(500,600);
stage.getHeight();
stage.getWidth();
extendViewport =new ExtendViewport(500,1500, camera);
stage=new Stage(extendViewport);
Table scrollableTable = new Table();
scrollableTable.setFillParent(true);
stage.addActor(scrollableTable);
Table table = new Table();
table.add(label2).spaceBottom(1200).row();
//table.add(smile).spaceBottom(2300).row();
table.add(label).spaceBottom(2300).row();
table.add(menuButton1).row();
//table.add(topbutton).spaceBottom(2300).row();
table.add(label3).spaceBottom(2300).row();
table.pack();
table.setTransform(true); //clipping enabled
stage.setDebugAll(true);
table.setOrigin(table.getWidth()/2,table.getHeight()/2);
// table.setScale(.5f);
final ScrollPane scroll = new ScrollPane(table);
scrollableTable.add(scroll).expand().fill();
Gdx.input.setInputProcessor(stage);

ScrollPane.scrollTo works well, but as you use a table, you need to validate ScrollPane before calling it.

Related

Libgdx table setBackground does nothing

I made .pack file and load it to skin. I can set it as drawable for Image class and it works fine. But Table.setBackground do nothing.
Drawable background = skin.getDrawable("tooltip_background");
tooltipGroup.setBackground(background);
Why this code not work?
Full code:
tooltipGroup = new Table(skin);
tooltipGroup.setWidth(w/6);
Label.LabelStyle headerStyle = new Label.LabelStyle(headerFont, Color.GREEN);
Label headerLabel = new Label(effect.getName(), headerStyle);
headerLabel.setWidth(w/6);
headerLabel.setX(20);
headerLabel.setWrap(true);
headerLabel.setAlignment(Align.topLeft);
tooltipGroup.addActor(headerLabel);
Label.LabelStyle style = new Label.LabelStyle(font, Color.WHITE);
Label descriptionLabel = new Label(effect.getDescription(), style);
descriptionLabel.setWidth(w/6);
descriptionLabel.setWrap(true);
descriptionLabel.setAlignment(Align.topLeft);
descriptionLabel.setPosition(20, -descriptionLabel.getHeight());
tooltipGroup.addActor(descriptionLabel);
tooltipGroup.setPosition(mouseX, h - mouseY);
Drawable background = skin.getDrawable("tooltip_background");
tooltipGroup.setBackground(background);
stage.addActor(tooltipGroup);
Don't use addActor on the Table. In order to add Actors to the table, use the add method, and specify custom parameters like fill or width / height of the actors in chaining methods. Like this, you can control how you add the actors to the table, since the table does auto layouting.
Table rootTable = new Table();
rootTable.setFillParent(true);
TextField fieldFirst = new TextField("First", skin);
TextField fieldSecond = new TextField("Second", skin);
rootTable.add("First:");
rootTable.add(fieldFirst).width(100).row();
rootTable.add("Second:");
rootTable.add(fieldSecond).width(100);
stage.addActor(rootTable);
As you can see, this allows you to specify the width, in the table, with the expand(), and fill() methods, you can even have an element fill all the available space within one row. Once you use this auto layouting of your table, the table should have the appropriate size, and your background should show. More on how to use the libGdx table layouting can be found here.

Libgdx Scene2d Table and Scrollpane bug?

I tried to do a simple scrollable table with buttons. Here is the code:
craft = new Stage();
craftbl = new Table();
craftbl.setDebug(true);
craftbl.add(button1);
craftbl.row();
craftbl.add(button2);
scroll = new ScrollPane(craftbl);
scroll.setSize(640, 480);
craft.addActor(scroll);
But as a result I have only one button on screen:
How can i fix it?
Solved. To fix this, you need to set size at least of one of table's cells. E.G. like this: table.add(somewidget).width(100);

Scene2d ScrollPane not scrolling

I create a ScrollPane, with table inside. It works fine, but when I add this ScrollPane to layout Table (or VecrticalGroup) it stops working.
layoutTable = new Table();
groupTable = new Table();
SP = new ScrollPane(groupTable, skin);
SP.setWidth(1105);
SP.setHeight(300);
groupTable.top();
groupTable.left();
layoutTable.add(inputTable);
layoutTable.row();
layoutTable.add(SP);
layoutTable.row();
layoutTable.add(resultTable);
layoutTable.setFillParent(true);
stage.addActor(layoutTable);
Once you add it to the table, the table manages its size, so your earlier calls (SP.setWidth() and SP.setHeight()) are overwritten (probably with whatever is the prefSize of the scroll pane's contents, which is zero if it's an empty table). If you want to control the scroll pane's size, do it by adjusting its cell when you add it to the table:
layoutTable.add(SP).width(1105).height(300).fill();
The fill() call tells the table to resize the scroll pane to fit the cell dimensions.

Adding JScrollPane to JTable doesn't show up

I'm pretty new to Javas swings, so sorry if this is something trivial. This is the constructor, I excluded unnecessary form items. (I tried running the code as short as this, but the problem still appears)
//This just opens a connection to MySQL server, this doesn't create any problems.
bp = BazaPodataka.getBaza();
//Forming the main frame..
JFrame frame = new JFrame();
{
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setBounds(d.width/2 - sirina/2, d.height/2 - visina/2, sirina, visina);
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout(0, 0));
//Adding a layered pane so I can place items inside the form more 'freely'
JLayeredPane layeredPane = new JLayeredPane();
frame.getContentPane().add(layeredPane, BorderLayout.CENTER);
//Adding a table
JTable table = new JTable();
String[] rowData = {"Name:", "Price:", "Cathegory:", "Sum:"};
DefaultTableModel model = new DefaultTableModel(rowData, 0);
JScrollPane skrol = new JScrollPane(table);
table.setModel(model);
//The 2 lines below work as intended
ResultSet rs = (ResultSet) bp.query("SELECT * FROM table"); //This calls a query
popuniTabelu(rs, model); //This populates the table.
table.setBounds(10, 110, 500, 350);
table.setEnabled(false);
table.setShowHorizontalLines(false);
layeredPane.add(table);
Populating the table and displaying it isn't the problem, there's enough information inside the table 'table' that the user even needs to scroll down.
But that's where the problem begins, the scroll doesn't show up. How do I implement it from here. I tried following solutions I found on google, but they pretty much sum up on:
JScrollPane scroll = new JScrollPane(table);
Which simply doesn't work in my case.
The problem may be trivial, if it is, I'm sorry, I'm still learning swing. Also, sorry for my bad english, it's not my native language. :)
Also, if there's something I forgot to include, please let me know.
Thank you!
You add your table to 2 components :
JScrollPane skrol = new JScrollPane(table);
and
layeredPane.add(table);
Because of Swing component can have just one parent component second statment override first, so your JScrollPane is empty. Seems you need to remove layeredPane.add(table);
As mentioned here
Each GUI component can be contained only once. If a component is already in a container and you try to add it to another container, the component will be removed from the first container and then added to the second.

Scrolling issue (Java-JFrame-JScrollBar)

INTRO:
I created a java application using JFrame. I have a JMenuBar at the top and under that I'd like to display rows of text.
PURPOSE:
When I have 50 rows and only 20 are displayable at once, I'd like to be able to scroll down and back up again.
PROBLEM:
Of course, my theory doesn't wanna work as it should. My problem is that I don't know how to add a vertical scroll properly.
QUESTION:
How should I change this code to reach my goal?
public void display(){
Container content = this.window.getContentPane();
content.setLayout(new BorderLayout());
Border border = LineBorder.createGrayLineBorder();
//this is just a sample
for(int i = 0;i<50;i++){
JLabel lab = new JLabel("lonyaladek");
lab.setSize(570, 20);
lab.setBorder(border);
lab.setLocation(10, 20+(i*25));
content.add(lab);
}
//scroll
JScrollBar sb = new JScrollBar(JScrollBar.VERTICAL, 0, 0, 0, 0);
content.add(sb);
}
First you need to start with a layout manager that allows you to add multiple components to the container. Maybe a GridLayout is the best place to start.
Then you add this container to the scrollPane and then you add the scrollpane to the window.
So the basic code would be:
JPanel panel = new JPanel( new GridLayout(0, 1) );
panel.add(...);
panel.add(...);
JScrollPane scrollPane = new JScrollPane( panel );
window.add(scrollPane, BorderLayout.CENTER);
I suggest you read the section from the Swing tutorial on How to Use Scroll Panes for more info.

Categories

Resources