aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Jpanels and Jscrollpanels Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Jpanels and Jscrollpanels" Watch "Jpanels and Jscrollpanels" New topic
Author

Jpanels and Jscrollpanels

Oliver Higgins
Greenhorn

Joined: Feb 14, 2011
Posts: 8
Hi,

I am reallllly hoping you guys can help me.

I have a small gui app, it is using the mvc model and i am having issues with the listner on the jscrollpanel.

I have the main jpanel, in it is 3 area, the center being for my data. this is a gridlayout jpanel. I had a loop which put another jpanel in it. The listener was attached to middle panel.this works, but to meet the requirements of my assignment i need to use the jscrollpanel. I have added this to the middle panel and then added the final panel to it. This works great excpet now i cannnot get the jscrollpanel to trigger the listener. Any idea would be great. I have the code but it is long.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
Welcome to the Ranch

What sort of Listener is it?
Don't know, but I think we shall need to see your c ode.
Oliver Higgins
Greenhorn

Joined: Feb 14, 2011
Posts: 8
Campbell Ritchie wrote:Welcome to the Ranch

What sort of Listener is it?
Don't know, but I think we shall need to see your c ode.


Cheers, this is the View part of my work

Line 129 is where i need to add my object so the controller will see it with the listener. 139-200ish is where the gui is put together to make my grid. I have added screenshot to give you an idea.




[Thumbnail for homecad.jpg]

Oliver Higgins
Greenhorn

Joined: Feb 14, 2011
Posts: 8
at line 186

//content2.add(scrollPane, BorderLayout.CENTER);
content2.add(j_x, BorderLayout.CENTER);

if i use this instead my programs functions work perfect but i do not get my scroll bars etc.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
Why do you want to a scroll pane in a method called xxxListener? That method has a confusing name; it should have add or remove in its name. You should be adding the scroll panes in the constructor or "setUpGUI" method.
Oliver Higgins
Greenhorn

Joined: Feb 14, 2011
Posts: 8
Campbell Ritchie wrote:Why do you want to a scroll pane in a method called xxxListener? That method has a confusing name; it should have add or remove in its name. You should be adding the scroll panes in the constructor or "setUpGUI" method.



Thanks Campbell

It is called from the constructor.

The grid redraws itself after each click or double click but when i implement the jscrollpane i no longer get the mouse event to fire.
Oliver Higgins
Greenhorn

Joined: Feb 14, 2011
Posts: 8
maybe if i explain it this way;

For my GUI i the following
JFrame
>JPanel (nav bar)
>JPanel (gridlayout dynamic based on house size)
>> JPanel(each grid/cell)
>>> Componts (label etc)
>> JPanel (Stat bar)
This works fine to select and work with my rooms(part of the project) in each grid. I have the listener attached to the gridlayout panel, so i know where my mouse has clicked and therefore which grid it belongs to. The listener is the mouselistener on the JPanel.

But when i place a JScrollPane in here;
JFrame
>JPanel (nav bar)
>JPanel (gridlayout dynamic based on house size)
>>jScrollPane
>>>JPanel(each grid/cell)
>>>>Components (label etc)
>>>JPanel (Stat bar)
To give me the scroll bars for each room, my gui no longer accepts the mouse event (it does but not on the gridlayout JPanel as it did before). Does anybody know how i can overide the JScrollPane and let my JPanel underneath receive the event?
I am struggle to find a way to do this that will adhere to the MVC structure. As the code to check and prepare everything is in the controller which just hands the basic info over to the view to repaint it all.
Or i am just completely going in the wrong direction
Javad Rashidi
Ranch Hand

Joined: Feb 13, 2011
Posts: 34

Hi Oliver. i think the problem is JScrollPane because it is gets mouse event and do not let JPanel get mouse event. you should use dispatchEvent to get mouse event from JScrollPane and send it to JPanel.


http://www.myviwo.com
Oliver Higgins
Greenhorn

Joined: Feb 14, 2011
Posts: 8
Javad Rashidi wrote:Hi Oliver. i think the problem is JScrollPane because it is gets mouse event and do not let JPanel get mouse event. you should use dispatchEvent to get mouse event from JScrollPane and send it to JPanel.


Do you have an example? I am basically trying to do this

Add JScrollPane to Jpanel
Send click from JscrollPane to JPanel.
Javad Rashidi
Ranch Hand

Joined: Feb 13, 2011
Posts: 34

here is an example:

parent: component that you want to recive the MouseEvent fom this component
Oliver Higgins
Greenhorn

Joined: Feb 14, 2011
Posts: 8
Thankyou for that, i still fighting with it. my "parent" is called content2 and it is a JPanel. I changed the JComponent to JPanel, but i;m not sure which "parent" in the code to change to content2....??? (I am really appreciating your help)
Javad Rashidi
Ranch Hand

Joined: Feb 13, 2011
Posts: 34

you should pass content2 to constructor of MyScrollPane because you add MyScrollPane to content2. when mouse event happen on MyScrollPane it dispatch to content2. you don't need to change parent to content2.
Oliver Higgins
Greenhorn

Joined: Feb 14, 2011
Posts: 8
like this
Javad Rashidi
Ranch Hand

Joined: Feb 13, 2011
Posts: 34

yes exactly
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Jpanels and Jscrollpanels