libgdx scrollpane doesn't display - java

I'm having issues getting libgdxs scrollpane control to work. The code below shows control setup for a simple layout with a label, a List of items inside a scrollpane, and a button. The problem is my scroll pane doesn't show anything other than the vScroll/vScrollKnob ninepatch (that tiny white square)
it just looks like this:
screenshot.
private void setupLayout()
{
String[] listEntries = {"1","2","3","4","5"};
ListStyle listStyle = new ListStyle();
NinePatch example = new NinePatch(new Texture(Gdx.files.internal("data/example.9.png")));
listStyle.selectedPatch = example;
listStyle.font = new BitmapFont();
mList = new List(listEntries,listStyle);
ScrollPaneStyle paneStyle = new ScrollPaneStyle();
paneStyle.vScroll = example;
paneStyle.vScrollKnob = example;
mListScroll = new ScrollPane(mList,paneStyle);
mListScroll.setScrollingDisabled(true, false);
mListScroll.width = 500;
mListScroll.height = 500;
LabelStyle ls = new LabelStyle();
ls.font = new BitmapFont();
ls.fontColor = new Color(1.0f, 1.0f, 1.0f, 1.0f);
mLabel = new Label("Label", ls);
TextButtonStyle buttonStyle = new TextButtonStyle();
buttonStyle.font = new BitmapFont();
mButton = new TextButton("Button",buttonStyle);
Table table = new Table();
table.add(mLabel);
table.row();
table.add(mButton);
table.row();
table.add(mListScroll);
mStage.addActor(table);
}
It works as expected if i don't user the scroll pane and add the list directly to the table like this:
Table table = new Table();
table.add(mLabel);
table.row();
table.add(mButton);
table.row();
table.add(mList); //changed mListScroll(scrollpane class) to mList(List class)
mStage.addActor(table);
screenshot
But then it will extend out the bottom of my screen when there are too many items. What am I doing wrong here? Is there another parameter i need to set on the scrollpane?

I believe the issue you are having is how you are adding things to the table. I would suggest the following code instead of how you are doing it:
Table table = new Table();
table.add(mLabel);
table.row();
table.add(mButton);
table.row();
// Changing the table layout size itself.
table.add(mListScroll).size(500, 500);
mStage.addActor(table);
For more a more detailed explanation refer to TableLayout the quick start here shows you more how using tables for laying out objects.

Related

Table does not draw following rows

I tried to draw multiple tables with one stage, so far it worked with two tables in different rows, but when I try to add the 3rd or 4th it doesn't work. I tried to draw the tables alone and they work but not in combination which is funny, because they work for 2 Tables....
I tried drawing the tables single alone and they worked but not in combination.
public void setupScreen() {
TextureAtlas menuUi = assetManager.get(Assetdescriptor.backGround);
TextureRegion backGroundRegion = menuUi.findRegion(Regionnames.backGround);
Skin menuSkin = assetManager.get(Assetdescriptor.skinUI);
TextButton team1 = new TextButton("Team 1", menuSkin, "Team1");
TextButton team2 = new TextButton("Team 2", menuSkin, "Team1");
TextButton team3 = new TextButton("Team 3", menuSkin, "Team3");
TextButton team4 = new TextButton("Team 4", menuSkin, "Team4");
TextButton team5 = new TextButton("Team 5", menuSkin, "Team5");
TextButton player1 = new TextButton("Spieler 1", menuSkin);
player1.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
System.out.println("Clicked");
}
});
TextButton player2 = new TextButton("Spieler 2", menuSkin);
TextButton kiPanzer1 = new TextButton("Ki Panzer 1", menuSkin);
TextButton kiPanzer2 = new TextButton("Ki Panzer 2", menuSkin);
TextButton kiPanzer3 = new TextButton("Ki Panzer 3", menuSkin);
Table teamTable1 = new Table();
Table teamTable2 = new Table();
Table teamTable3 = new Table();
Table teamTable4 = new Table();
Table teamTable5 = new Table();
Table TeamOverview = new Table();
Table spieler1= new Table(); // Spieler 1
Table spieler2= new Table(); // Spieler 2
Table Ki3 = new Table(); // Ki Panzer
Table Ki4 = new Table(); // Ki Panzer
Table Ki5 = new Table(); // Ki panzer
spieler1.add(player1);
spieler2.add(player2);
Ki3.add(kiPanzer1);
Ki4.add(kiPanzer2);
Ki5.add(kiPanzer3);
TeamOverview.add(team1, team2, team3, team4, team5);
teamTable1.center();
teamTable1.add(spieler1,spieler2,Ki3,Ki4,Ki5);
teamTable2.center();
teamTable2.add(spieler1,spieler2,Ki3,Ki4,Ki5);
teamTable3.center();
teamTable3.add(spieler1,spieler2,Ki3,Ki4,Ki5);
teamTable4.center();
teamTable4.add(spieler1,spieler2,Ki3,Ki4,Ki5);
Table table = new Table();
table.add(TeamOverview);
table.add(teamTable1).row();
-- -These are the tables which dont work---
table.add(teamTable2).row();
table.add(teamTable3).row();
table.add(teamTable4).row();
-----------------------------------
table.setFillParent(true);
table.setBackground(new TextureRegionDrawable(backGroundRegion));
table.pack();
stage.addActor(table);
}
There is no error message, but there is no change in drawing expected result is 6 rows but there is only 2.
As far as I know you are not allowed to reuse the same object's (spieler1,spieler2,Ki3,Ki4,Ki5). Create unique instance for every time you add them to the different tables.
Check com.badlogic.gdx.scenes.scene2d.Group#addActor
(which Table extends from)
public void addActor (Actor actor) {
if (actor.parent != null) {
if (actor.parent == this) return;
actor.parent.removeActor(actor, false);
}
children.add(actor);
actor.setParent(this);
actor.setStage(getStage());
childrenChanged();
}
A actor can only have one parent, the previous one is removed.
Your table might still have all the rows you want, they are just empty.
You can use setDebug(true) to see the drawing 'lines'.
Edit
This question seems to be a duplicate anyway.
Refer to How to draw several copies of the same actor on one stage at different positions?

