libGDX - How to fill Table to Cell size? - java

I want to make sub Table in a Cell to fill to that cell as a parent.
// Main Table
Table mainTable = new Table();
// Sub Table
Table subTable = new Table();
mainTable.add( ).expand().row(); // cell-1
mainTable.add(subTable).expand().row(); // cell-2
mainTable.add( ).expand().row(); // cell-3
The Code result:
When I make subTable.setFillParent(true), it will fill to mainTable. Ok its well.
But I want to make subTable fill to expand size of the cell-2.
I mean:
How can I make this?

Check LibGDX wiki: https://github.com/libgdx/libgdx/wiki/Table#fill
The fill method causes a widget to be sized to the cell. Like expand,
there are also fillX and fillY methods.
table.add(nameLabel).expand().bottom().fillX();
...

Related

iText 7 borderless table (no border)

This code below does not work.
Table table = new Table(2);
table.setBorder(Border.NO_BORDER);
I am new to iText 7 and all I wanted is to have my table borderless.
Like how to do it?
The table itself is by default not responsible for borders in iText7, the cells are. You need to set every cell to be borderless if you want a borderless table (or set the outer cells to have no border on the edge if you still want inside borders).
Cell cell = new Cell();
cell.add("contents go here");
cell.setBorder(Border.NO_BORDER);
table.addCell(cell);
You could write a method which runs though all children of a Table and sets NO_BORDER.
private static void RemoveBorder(Table table)
{
for (IElement iElement : table.getChildren()) {
((Cell)iElement).setBorder(Border.NO_BORDER);
}
}
This gives you the advantage that you can still use
table.add("whatever");
table.add("whatever");
RemoveBorder(table);
instead of changing it on all cells manual.

Updating a table's cell with an ImageButton in LibGDX?

I have a class that initiates a table with 3 cells filled like so:
[leftButton] [ImageButton] [rightButton]
ImageButton is created & added to characterSelection array:
Texture characters2 = TrafficGame.res.getTexture("chevy");
characterImageStyle = new ImageButton.ImageButtonStyle();
car = new TextureRegion(characters2, 32, 32);
characterImageStyle.imageUp = new TextureRegionDrawable(new TextureRegion(car));
characterImageChevy = new ImageButton(characterImageStyle);
characterSelection.add(characterImageChevy);
Adding to table when player selection is initiated:
table.add(characterSelection.pop());
Now when the user clicks the left/right button, moveLeft/Right() gets called:
public void moveLeft(){
table.getChildren().get(1).clear();
}
This where I'm stuck. It obviously clears the cell but I have no idea how to add an ImageButton back to it from the array.
Table doesn't support swapping actors into the same cell. Instead of clearing the image button, you should hide it. When you're ready for it to show another image, change its image and show it.
characterImageChevy.hide();
((TextureRegionDrawable)characterImageChevy.getStyle().imageUp).setTexture(...);
characterImageChevy.show();
You can directly change the contents of a Scene2D cell. You can obtain the Cell by asking the table to return Cell for a particular Actor. After that you can clear the Cell and set new Actor for its contents.
// first, grab the Actor that you wish to replace
ImageButton actor = characterImageChevy; // you know where to get it from
// next, take the Cell where it's at
Cell<Table> cell = table.getCell(characterImageChevy);
// remove the actor
cell.clearActor();
// set new Actor
cell.setActor(characterImageChevy);

jTable function getSelectedRow(), getSelectedColumn() returning -1

