This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes Layouts - finding item location Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Layouts - finding item location" Watch "Layouts - finding item location" New topic
Author

Layouts - finding item location

Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 818
I have a large JPanel which uses a GridBagLayout in which I put other JPanels inside the cells. The main JPanel is then added to a JScrollPane. What I'm looking to provide the user a way to automatically scroll to a particular item in the scrollable area (i.e. each of the child JPanels would represent - let's say a person for argument sake - and an user would like to click a button or bring that JPanel into focus / view.

The JPanels are added within a loop to the grid and references of these JPanels at the moment aren't store in an array or anything, so it's a case of being added and that's it. Also there's the case of where was the object exactly added and also how to have the scrollbar move to the proper focus location (both horizontally and vertically).

Any helpful comments / suggestions would be appreciated. Thanks.
m Korbel
Ranch Hand

Joined: Jun 19, 2012
Posts: 155
I have a large JPanel ..... location (both horizontally and vertically).

e.g. JPanel.scrollRectToVisible(JComponent.getBounds());,


- everything else is only simulations of,

- maybe you want to/would need to centering in the JViewport, then you have to use bound from JViewport in your calculation too

- everything with JViewport, JScrollBar, JScrollPane must be done on EDT,

- events from Focus/FocusSubsystem are asynchronous then shopuld be delayed, moved to the end of EDT, by using, wrapped inside invokeLater
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 818
Thanks for the response. Since I'm only "throwing" in JPanels into the main JPanel what would be the easiest / best approach to take to track the JPanels? Each JPanel has a series of strings inside of it (for simplicity sake let's say a person's ID, name, address, etc..,). I want to allow an user to search for information using a series of text fields and a search button. What approach would you consider the easiest / most efficient way of doing this? I'm struggling with the best / most effective approach to take. Thanks again.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2177
    
    7
(for simplicity sake let's say a person's ID, name, address, etc..,).


Maybe it would be easier to use a JTable to display the information in a single row. Read the section from the Swing tutorial on How to Use Tables. You could even use the table's filtering feature to search for people.

what would be the easiest / best approach to take to track the JPanels?


Otherwise I would guess you would store you custom class in an ArrayList. You custom class would need methods like getName(), getWhaterYouWantToSearchOn(). Then you iterate through the list to find the panels that contains the search word. The problem with this approach is that you can only scroll to the first panel, so you would have to keep eliminating panels one search word as a time.
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 818
Thanks Rob. The JPanels aren't being displayed in tabular display such as spreadsheet form (although this was interesting for something else I'm working on), there is column and rown spanning going on as well. I was hoping I wouldn't have to track them in an array but I couldn't really see an alternative approach - thanks for confirming that :-)
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 818
Finding the exact coordinates in the parent JPanel is all I believe I'm left with. I have a loop that puts child JPanels in a GridBagLayout across the screen (contained in a JScrollPane). I'm storing each child JPanel within an ArrayList now with associated information (name, address, etc..,). If I randomly selected an entry from that ArrayList of JPanel children what is the easiest way I could find out the coordinates of this JPanel within theparent JPanel? I'm assuming I need those coordinates to know where the JScrollPane needs to scroll to.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2177
    
    7
You should be able to use the getBounds() of the child panel and then invoke the scrollRectToVisible() method on the parent panel.
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 818
getBounds() returns 0 for x and y. The child JPanels are within a larger / parent JPanel that use GridBagLayout as the layout manager. I keep all the child JPanels in an ArrayList. If I try to use getBounds on any JPanel in this ArrayList I get 0s.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2177
    
    7
Something is wrong with your code. If you have a true parent/child relationship then the x/y or the child panels must all be different. Of course the GUI must be visible in order for the location of all the panels to be calculated.

Post you SSCCE that demonstrates the problem. Again, the SSCCE is not about your real code, it is simply a text program where you create and add all the child panels (we don't care about the compnents on the panels) to your parent panel with the GridBagLayout. Then once the GUI is visible you iterate through your ArrayList to print the location of all the panels. Once you get this simply example working you determine what is different between this code and your real code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Layouts - finding item location