Changing Background Color of Row using Google Sheets API v4

I'm trying to change the background color of the first row (header) in my Google spreadsheet. Based on several hours of research and reading I came up with the following code:
Request updateBackgroundColorRequest = new Request();
UpdateCellsRequest updateCellsRequest = new UpdateCellsRequest();
CellFormat cellFormat = new CellFormat();
Color color = new Color();
color.setRed((float) 246);
color.setGreen((float) 178);
color.setBlue((float) 107);
cellFormat.setBackgroundColor(color);
GridRange gridRange = new GridRange();
gridRange.setStartRowIndex(0);
gridRange.setEndRowIndex(1);
gridRange.setStartColumnIndex(0);
gridRange.setEndColumnIndex(14);
updateCellsRequest.setRange(gridRange);
String field = "userEnteredFormat.backgroundColor";
updateCellsRequest.setFields(field);
updateBackgroundColorRequest.setUpdateCells(updateCellsRequest);
requestList.add(updateBackgroundColorRequest);
batchUpdateSpreadsheetRequest.setRequests(requestList);
Spreadsheets.BatchUpdate batchUpdate = service.spreadsheets()
.batchUpdate(spreadsheetId, batchUpdateSpreadsheetRequest);
batchUpdate.execute();
However, although I am able to execute this code successfully (no errors are returned by API) the background color does not change at all (I'm expecting it to change to orange but it remains white).
Did I miss something?
Stucked with the same problem. Thx for your code! It helps me a lot!
So the problem is in line.
updateBackgroundColorRequest.setUpdateCells(updateCellsRequest);
You need RepeatCellRequest class instead of UpdateCellRequest.
Simple request set will be like
requestList.add(new Request().setRepeatCell(
new RepeatCellRequest().
setCell(cellData).
setRange(gridRange).
setFields("*")));
All my code looks like this:
List<Request> requestList = new ArrayList<>();
CellFormat cellFormat = new CellFormat(); //setting cell color
Color color = new Color();
color.setRed(246f);
color.setGreen(178f);
color.setBlue(107f);
cellFormat.setBackgroundColor(color);
CellData cellData = new CellData();
cellData.setUserEnteredFormat(cellFormat);
GridRange gridRange = new GridRange(); //setting grid that we will paint
gridRange.setSheetId(1115615634); //you can find it in your URL - param "gid"
gridRange.setStartRowIndex(0);
gridRange.setEndRowIndex(1);
gridRange.setStartColumnIndex(0);
gridRange.setEndColumnIndex(14);
requestList.add(new Request().setRepeatCell(new RepeatCellRequest().setCell(cellData).setRange(gridRange).setFields("userEnteredFormat.backgroundColor")));
BatchUpdateSpreadsheetRequest batchUpdateSpreadsheetRequest = new BatchUpdateSpreadsheetRequest();
batchUpdateSpreadsheetRequest.setRequests(requestList);
final Sheets.Spreadsheets.BatchUpdate batchUpdate = sheetsService.
spreadsheets().batchUpdate("yourSpreadSheetId - find it in URL", batchUpdateSpreadsheetRequest);
batchUpdate.execute();

Java Swing - JTable Not Showing in Scroll Pane

I created a class Cart and inside is a JTable and two ArrayLists. For some reason, my JTable is not displaying.
Here is my Cart Class:
class Cart {
ArrayList<Product> products = new ArrayList<>(); // Holds the products themselves
ArrayList<Integer> quantities = new ArrayList<>(); // Holds the quantities themselves
JTable prdTbl = new JTable(); // The GUI Product Table
DefaultTableModel prdTblModel = new DefaultTableModel(); // The Table Model
Object[] columns = {"Description","Price","Quantity","Total"}; // Column Identifiers
DecimalFormat fmt = new DecimalFormat("$#,##0.00;$-#,##0.00"); // Decimal Format for formatting USD ($#.##)
Cart() {
setTableStyle();
}
void renderTable() {
// Re-initialize the Table Model
this.prdTblModel = new DefaultTableModel();
// Set the Table Style
setTableStyle();
// Create a row from each list entry for product and quantity and add it to the Table Model
for(int i = 0; i < products.size(); i++) {
Object[] row = new Object[4];
row[0] = products.get(i).getName();
row[1] = products.get(i).getPrice();
row[2] = quantities.get(i);
row[3] = fmt.format(products.get(i).getPrice() * quantities.get(i));
this.prdTblModel.addRow(row);
}
this.prdTbl.setModel(this.prdTblModel);
}
void setTableStyle() {
this.prdTblModel.setColumnIdentifiers(columns);
this.prdTbl.setModel(this.prdTblModel);
this.prdTbl.setBackground(Color.white);
this.prdTbl.setForeground(Color.black);
Font font = new Font("Tahoma",1,22);
this.prdTbl.setFont(font);
this.prdTbl.setRowHeight(30);
}
JTable getTable() {
renderTable(); // Render Table
return this.prdTbl;
}
}
Note: some methods have been removed such as addProduct() and removeProduct(), as I feel they aren't necessary. If you need to see them, please ask.
Here is my initialize() method for the Swing Application Window:
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 590, 425);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Cart cart = new Cart();
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
frame.getContentPane().add(tabbedPane, "cell 0 0,grow");
JPanel cartPanel = new JPanel();
tabbedPane.addTab("Cart", null, cartPanel, null);
cartPanel.setLayout(new MigLayout("", "[grow]", "[][grow]"));
JScrollPane scrollPane = new JScrollPane();
cartPanel.add(scrollPane, "cell 0 1,grow");
table = new JTable();
scrollPane.setViewportView(table);
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String[] item = {"Macadamia", "Hazelnut", "Almond", "Peanut", "Walnut", "Pistachio", "Pecan", "Brazil"};
Double[] price = {2.00, 1.90, 1.31, 0.85, 1.12, 1.53, 1.25, 1.75};
int choice = (int) (Math.random() * item.length);
Product p = new Product(item[choice], price[choice]);
cart.addProduct(p);
table = cart.getTable();
}
});
cartPanel.add(btnAdd, "flowx,cell 0 0");
JButton btnRemove = new JButton("Remove");
cartPanel.add(btnRemove, "cell 0 0");
JButton btnClear = new JButton("Clear");
cartPanel.add(btnClear, "cell 0 0");
}
I'm not sure if I'm missing something here? It has worked fine like this in the past? I've also tried printing out values at table = cart.getTable();, and it seems to be receiving the values fine, so it leads me to believe it has something to do with the Swing initialize() rather than my Cart class, but just in case I posted the Cart class as well.
Are you sure you're adding the right table? Your code shows:
table = new JTable();
scrollPane.setViewportView(table);
I cannot see where's table declared, further more table contains nothing, no rows no columns, while inside actionListener you initialize table with a new instance:
table = cart.getTable();
but the scrollPane holds another instance of JTable.
It looks like you never associate your cart with your cartPanel; I think your problem is here:
JPanel cartPanel = new JPanel();
you make the new panel but never hook your cart to it. Looks good otherwise.
Good luck!

