Scene2d ScrollPane not scrolling - java

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.

Related

LibGDX Change my ScrollPane's startposition

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.

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.

How can a scrollpane of jtable show whole data vertically without scrolling

I have a Jtable and it enclose in a default scrollpane. When my table grow up with its row, a vertical scrollpane appear. But i don't want it. How can i show whole row vertically without vertical scrollbar.
After loading the data into the TableModel you can set the preferred size of the viewport:
JTable table = new JTable(model);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane( table );

Java - JScrollPane Doesn't align with JTable

I am trying to make a JTable that has a width of 500. The problem is that the JScrollPane associated with the table doesn't appear next to the table.
Here is the relevant code:
// Create authorsPanel
JPanel authorsPanel = new JPanel(new BorderLayout(5,5));
authorsPanel.setBorder( new TitledBorder("Author Selection") );
// Configure author table
DefaultTableModel authorstableModel = new DefaultTableModel(new String[80][1], new String[]{"Authors"}) {
#Override
public boolean isCellEditable(int row, int column) {
//all cells false
return false;
}
};
JTable authorsTable = new JTable(authorstableModel);
authorsTable.getColumnModel().getColumn(0).setPreferredWidth(500);
authorsTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
try {
authorsTable.setAutoCreateRowSorter(true);
} catch(Exception continuewithNoSort) {
}
JScrollPane tableScroll = new JScrollPane(authorsTable);
Dimension tablePreferred = tableScroll.getPreferredSize();
tableScroll.setPreferredSize(new Dimension(500, tablePreferred.height/3));
authorsPanel.add(tableScroll);
Here is a screenshot:
When I get rid of the line:
authorsTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
Then the table returns to being the full width of the panel, so it seems like I need this line.
The java docs on BorderLayout states :
The components are laid out according to their preferred sizes and the
constraints of the container's size. The NORTH and SOUTH components
may be stretched horizontally; the EAST and WEST components may be
stretched vertically; the CENTER component may stretch both
horizontally and vertically to fill any space left over.
You have used authorsPanel.add(tableScroll) to add the JScrollPane to the JPanel. So you are basically adding it to the center. So this is going to occupy the whole space that is lying vacant.
The solution to your problem lies in choosing a different layout. I could suggest MigLayout which is very versatile and you can get all kinds of effects using it.
You add your scroll pane to a panel with BorderLayout. BorderLayout does not care about preferred size. Use e.g GridBagLayout with proper constraints or you can BoxLayout (with horizontal flow) placing the table first and an empty panel second as place holder.

Jtable in BoxLayout

I have a BoxLayout (in a panel of a BorderLayout) in which I've put a JTable and a button vertically. I would like the table to have a fixed width and that the height should resize itself up to filling the panel. At the moment, I'm fine with the displayed width but not that it's filling up the whole panel height. For example, if there are zero data in the table, I would like only the column names to be shown. I've tried things like setViewportView() and setPreferredSize(), but can't really make it work. Any suggestions? Is it in the layout or scrollpane?
String[] columnNames = { "Date", "Type"
};
Object[][] data = {
};
JTable myTable = new JTable(data, columnNames);
JButton okButton = new JButton("OK");
JPanel panWest = new JPanel();
panWest.setLayout(new BoxLayout(panWest, BoxLayout.PAGE_AXIS));
JScrollPane scrollPane = new JScrollPane(myTable);
panWest.add(scrollPane);
panWest.add(okButton);
EDIT
This is what ended up working:
Dimension dimension = new Dimension();
dimension.height = (myTable.getRowCount()*myTable.getRowHeight())+
myTable.getTableHeader().getPreferredSize().height;
dimension.width = myTable.getColumnModel().getTotalColumnWidth();
scrollPane.setMaximumSize(dimension);
I would like the table to have a fixed width and that the height
should resize itself up to filling the panel.
and
I have a BoxLayout (in a panel of a BorderLayout) in which I've put a
JTable and a button vertically.
there are three (mentioned only simple) ways
use built_in LayoutManager for JPanel - FlowLayout (FLowLayout.CENTER),
override FLowLayout.LEFT or RIGHT in the case that you want to aling,
JComponent laid by FlowLayout never will be resizable without programatically to change XxxSize and notified by revalidate & repaint,
set proper size for JScrollPane by override JTable.setPreferredScrollableViewportSize(new Dimension(int, int));,
JScrollPane accepts this Dimension as initial size, required is usage of JFrame.pack(), before JFrame.setVisible(true)
BoxLayout accepts min, max and preferred size, override all these three sizes for JScrollPane
(half solution, but most comfortable) change LayoutManager for JPanel to BorderLayout,
put JScrollPane with JTable to BorderLayout.EAST/WEST area,
then override JTable.setPreferredScrollableViewportSize(new Dimension(int, int));,
JScrollPane will occupy whole area for BorderLayout.EAST/WEST and will be resizeable only on heights coordinates
use MigLayout, here are a few posts about MigLayout and JScrollPane (with JTextArea, JTable)

Categories

Resources