I am new in Java programming. I need to get the indices of selected column and row. I am getting -1 as selected indices for both the column and row. I have searched for a solution but didn't find anything satisfactory.
My code is following:
private void deleteProductButtonActionPerformed(java.awt.event.ActionEvent evt) {
DefaultTableModel tableModel = (DefaultTableModel) this.productDisplaTable.getModel();
JTable table = new JTable(tableModel);
int selectedRowIndex = table.getSelectedRow();
int selectedColIndex = table.getSelectedColumn();
System.out.println(selectedRowIndex );
System.out.println(selectedColIndex);
}
You're checking if a row is selected before the JTable has been displayed before the user can even interact with it.
Instead why not have that code in an ActionListener or some other listener so that the user at least has a chance to select something? This suggests that you might have a misunderstanding on how event-driven programming works and need to study the concepts a little bit more.
What makes you think that creating a a new JTable would have any selected rows or columns
JTable table = new JTable(tableModel); //???
Try using a table that is actually visible to the user instead
In your code you create a new JTable but you don't add this component to any container. Thus it won't never be visible and no row nor column could ever be selected.
Now, while we can add components dynamically in Swing we tipically place all our components before the top-level container (window) is made visible. In this case you should place the table when you initialize your components (don't forget the scroll pane) and do whatever you need to do when the button is pressed.
On the other hand I'm not sure what are you trying to achieve. I mean you already have a table called productDisplaTable. If you want to print the selected row and column in that table then make this little change:
private void deleteProductButtonActionPerformed(java.awt.event.ActionEvent evt) {
//DefaultTableModel tableModel = (DefaultTableModel) this.productDisplaTable.getModel();
//JTable table = new JTable(tableModel);
int selectedRowIndex = this.productDisplaTable.getSelectedRow();
int selectedColIndex = this.productDisplaTable.getSelectedColumn();
System.out.println(selectedRowIndex );
System.out.println(selectedColIndex);
}
Thanks all for taking time to reply.
I got the answer I was looking from #dic19's comment.
Now I clearly see the mistake I was doing. This was due to my lack of knowledge in Java programming.

libgdx, table in scrollpane

I'm trying to make a shop for my game.
In this shop, there should be 2 scroll pannel : one with your iventory's stuff, the other one with what the dealer sells.
(It 'd like to make it this way)
http://i.stack.imgur.com/5gLUr.jpg
So at first, I tried to put a list in my ScrollPane, but I can't put both image of my weapons and text in those.
So I tried to put a table in my ScrollPane : as long as I have stuff in my inventory, I add cells with the item's texture, another one with the name of the weapon, ...)
But there's no way I found to select the table ...
So I'm a bit confused : if I use a List, I can't put texture and make it look fancy, but with Tables, I can't select the items ...
Have you guys got any ideas of how I should do that ?
Thanks !
EDIT : Here is some code, but no everything since it's pretty ugly !
Table test3 = new Table(skin2);
for(WeaponElement weapon : GameScreen.dataLoader.getWeaponArray()){
Image img = new Image(new Texture(weapon.getIconPath()));
test3.add(img).width(32);
test3.add().width(10);
test3.add(weapon.getName()).align(Align.left).row();
}
panelShop = new ScrollPane(test3, skin2);
panelShop.layout();
So basically, I add all my weapons into my test3 table rows add it into my ScrollPane, but i'd like to make them selectable, with I can do by using lists but if I do so, I can't use anymore tables to make it look great...
What about using Buttons to push? The example below shows a table with one button a row. Use a Button with the text you want to display and add a picture beside it.
Table scrollableTable = new Table(skin);
scrollableTable.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
for(WeaponElement weapon : GameScreen.dataLoader.getWeaponArray()){
final TextButton button = new TextButton(wapon.name, skin, "default");
button.addListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
// do stuff
});
scrollableTable.add(button).center();
// add a picture
Image img = new Image(new Texture(weapon.getIconPath()));
scrollableTable.add(img).width(32);
// finish a row
scrollableTable.row();
}
Try Tree ui component and use it as table. It is selectable and accepts Actor as node (use Table as row layout).
See com.badlogic.gdx.tests.TestTree (link) example.

Java Swing JTable select programmatically multiple rows

I have a JTable with multiple rows and every row is presented via Point on a scatter plot. What I have to do is when a given point is selected on the scatter plot I have to associate this selection with selecting of the corresponding row in the JTable.
I have an Integer that represents, which row I have to highlight.
What I tried is:
JTable table = new JTable();
...
...// a bit of code where I have populated the table
...
table.setRowSelectionInterval(index1,index2);
So the problem here is that this method selects all rows in the given range [index1,index2]. I want to select for example rows 1,15,28,188 etc.
How do you do that?
To select just one row, pass it as both the start and end index:
table.setRowSelectionInterval(18, 18);
Or, if you want to select multiple, non-contiguous indices:
ListSelectionModel model = table.getSelectionModel();
model.clearSelection();
model.addSelectionInterval(1, 1);
model.addSelectionInterval(18, 18);
model.addSelectionInterval(23, 23);
Alternately, you may find that implementing your own subclass of ListSelectionModel and using it to track selection on both the table and the scatterplot is a cleaner solution, rather than listening on the scatterplot and forcing the table to match.
It also works without using the ListSelectionModel:
table.clearSelection();
table.addRowSelectionInterval(1, 1);
table.addRowSelectionInterval(15, 15);
table.addRowSelectionInterval(28, 28);
...
Just don't call the setRowSelectionInterval, as it always clears the current selection before.
There is no way to set a random selection with a one method call, you need more than one to perform this kind of selection
table.setRowSelectionInterval(1, 1);
table.addRowSelectionInterval(15, 15);
table.setRowSelectionInterval(28, 28);
table.addRowSelectionInterval(188 , 188 );
And So On....
Here is a generic method to accomplish this:
public static void setSelectedRows(JTable table, int[] rows)
{
ListSelectionModel model = table.getSelectionModel();
model.clearSelection();
for (int row : rows)
{
model.addSelectionInterval(row, row);
}
}

Categories