I have a JScrollPane that holds a JTable. The table is dynamically populated from a database query. That all works fine and the table is getting populated, but its wider than the visible window.
I can see the left part of the table and I need to scroll right to see the right end of it. When I do that, the part of the table that was hidden (right end) is blurred. Then I stop scrolling and wait few seconds and then it is repainted and looks fine. I scroll back left and everything is OK - no blur.
I guess I'm missing some force repaint - but shouldn't JScrollPane handle it automatically ?
How do I get rid of this effect ?
Here is how I construct it (I wont paste all the code since it basically work):
jScrollPane1.setAutoscrolls(true);
jScrollPane1.getViewport().setLayout(borderLayout2);
jScrollPane1.getViewport().add(table, BorderLayout.CENTER);
this.add(jScrollPane1, BorderLayout.CENTER);
Never seen such an artifact, might be due to the fact that you are adding the table in a completely wrong manner. Replace the first three lines of your snippet with:
jScrollPane1.setViewportView(table);
Related
I have a JScrollPane with JPanel on it. I have also many different components on JPanel (for example Labels, JTextFields, JTextAreas). These components are added programmatically (at runtime, on the user request).
JPanel uses SpringLayout. The program calculates preferred size of JPanel after adding components, because scrolling doesn't function properly without calculating. Adding components and calculating preferred size are part of my prepareGui() method.
The data displayed on components are periodically refreshed (in my refreshData() method, which is invoked by listener).
My problem: after refreshing, scroll bar always sets to fixed position (I don't know why). I want the scroll bar to stay in old position after refreshing.
I tried to find a place, where the scroll bar moves. I checked some values at the start and at the end of refreshData() method, but they was the same (they didn't change inside of refreshData()):
scrollPane.getVisibleRect().getX()
scrollPane.getVisibleRect().getY()
scrollPane.getVisibleRect().getWidth()
scrollPane.getVisibleRect().getHeight()
panel.getVisibleRect().getX()
panel.getVisibleRect().getY()
panel.getVisibleRect().getWidth()
panel.getVisibleRect().getHeight()
scrollPane.getHorizontalScrollBar().getX()
scrollPane.getHorizontalScrollBar().getY()
scrollPane.getHorizontalScrollBar().getWidth()
scrollPane.getHorizontalScrollBar().getHeight()
scrollPane.getHorizontalScrollBar().getValue()
scrollPane.getVerticalScrollBar().getX()
scrollPane.getVerticalScrollBar().getY()
scrollPane.getVerticalScrollBar().getWidth()
scrollPane.getVerticalScrollBar().getHeight()
scrollPane.getVerticalScrollBar().getValue()
Changing of layout manager didn't take effect.
I noticed one thing (I don't know if it has any meaning). When I set only preferred size (in prepareGui()), the scrollbar moves to the end. When I set also size, the scrollbar moves to strange, fixed position.
How can I prevent this?
The scroll bar should keep it's current position.
I finally found the answer here: https://stackoverflow.com/a/5230477/5694159 (thanks to Johan M) in the topic about JTextArea instead of JPanel.
When the refreshData() is calling in separate thread it works good.
I hope this question and answer will help someone.
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();
For most items within Java, you simply add .setVisible(false). Then the item disappears from the form, but when I do this for a Jtable within Netbeans, using:
table1.setVisible(false);
Rather the table going invisible, it goes grey instead. Shown in picture below:
How do I get this to work?
You need to call reset() and revalidate() on the container that holds the JTable after making it invisible.
You shouldn't be making it invisible anyway, yes perhaps the JScrollPane that holds it, but not the JTable itself.
You've misspelled "Length".
I have a DataGrid in a simple layout panel that is collapsible. It is collapsed by default, and when I expand it after the page loads, the column headers are visible but the data rows are not. If I set the panel to be expanded by default, then all the data in the table is visible, and remains so after collapsing/expanding again.
Any ideas what could be causing this? I have layout panels all the way up. I tried making the panel visible to begin with, then setting it invisible in a deferred command after setting the data provider. However, at the time the deferred command ran, the table reported having two data rows but they hadn't displayed yet. The panel was set invisible before the data rows ever became visible, so they never became visible.
I hope my description makes sense. Thanks for any advice you can give!
Calling dataGrid.onResize() after making the grid visible did the trick for me.
Please keep in mind that onResize() may need to be called from a deffered command, like this:
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
#Override
public void execute() {
view.titleGrid.onResize();
}
});
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.