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);
Related
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.
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.
I'm trying to set a cell width in my LibGDX's table and it's ignoring the value I pass. I suspect it is because it's under rows that are fill and has colspan but I can't explain exactly why It is happenning.
Here's a screen:
I would like the cell of my play button to be smaller in width, so I wrote that code:
setDebug(true, true); //just mentionning, this is in the constructor of a class that extends Table, to prevent having table creation code on my screen
setWidth(400f);
setHeight(80f);
padLeft(30f);
padRight(30f);
Image image = new Image(assets.getTexture("gfx/menu/level-online.png")); //TODO icon depends on state
image.setScaling(Scaling.fill);
Button playButton = new TextButton(level.getType() == LevelType.RACE ? "Play" : "Visit", skin);
Button editButton = new TextButton("Edit", skin);
add(level.getName()).bottom().left().colspan(2).expand();
add(image).width(30f).height(30f);
row();
add(level.getOwner()).top().left().colspan(3).expand();
row();
add(playButton).width(40f).center(); //HERE
add(editButton).left().colspan(2);
As you can see, the width of the cell of the play button is supposed to be 40 but it's way more. (For reference, the cloud is 30 x 30) How can I fix this ?
Fixed it using a table in my last row to separate elements. Thanks to Xoppa and Tenfour04 for their help.
add(level.getName()).bottom().left().colspan(2).expand();
add(image).width(30f).height(30f);
row();
add(level.getOwner()).top().left().colspan(3).expand();
row();
Table buttons = new Table();
buttons.add(playButton).padRight(10f);
buttons.add(editButton);
add(buttons).expand().colspan(3).left();
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.
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.