How do I apply multiple layouts in Codename One?

I have been trying to customize the business theme in Codename One. So far I have added extra buttons. Right now I am trying to get those buttons to be constrained by a y- axis boxlayout, but I am currently getting a IllegalArgumentException. I have set the form to a border layout:
Form hi = new Form("Welcome", new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE));
Button Customer = new Button("Customer");
Button gpsProduct = new Button("Find A product Near You");
Button learnMore = new Button("Learn More");
Button Website = new Button("Visit Our Website");
hi.add(BoxLayout.Y_AXIS, Customer).
add(BoxLayout.Y_AXIS, learnMore).
add(BoxLayout.Y_AXIS, gpsProduct).
add(BoxLayout.Y_AXIS, Website);
hi.show();
Box layout Y isn't a constraint for border layout. It's unclear how you wanted this to look but I'm guessing you want something like this which will arrange the components one after the other:
Form hi = new Form("Welcome", BoxLayout.y());
Button Customer = new Button("Customer");
Button gpsProduct = new Button("Find A product Near You");
Button learnMore = new Button("Learn More");
Button Website = new Button("Visit Our Website");
hi.add(Customer).
add(learnMore).
add(gpsProduct).
add(Website);
hi.show();
Here are two nested examples that place the box in a border layout parent:
Form hi = new Form("Welcome", new BorderLayout());
Button Customer = new Button("Customer");
Button gpsProduct = new Button("Find A product Near You");
Button learnMore = new Button("Learn More");
Button Website = new Button("Visit Our Website");
Container box = new Container(BoxLayout.y());
box.add(Customer).
add(learnMore).
add(gpsProduct).
add(Website);
hi.add(BorderLayout.CENTER, box);
hi.show();
This can be written in shorthand as:
Form hi = new Form("Welcome", new BorderLayout());
Button Customer = new Button("Customer");
Button gpsProduct = new Button("Find A product Near You");
Button learnMore = new Button("Learn More");
Button Website = new Button("Visit Our Website");
hi.add(BorderLayout.CENTER,
BoxLayout.encloseY(Customer, learnMore, gpsProduct, Website););
hi.show();

