Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can anybody help??

 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Friends:

I hope to draw the line between different components in a JPanel, but I hope the line should be a component, ie, it can be clicked, highlighted, selected and deleted instead of just painting a line in a panel,
But I search on web and google a while, I did not find this kind of "Line Component Example" to connect components in JPanel,
Can any guru throw some light or give some example??
Thanks in Advance

Michelle
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try a panel and set its width at 1 pixel??? There isn't a setWidth method but there are several methods allowing you to set sizes.
 
Vijay Daniel
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

JSplitPane will be the right solution for your problem...
Get help from Java API...

With Regards
Vijay
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a gander at the very last post in this thread.
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friends:
Thanks for reply.
see below Craig's famous and excellent code.
But What I need is that I need all these lines to connect theses components should be independent clickable or selectable components, these lines can be selected, clicked and remove at run time, not painted at design time and like this cannot be selected and deleted,
ie. independent clickable or selectable components are like JButton and JLabels etc, can any grur help??
Thanks.
Michelle.



 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great!! Thanks so much Craig, How can your guru so smart and so genius?
Highly appreciated.
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Sir:
One more question,
[1].how can I adjust the width of the lines I draw in this example??
i.e, make them wider or narrower at my will;
[2]. How to draw arrow lines??
I tried to use Strokes, but fails.

Really sorry to bother.
Thanks
sunny
[ April 24, 2007: Message edited by: girl sunny ]
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great!! Thanks so much guru, one more question, how to draw line with arrow in this sample??
[ April 24, 2007: Message edited by: girl sunny ]
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changes to SelectableLinks for adding arrow heads to line:
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
great jobs, great man!! thanks so much, highly appreciated. so wonderful!!
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Craig,
Your sample code is excellent, My case is a little bit different from your one.
[1]. you use Rectangle as a basic component



But my case use JLabel as basic components,

the difference is that in your code:

here only Rectanle has method to call lines[j].intersects(seeker);
but JLabel did not have this method, If I use JLabel, how Can I find a method something like intersects to make sure this line intersects with this JLabel instead of Rectangle?? I check the API already, so far not yet find this kind of method, can you help how to use it??

thanks
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you use Rectangle as a basic component
In the getContent method the Rectangle is used to store the [x, y, width, height]
information that is required for setting the location (x,y) and size (width,height) of
each JLabel added to the parent Container with null layout.
In the addLabel method

We are still adding JLabel (as the basic) components but using a null layout which
requires that we specify the location/size for each child component. setBounds is
one way to do this.

the difference is that in your code
...
here only Rectanle has method to call lines[j].intersects(seeker);
but JLabel did not have this method,

In the code block for mousePressed the Rectangle seeker is used as an easy
way to test if the mouse is on top of or close enough to any of the lines which connect
the components. By moving/centering the Rectangle seeker under the mousePressed
location we can test for Rectangle/Line intersection. This will allow the user to select
any of the visible lines that connect the basic components. We are testing for
intersection between the single/movable Rectangle instance seeker and each of the
visible connection lines.

If I use JLabel, how Can I find a method something like intersects to make sure this
line intersects with this JLabel instead of Rectangle??

This is a different question. To test if any line intersects a JLabel (or just about any
other Component) you can use the bounds of the JLabel for the test

Edit: Correct formatting.
[ April 30, 2007: Message edited by: Craig Wood ]
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Guru:
Thanks so much for your wonderful code and explanation
[ May 01, 2007: Message edited by: girl sunny ]
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Craig:
I tried to apply and integrate your idea and samples into my following code, but not success, can you help some more??
I was really stopped. In the following code, I can dynamically draw line between any components, But I cannot make these lines selectable and deletable. see code below. How to make these lines selectable and deletable by using your methods??

Thanks a lot!



Regards.

Michelle.
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be able to select lines with the mouse you will have to have references to the lines or
construct them for each mousePressed event. This will be difficult to do when you add labels
to the vector and draw connecting lines between components contained in the vector in
paintComponent (the way it is now.)
I would try to add lines to the vector instead of adding labels to the vector. This will
simplify the selection and removal of lines. Adding new lines becomes the problem with this
approach; you'll have to figure out a way to allow the user to add a line between any two
components. After that things should go much smoother.
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Craig,
You are right, last night, I tried to add Lines into Vector and hope to dynamically draw lines between JLabels and make these lines selectable, I can draw lines. but failed to select and remove lines, I am still trying and figure it out. your suggestion is very valuable.
Really thanks for your great help.
Regards
Michelle
[ May 02, 2007: Message edited by: girl sunny ]
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you get the lines in the vector the matter of selecting and removing them is pretty
much the same as used in SelectabeLinks. The only difference is that you are using a vector
instead of an array of lines.
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so far not yet success, thanks for advice.
[ May 04, 2007: Message edited by: girl sunny ]
 
Craig Wood
Ranch Hand
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was more difficult than I had imagined. My last post missed the mark.
This app should allow adding, selecting and removing lines as you wish.
To add a line: press the addLine button and then click any two labels.
The removeLine button will work for selected (red) lines as before.
The app is set up for non-generics, j2se 1.4-
There are generic statements (for the two List/ArrayLists) which you can
uncomment (and comment–out the two non–generic statements right beneath them)
if using j2se 1.5+ and you want to avoid compiler warnings.
 
Michelle Wang
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for your great help. really appreciated, have a very good weekends. Test success. without you help, I cannot solve this problem.
Best Regards.
Michelle.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic