Why is my Libgdx ScrollPane not scrolling horizontally? - java

Hi I am trying to build a horizontal scrolling screen in my libgdx game but can only get it to scroll vertically rather than horizontally for some reason. Although the ScrollPane sounds simple I don't fully understand it for some reason but a scrolling credits example on here helped me a little.
I have a table and have added rows of text to it. The table matches the size of the screen and it also contains a ScrollPane called scroller which fills the size of the table. I am guessing since the rows of text don't fit on the screen and therefore the scroller, the scroller automatically allows vertically scrolling in the credits example? However when I change the text to one long line of text rather than new lines the scroller doesn't allow me to scroll horizontally to view all the text why is that? Surely the text is going outside the scroller width?
scrollTable = new Table(); //create table
scrollTable.add(text); //add a long line of text bigger than screen
scrollTable.row();
scrollTable.add(text2);
scrollTable.row();
scrollTable.add(text3);
scroller = new ScrollPane(scrollTable); //create the ScrollPane
Table table = new Table();
table.setFillParent(true);
table.add(scroller).fill().expand();
//scroller.setWidth(500);
//scroller.setScrollingDisabled(false, true);
stage.addActor(table);
private static final String reallyLongString =
"This is a really long string that has lots of lines and repeats itself over and over again This is a really long string that has" +
" lots of lines and repeats itself over and over again This is a really long string that has lots of lines and repeats itself over and over"+
" again This is a really long string that has lots of lines and repeats itself over and over again"+
" This is a really long string that has lots of lines and repeats itself over and over again This is a really long string that has lots"+
" of lines and repeats itself over and over again";
//This is my long string of text.

Try this:
Table table = new Table();
ScrollPane scroll = new ScrollPane(table);

Related

How to start scrollpane without padding at top in Libgdx?

In libgdx I have a ScrollPane in my application and I can't figure out how to get it to scroll without extra space at the top. Currently, there is a massive space at the top of the pane, and when I scroll down, the text (high scores) slowly fill up the space.
BEFORE SCROLLING:
AFTER SCROLLING:
Is it possible to start without the space (so the pane is completely filled) and when I scroll, I can scroll through the high scores without them moving upwards?
I noticed I can accomplish this by removing the following line.
table.setTransform(true);
But if I do this, I cannot scale or set origin on the table so the text appears too large.
Below is my scrollpane creation method:
private void createScrollPane(ArrayList<String> scrollPaneScores) {
scrollPaneViewport = new FitViewport(800, 480);
stage = new Stage(scrollPaneViewport);
Skin skin = new Skin(Gdx.files.internal("skin/uiskin.json"));
Table scrollableTable = new Table();
scrollableTable.setFillParent(true);
stage.addActor(scrollableTable);
Table table = new Table();
for (String score : scrollPaneScores) {
table.add(new TextButton(score, skin)).row();
}
table.pack();
table.setTransform(true);
//table.setTransform(true); //clipping enabled ... this setting makes scrolling not occur in uniform fashion
table.setOrigin(400, 0);
table.setScale(0.75f);
final ScrollPane scroll = new ScrollPane(table, skin);
//scrollableTable.add(scroll).expand().fill();
scrollableTable.add(scroll).maxHeight(300);
//stage.setDebugAll(true);
//When stage is created, set input processor to be a multiplexer to look at both screen and stage controls
inputMultiplexer.addProcessor(inputProcessorScreen);
inputMultiplexer.addProcessor(stage);
Gdx.input.setInputProcessor(inputMultiplexer);
scrollPaneCreated = true;
}
Remove the setFillParent line. It is an old problem with tables inside scrollpanes.
Try removing the scrollableTable.setFillParent(true);

Scroll pane isn't coming down when add components

