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
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.
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 ]
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??
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
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??
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.
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 ]
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.
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.
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 by:autobot
The first person to drink cow's milk. That started off as a dare from this tiny ad:
a bit of art, as a gift, that will fit in a stocking