SWT - show multiple table items

Could anybody help me?
I have a table in SWT and simply I want to show some table items but the table only shows one item and the others with vertical scroll. I want to show everything, without scrolling. I tried with the option SWT_NO_SCROLL but is not working. I have a method that creates the table and other that populates it creating a new table item, they´re working good, the problem is that only shows the first item and the other are scrolling.
My code is:
private void createTable(Composite parent){
table = new Table (parent, SWT.BORDER);
TableColumn tcFile = new TableColumn(table, SWT.LEFT);
TableColumn tcStatus = new TableColumn(table, SWT.LEFT);
tcFile.setText("File");
tcStatus.setText("Status");
tcFile.setWidth(500);
tcStatus.setWidth(500);
table.setVisible(true);
table.setHeaderVisible(true);
}
private void populateTable(String file, String status){
TableItem item = new TableItem(table, SWT.LEFT);
item.setText(new String[] { file, status});
}
Composite top = new Composite(parent, SWT.WRAP);
GridLayout layout = new GridLayout();
layout.marginHeight = -5;
layout.marginWidth = 0;
top.setLayout(layout);
Composite banner = new Composite(top, SWT.WRAP);
banner.setLayoutData(new GridData(GridData.FILL, GridData.VERTICAL_ALIGN_BEGINNING, false, false));
layout = new GridLayout();
layout.marginHeight = 0;
layout.marginWidth = 10;
layout.numColumns = 5;
banner.setLayout(layout);
createTable(top);
You haven't specified any layout data for the Table, try something like:
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
table.setLayoutData(data);
If you don't have anything else that is setting the dialog/window size you may need to specify a table height hint:
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
data.heightHint = 200; // Vertical size for table
table.setLayoutData(data);

Categories

Resources