I have this simple program where I upload images into a java db database (just add their location to the data base as well as their type) then I show them in the program in a scrollpane the problem is the scrollpane isn't scrolling down,
this is my code
`
JPanel cont = new JPanel();
cont.setPreferredSize(new Dimension());
cont.setLayout(new FlowLayout(FlowLayout.LEFT));
while(sports_re.next()){
String location=sports_re.getString("LOCATIONN");
for(int i=0;i<100;i++){
JLabel picLabe = new JLabel();
picLabe.setIcon(new ImageIcon(new
ImageIcon(location).getImage().getScaledInstance(100, 100,
Image.SCALE_SMOOTH)));
cont.add(picLabe);
}
}
jScrollPane1.getViewport().setView(cont);
`
I tried to add width and height to the dimension and it worked but I have to put a huge number for it to work (if I put a small number it won't allow me to scroll ) It's not right
this is what it looks like without the "cont.setPreferredSize(new Dimension());"
I want to make it dynamic, please help, thank you
cont.setPreferredSize(new Dimension());
Remove this to let LayoutManager calculate the size. TGhen scrollpane reflect the calculated size rather than fixed you provided.

How to avoid that multiline text is clipped in javafx?

I have a GridPane which I'm filling with various graphical/textual elements.
For the text, single line labels gets the right size. The same happens to e.g. images
of various sizes (the grid is stretched to give space for the image).
However, for multiline text elements (a.e. a label containing text with newlines in it), it clips the element at one line height... How can I force an UI element (like a label) to take up enough space to display its content?
Here's some code (scala):
val chatPanel = new GridPane {
setFitToWidth(true)
setFitToHeight(true)
setManaged(true)
setMaxWidth(10000)
setMaxHeight(10000)
}
def sendTextInfoBlock(title:String,message:String) {
val button = new Label(message) {
// setWrapText(true)
// setMinHeight(100) <- this works, but of course doesn't match the required height
}
// val button = new Button(message)
chatPanel.add(button,1,row)
the message is a text with newlines, like "this is a long\nand interresting\nmessage"
I gave up, stacked a VBox full of labels (one label per line of text), and just styled it
like a button. That worked well at least.

Reduce gap between JPanels

I have a simple Class StatPanel that extends JPanel and has a few JTextFields, a JCheckBox, and a couple JLabels. The panel is going to be used as a series of panels used to display information and allow the user to edit the data in some of the JTextFields and I will probably have about 40 of them all stacked together on the side of the program in a JScrollPane JPanel. The layout I am using is a GridLayout(0,1).
I have them working but when the are in the scroll panel they are spaced too far apart vertically. I have tried changing the margins, changing the spacing in the GridLayout and changing the margins of the various items. Nothing I do seems to make them closer.
This is the constructor for the StatPanel class:
public StatPanel(String statName, String statAbility){
super();
this.skillName = statName;
this.setAlignmentY(CENTER_ALIGNMENT);
isClassSkill = new JCheckBox();
statLabel = new JLabel(skillName);
statTotalField = new JTextField(maxLength + 1);
statTotalField.setHorizontalAlignment(JTextField.CENTER);
statAbilityLabel = new JLabel("= " + statAbility + " ");
statAbilityModField = new JTextField(maxLength);
statAbilityModField.setHorizontalAlignment(JTextField.CENTER);
statSeperator1 = new JLabel(" + ");
statRanksField = new JTextField(maxLength);
statRanksField.setHorizontalAlignment(JTextField.CENTER);
statSeperator2 = new JLabel(" + ");
statMiscModField = new JTextField(maxLength);
statMiscModField.setHorizontalAlignment(JTextField.CENTER);
this.add(isClassSkill);
this.add(statLabel);
this.add(statTotalField);
this.add(statAbilityLabel);
this.add(statAbilityModField);
this.add(statSeperator1);
this.add(statRanksField);
this.add(statSeperator2);
this.add(statMiscModField);
}
}
When I use it in the program it looks like this:
As I am going to stack so many of them I want them pretty much one on top of the other but I can seem to remove the gap between them.
How is this done?
Thank you for your help.
Check out the FlowLayout API. By default a JPanel uses a FlowLayout with default horizontal/vertical gap of 5 pixels. You will want to change the vertical gap used by the layout manager. So at the top of your method you will want to add:
setLayout( new FlowLayout(...) );
Edit:
Once you change the gap you will also lose the gap at the top/bottom of the main panel so you might want to add an EmptyBorder to the panel.

Limit rows shown in JTable + Next/Previous button

I'm trying to create a JTable that's only showing one row at the time. When you press Next/Previous, the table will display either the next or the previous row, depending on the button pressed. If there is no Next/Previous, the JTable shall display the first/last element.
Code for only showing one row at a time:
tableModel = new DefaultTableModel(data, columnNames);
tableModel.setRowCount(0); //If I remove this, the things I add ends up att the 2nd line.
tableModel.addTableModelListener(resultTable);
resultTable = new JTable(tableModel);
scroll = new JScrollPane(resultTable, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
final int rows = 1;
Dimension d = resultTable.getPreferredSize();
resultTable.setPreferredSize(new Dimension(d.width,resultTable.getRowHeight()*rows));
Code for Next-button (Not working, what do to? Shows the same element as before):
int height = resultTable.getRowHeight()*(rows-1);
JScrollBar bar = scroll.getVerticalScrollBar();
bar.setValue( bar.getValue()+height);
Code for Previous-button (Not working, same as above, nothing happens/changes.)
int height = resultTable.getRowHeight()*(rows-1);
JScrollBar bar = scroll.getVerticalScrollBar();
bar.setValue(bar.getValue()-height);
What do to? Some help would be greatly appreciated. Been trying to solve this forever.
Edit: Trying to follow the code from here: JTable row limitation but can't get it to work.. Though I only want to show 1 row, not 10.
From the above you specify row count to get single row in a table
final int rows = 2;
Alternatively you could try with table with Row header. Here is a sample
http://www.java2s.com/Code/Java/Swing-Components/TableRowHeaderExample.htm

Categories