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?
Related
I'm currently developing an eclipse plugin and in that plugin, there is a form view as a design template. In that form view, I have added a Table and there should have two columns in width ratio of 1:2. And also I want that table to be responsive and dynamically change its column width in order to the formView page width.
The following code segment is the one I'm currently using.
Table table = new Table(parent, SWT.MULTI | SWT.H_SCROLL | SWT.BORDER);
fd = new FormData();
fd.height = 200;
fd.top = new FormAttachment(removeTestCaseButton, 5);
fd.left = new FormAttachment(1);
fd.right = new FormAttachment(99);
table.setLayoutData(fd);
table.setLinesVisible(true);
table.setHeaderVisible(true);
TableColumn column1 = new TableColumn(testCaseTable, SWT.CENTER);
column.setText("column One");
TableColumn column2 = new TableColumn(testCaseTable, SWT.CENTER);
column2.setText("column Two");
form.addControlListener(new ControlAdapter() {
public void controlResized(ControlEvent e) {
Rectangle area = form.getBody().getClientArea();
int width = area.width;
column1.setWidth(width / 3);
column1.setWidth(width * 2 / 3);
}
});
But here the problem is when I open the FormView it works fine. But my table is inside a Section. Once I expand or collapse the Section the table width is getting increased with appearing the horizontal scrollbar.
I just want a solid solution for this.
This is much easier to do using the JFace TableViewer with the TableColumnLayout and ColumnWeightData, but you will have to rework your code to use JFace style content and label providers for the table.
TableColumnLayout tableLayout = new TableColumnLayout();
// A separate composite containing just the table viewer is required
Composite tableComp = new Composite(parent, SWT.NONE);
tableComp.setLayout(tableLayout);
TableViewer viewer = new TableViewer(tableComp, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
TableViewerColumn col1 = new TableViewerColumn(viewer, SWT.LEAD);
col1.getColumn().setText("Column 1");
col1.setLabelProvider(.... label provider for column 1 ....);
// Weight for column
tableLayout.setColumnData(col1.getColumn(), new ColumnWeightData(60));
TableViewerColumn col2 = new TableViewerColumn(viewer, SWT.LEAD);
col2.getColumn().setText("Column 2");
col2.setLabelProvider(....... label provider for column 2 .....);
// Weight for column
tableLayout.setColumnData(col2.getColumn(), new ColumnWeightData(40));
viewer.getTable().setHeaderVisible(true);
viewer.getTable().setLinesVisible(true);
viewer.setContentProvider(ArrayContentProvider.getInstance());
viewer.setInput(.... input data for the viewer ....);
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!
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);
The code below displays 2 JTables.
As they both will have exactly the same headers I wanted, for the sake of efficiency, to reuse the header from the first table.
However running the code results in the header appearing in the second table but not in the table it came from originally.
I am less interested in work-arounds, but - for the sake of learning and understanding - more interested in finding out why the header does not appear in the first table.
Here is the code:
public class HeaderTest1 {
public void doTheTest() {
JFrame testFrame = new JFrame("Header Test");
JPanel pane = new JPanel();
Container theContentPane = testFrame.getContentPane();
BoxLayout box = new BoxLayout(pane, BoxLayout.Y_AXIS);
pane.setLayout(box);
theContentPane.add(pane);
String theData[][]
= {
{"One", "two", "3"},
{"four", "5", "six"},
{"7", "8", "9.0"},
{"£10.00", "11", "twelve"}
};
String columnNames[] = {"Column 1", "Column 2", "Column 3"};
JTable firstTable = new JTable(theData, columnNames);
JScrollPane thisScrollPane = new JScrollPane(firstTable);
JTableHeader thisTableHeader = firstTable.getTableHeader();
pane.add(thisScrollPane);
buildTheSecondTable(thisTableHeader, firstTable, columnNames, pane);
testFrame.pack();
testFrame.setVisible(true);
}
private void buildTheSecondTable(JTableHeader headerFromTheFirstTable,
JTable firstTable, String[] columnNames, JPanel pane) {
JTable secondTable = new JTable();
int columnCount = columnNames.length;
JScrollPane thisScrollPane = new JScrollPane(secondTable);
secondTable.setTableHeader(headerFromTheFirstTable);
Object[][] emptyData = new Object[1][columnCount];
for (int n = 0; n < columnCount; n++) {
emptyData[0][n] = "";
}
DefaultTableModel thisTableModel = new DefaultTableModel();
thisTableModel.setDataVector(emptyData, columnNames);
secondTable.setModel(thisTableModel);
secondTable.setLayout(firstTable.getLayout());
secondTable.setCellEditor(firstTable.getCellEditor());
pane.add(thisScrollPane);
}
public static void main(String[] args) throws SQLException, ParseException {
HeaderTest thisTest = new HeaderTest();
thisTest.doTheTest();
}
Any advice would be appreciated
A Swing component can only have a single parent so you can't share the table header component.
You can however share the Array of column names:
JTable firstTable = new JTable(theData, columnNames);
In your buildTheSecondTable method you have access to the array of column names so just use:
//DefaultTableModel thisTableModel = new DefaultTableModel();
DefaultTableModel thisTableModel = new DefaultTableModel(columnNames);
Then you can add data to the model and the model to the table.
Then reorder the code to create the JScrollPane after you add the model to the table.
Also, get rid of the table.setLayout() code. You would never use a layout manager on a table. You don't add components to the table. The table renders the data itself without using real components.
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.