Adding Libgdx Scrollbar to TextArea - java

So I've been searching around and for the life of my cannot figure out how to correctly do this. I simply have a small text area and want to be able to scroll through it. I've been told it's as easy as adding the TextArea to a ScrollPane, but it appears to be more complicated than that. Here's the gist of my code:
Skin defaultSkin = newSkin(DEFAULT_SKIN_FILEPATH +"uiskin.atlas", DEFAULT_SKIN_FILEPATH
+"uiskin.json");
TextArea textArea = new TextArea(levelLoader.getCodeSnippet(), defaultSkin);
ScrollPane pane = new ScrollPane(textArea, defaultSkin);
pane.setForceScroll(false, true);
pane.setFlickScroll(false);
pane.setOverscroll(false, true);
pane.setBounds(0f, 20f, game.getWindowWidth(), 300f);
gui.addActor(pane);
Gdx.input.setInputProcessor(gui);
setIsCreated(true);
levelLoader.getCodeSnippet() returns a string containing a multi-line piece of text from a .txt. The TextArea appears in the game window, and the multi-lined text also appears. However, I can only scroll through the text with the arrow keys. I forced the scrollbar to display itself, but it occupies the entire right side of the window like this:
http://s27.postimg.org/vqws36k77/pic.png
It will not scroll and thinks there are not multiple lines to scroll through even though there are evident by scrolling through with the arrow keys. I've also tried making the textArea larger than the scrollPane, but might have done it incorrectly. What am I doing wrong here?
Update: I've tried both placing the ScrollPane inside of a table and setting the cell size of the table. Any other suggestions would be appreciated.

This is not best solution. I guess it is LIBGDX update layout bug.
This is workaround solution that works for me:
Set pref. rows manually and update scroll layout, when the number of the rows is changed.
textArea.setPrefRows(numberOfScrollLines);
//numberOfScrollLines = text.split("\n").length
pane.layout();

Related

Swing JTextArea: Resize Dialog properly after automatic LineWrap

I hope I did not miss some duplicate question, I feel like this should be trivial!
Anyway, I have a JTextArea with text in it, with automatic line wraps:
public PleaseResize(){
super();
Container cp = this.getContentPane();
JTextArea area = new JTextArea();
area.setColumns(20);
area.setLineWrap(true);
area.setEditable(false);
area.setWrapStyleWord(true);
area.setText("Once upon a midnight dreary, while I pondered, weak and weary, over many a quaint an curious volume of forgotten lore.");
cp.add(area, BorderLayout.CENTER);
cp.add(new JButton("Hallo"), BorderLayout.SOUTH);
this.pack();
}
I want the text area to resize vertically to display the whole text... but it does not. It resizes happily if I choose the line breaks via pushing in \n, but with automatic wrapping, it just remains the size it is.
I somehow feel like I am missing something totally obvious...
Edit: for clarification:
On creation-time, I do not know how many lines the text will have (due to the automatic linebreaks happening). I am receiving Text via an XML-file, and it can vary between 0 and about 30 lines after wrapping. I have the vertical space to display everything, but I do not want to scroll or to have a huge, white area when there is no or only a little text to display.
Edit 2:
After rephrasing the question, they key to the problem I was facing was apparently not resizing the JTextArea, but making sure the dialog knows how big it is!
So, here is the link back to the solution I ended up using: An automatic resizing Text above a button?
You need to wrap JTeatArea with JScrollPane like next new JScrollPane(area); and then add JScrollPane to your JFrame.
Also you create JTextArea with empty constructor, use JTextArea(int rows, int cols) to specify count of rows and columns.

need noRepaint() when i validate()

I have a JScrollPane with a View. I'm doing a chat application , it's why i need to have my ScrollBar to the Maximum(). To get the right Maximum of the View i have to validate before. When I validate my ScrollPane an automatic repaint is doing. I don't want this repaint because it makes a double repaint when i set the ScrollBar to the Maximum : when the ScrollBar is at the top and a another when it is a the bottom.
my code :
Main.getWindow().getMainPanel().getScrollPaneCenter().validate();
scrollPaneCenter.getVerticalScrollBar().setValue(scrollPaneCenter.getVerticalScrollBar().getMaximum());
PS : I want to disable the repaint of my conponent or maybe you have a solution to have a reversed JScrollPane( to always have the ScrollBar to Bottom) .
I'm doing a chat application
I'm guessing your are using a a JTextArea or JTextPane.
it's why i need to have my ScrollBar to the Maximum().
You don't need to validate or set the scrollbar manually. You juat append the text to the bottom of the Document. See Text Area Scrolling for a couple of solutions.

Dynamically resize textarea to fit content

