Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Update JScrollPane with contents of JPanel  RSS feed

 
Josh Herron
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good evening, I am working on a small text file merging app.  (for learning purposes)

The intent is to have the user select 3 text files and when they click merge, the three files are combined, some analysis is done and  a JScrollPane containing the returned data should be displayed at the bottom portion of the ContentPane on the initial frame that was created.   I've been trying to use WindowBuilder in Eclipse to get an idea about how GUI creation works, but I can't figure out how to set up the GUI with a ScrollPane section that is blank until the  mergeFiles method runs.  

I have verified that the mergeFiles method is successfully returning a good JPanel containing the expected data.  I tested this by creating a new JFrame and adding that returned panel to the frame to review the data.  

I'm sure I'm making a stupid mistake, but I just can't figure out how to get the updated JScrollPane added to the ContentPane of the active window.   Any help would be greatly appreciated!

 
Les Morgan
Rancher
Posts: 767
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Set the minimum size property of you display object, then add it to the scroll pane, your display will look normal until you go beyond the displayable size, then the sliders of the scroll pane, which will then be visible, will give you the ability to scroll to see your off screen contents.
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Your question is too difficult to read because your comment lines run on forever and you cant read the code/comments without scrolling the window and the text area. Make sure your code/comments are visible without the need for horizontal  scrolling.

2) Post a proper SSCCE when you ask a question. Your question is about displaying a panel in a scroll pane when a button is pressed. The logic for creating the panel is irrelevant. What you need to do is update your code and include the "mergeFiles()" method. Of course we don't want your real method, we just want you to hardcode the creation of you panel with components that should cause the panel to scroll.  This can be as simple as adding a few text areas by using "panel.add( new JTextArea(20, 20) )".

Once you get the basic concept working of how to dynamically add a panel to a frame, then you worry about your real processing logic. So while you create the SSCCE you can remove the other unnecessary code. All you really need is the frame with a button to create/add the scrollpane.
 
Josh Herron
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the tips, I went back this morning and started revising the code so I could provide a SSCCE and the functionality I want started working.   So now I need to review the elements I removed while creating the SSCCE to see what was causing the ScrollPane to not display.

I will remember this lesson and simplify everything when I hit a roadblock and then start building again from a know good working config.
 
Knute Snortum
Sheriff
Posts: 3945
92
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I often find my problem just by trying to create an SSCCE.  At least I eliminate possible causes.  This is a good technique to use even outside of a forum.
 
Josh Herron
Ranch Hand
Posts: 39
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, I am quickly learning that the SSCCE is going to be a go to method for troubleshooting.   I'm about 6 weeks into my Java journey so I have much to learn, but it sure is interesting.
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So now I need to review the elements I removed while creating the SSCCE to see what was causing the ScrollPane to not display. 


I would start by NOT using a null layout and setBounds.

Swing was designed to be used with layout managers.

When using a JScrollPane, the scrollbars will appear automatically when the preferred size of the components added to the scrollpane is greater than the size of the scrollpane. If you use a null layout a panel doesn't have a preferred size. When you use layout managers the preferred size is dynamically recalculated as components are added/removed and you revalidate() the panel.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!