Here is a textarea I have put in my program using netbeans. The first image shows what the textarea looks like when I run the program. The second image is show the textarea after I press a button that adds about 50 "Hello world" strings to the text area - only the first 6 get shown. I need the textarea's height to dynamically increase to fit all these strings. Anyone know how to do it?
Edit: I dont want scrollbars so using a JScrollPane is not an option.
I wouldn't use JTextArea for anything except prototyping. It has the bitter functionality of notepad. But it's up to you.
I would use a JEditorPane or JTextPane. I know you can't size it based on character size but that's for the best. For word wrap, you can do setContentType("text/html"); and wrap the text in <'p><'/p> tags.
(Note you still use JScrollPane for scrolling. In fact, the scroll pane works for any component)
See the differences: http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html
Try this http://java-sl.com/tip_text_height_measuring.html
I haven't tried it with JTextArea but suppose it should work
Include the JTextArea inside a JScrollPane
JTextArea textArea = new JTextArea();
JScrollPane scrollArea = new JScrollPane(textArea);
This will dynamically change the text area based on if scrolling is needed or not

problem with textarea in javafx?

I used textarea in javafx 2.0 but i need to add it scrolpane.how can i do that?
Scrolllpane s = new Scrollpane();
s.setnode(textarea);
but when i click on scroll pnane it has doesn't move.
what is problem?
setNode() is the right method to call to set the node that the ScrollPane will scroll over. I've used ScrollPane extensively in my 2.0 app, but I have not tried it on Text Area. Based on the API documentation for TextArea (http://download.oracle.com/javafx/2.0/api/com/javafx/preview/control/TextArea.html) it sounds like it has its own built in scroll bars? I would try setting the width/height of the TextArea, and also set the max width/height, and see if you can trigger scrollbars to appear automatically when the lines in the text area exceed the available space.
If you still want to put it in a ScrollPane, perhaps with some other nodes, you should use a container node such as VBox or something to wrap the TextArea, then set the VBox to be your scroll node on ScrollPane.
Also, bear in mind that TextArea is not a committed control for FX 2.0 yet and is therefore less hardened than the other FX controls.
When we create a text area, scroll bar automatically appears when it goes beyond t

Java Swing Scrollpane in NetBeans

I have Java application which adds JTextFields # runtime to JPanel. Basically user clicks a button and new JTextField is added, clicks again added again...
Each new JTextField is directly below the previous one. Obviously I run out of space pretty soon so I'm trying to use JScrollPane and thats where the hell begins, because it just doesnt work no matter what I try.
Right click on JPanel and Enclose in Scroll Pane. Didnt work.
After reading some examples I realized I must have JPanel as an argument for JScrollPane constructor. Which I did via right clicking on ScrollPane and CustomizeCode. Because apparently auto-generated code is protected in NetBeans and I cannot just change all those declarations, etc. manually. Still doesnt work.
I did try to set PreferedSize to null for JPanel and/or JScrollPane, didnt help.
JScrollPane is a child of lets call it TabJPanel (which in turn is a tab of TabbedPane). I tried to mess with their relationships, basically trying every possible way of parentship between JFrame, JPanel(holding textfields), TabJPanel and JScrollPane, but nothing worked.
I also made VerticalScrollBar "always visible" just in a case. So I see the scrollbar, it's just that populating that JPanel with JTextFields does not affect it.
When there are too many JTextFields I they go "below" the bottom border of JPanel and I cannot see them anymore.
Code for adding new JTextFields is like this, in a case it's relevant.
JTextField newField = new JTextField( columns );
Rectangle coordinates = previousTextField.getBounds();
newField.setBounds(coordinates.x , coordinates.y + 50, coordinates.width, coordinates.height);
JPanel.add(newField);
JPanel.revalidate();
JPanel.repaint();
Sorry for a long post I'm just trying to provide as much info as possible, because being newbie I dont know whats exactly relevant and whats not. Thanks in advance :)
As there is another answer now, I'm adding my suggestion too.
This sounds exactly like a problem to use a JTable with a single column. JList is not yet editable (and might never be).
JTable would handle the layout problems for you, and you can easily access the values via the table.
Use your own TableModel (a simple Vector should be sufficient in your case), and add values to it.
An option you have is to utilize a LayoutManager, instead of setting the bounds directly on the components. To test this, a simple single column GridLayout with the alignment set to vertical should prove the concept.
panel.setLayout(new GridLayout(0,1));
zero in the rows param allows for rows to be added to the layout as needed.
I do this way to add a scrollpane, create a panel and fill it with few components, then create a scrollpane in the component you want to add it, cut and paste the panel in which all your details will fall in and resize the scrollpane.Because the components take a larger space than the one visible right click on the scrollpane and select design this container, there you can increase the size of the scrollpane and add as many components as you have.

Categories