my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes 3 questions regarding JPanel. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "3 questions regarding JPanel." Watch "3 questions regarding JPanel." New topic
Author

3 questions regarding JPanel.

Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 501

As per show some effort i try to read & implement via google search, forum's discussion, examples written by experts but i failed to get expected result. So i ask this question...

Scenario:-
I have 1 JFrame which contain ContentPane which size is same as JFrame.
In ContentPane there is 1 JPanel which size is same as ContentPane.
In Jpanel i put another JPanel which is small & it can Drag & drop any where in bigger(parent) JPanel.
I put 1 jpg file i same folder where this java file resides.

Here is code (i use Eclipse Helios 6.2 with Visual Editor plug in) :-


I got error in console as:-
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
at DDdemo.getFp(DDdemo.java:55)
at DDdemo.getJcp(DDdemo.java:239)
at DDdemo.initialize(DDdemo.java:206)
at DDdemo.<init>(DDdemo.java:186)
at DDdemo$3.run(DDdemo.java:171)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)


but program runs fine.
Question 1:- How i put jpg image as background?

Question 2:- When i drag small JPanel(mp),it start with cursor but after that it follow it not come with it (it get behind few centimeters to cursor), so how can i drag it with cursor?

Question 3:- I have to use many JPanels(small size) for drag & drop i 1 big JPanel. How can i use Array of JPanels?

Millions saw the apple fall, but Newton asked why.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Question 1:- How i put jpg image as background?

have a look at the Swing FAQ (main page of this forum) for an example

> Question 2:- When i drag small JPanel(mp),it start with cursor but after that it follow it not come with it (it get behind few centimeters to cursor), so how can i drag it with cursor?

normally you'd need to determine where the dragged panel was clicked and use that point as an offset to where the dragged panel's location is to be.
if this makes no sense, create a demo program, just frame/panel/panel or label (to drag)/listener and post that code here.

> Question 3:- I have to use many JPanels(small size) for drag & drop i 1 big JPanel. How can i use Array of JPanels?

don't understand the question - array of panels, to do what?
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 501

Thanks Michael,
As you wrote
have a look at the Swing FAQ (main page of this forum) for an example

i seen it.
Problem is, i use eclipse helios 6.2.0 with visual Editor, by that if i create new file as java class with name of BackgroundPanel & copy paste whole program which shown in example
It easily run & show image.(i put 1 gif file in same folder where BackgroundPanel.java file resides.
Now i try to write code for fulfilling my requirements
1. I need 1 Big jPanel with background image.(i use BackgroundPanel)
2. I need 1 small jPanel which should be moveable any where withing big jPanel( i name it mp)

I try to create program as follows:-


Now code runs & show image & BackgroundPanel. but not showing mp panel.
Now what should i do.

If i use Jpanel visual class at time of creating new file in eclipse, it create code

i confuse by
private void initialize()
&

public eg1() {
super();
initialize();
}

If i get idea of how to put example in to above code then might my problem solve.

Further requirements i will solve later on.
i need help.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
I can't help at all with gui-builder stuff, never used it, prefer the simplicity of coding by hand.

anyway, I slapped this together, single .java file, which might answer some of your problems.
compile/run from the command line if you can't get it to work in eclipse.
click the small red panel, and drag it around.

Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 501

Thanks Michael,
I can't help at all with gui-builder stuff, never used it, prefer the simplicity of coding by hand.

anyway, I slapped this together, single .java file, which might answer some of your problems.


Your code is work for me. After 6 days i got some relief. At first 2 days i search how panel should move in big panel. Got answer by studding many forum,examples,books,scholars notes. Mouse press & release.

Next 2 days went into went into find out how to drag that small panel.

When 3 conditions fulfill then i stuck in basics of swing. how to set image.

But now i move further.
Now i will try my 3rd question of 1st post of this topic.

Question 3:- I have to use many JPanels(small size) for drag & drop i 1 big JPanel. How can i use Array of JPanels?

Say at least 10 small jPanels can be use in one big jPanel.
Now first i study & try myself then ask as i do earlier.
But by you & this forum i got way.
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 501

I create code for solve my question 3, i use following line repeatedly for p to p9 so 10 jpanels created.
JPanel p = new JPanel();
Question 4. If i use Array instead of this traditional way,then code get smarter way. but how?
As per Show some efforts, i try search

As per above search i write

JPanel p[]=new JPanel[10];
for(i=0;i<10;i++)panel[i]=new JPanel();
if (i<10) {
p.setBackground(new Color(255, 255, 204));
p[i].setSize(new Dimension(25, 25));
p[i].setName("1");
p[i].setBorder(BorderFactory.createLineBorder(Color.black, 1));
//p.setLocation(new Point(9, 6));
}

but not works..something wrong...

Question 5. How to stores jpanels new locations in database like mysql, so if we restart application then also jpanels locate at new location not at original location...
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if this is your actual code, it can't work

for(i=0;i<10;i++)panel[i]=new JPanel();
if (i<10) {

the for loop only creates the panels, when i == 10, the for loop stops,
so the next line 'if (i<10)', will be false

> How to stores jpanels new locations in database

if you were saving to a text file, you'd possibly save it like this
0|50|100
1|150|200
etc

where:
0 and 1 are the panel array element numbers
50 and 150 are the x co-ords
100 and 200 are the y co-ords

then you'd read in the file, convert the strings to ints, so it'd be
panel[0].setLocation(50,100);//the numeric values here would be variable names
panel[1].setLocation(150,200);//ditto
etc

modify to suit for saving to a db
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 501

Again 3 questions regarding jpanels

Right now i stop work on Array, because priority of program is when it start,it should check whether jpanels co-ordinates are original one or it's change when last program run.(those are stores in database say mysql.)
If jpanels co-ordinates are change at last run then it should be set that its permanent location till next mouse drag event calls. If mouse drag event not call for month,then each time program runs jpanels are situated at its same location which set from database values(these stores at last program runs mouse release event.)

Now

Question 1. i can stores x,y co-ordinates (old & new,both).
But i can not read it & set to jpanel.setlocation(x,y);

Question 2. If i drag jpanel out of JFrame when Jframe is its original state, it hides but when i maximized JFrame then it seen. how jpanel set bound to jframe so it can not drag out of jframe when it not maximized?

Question 3. Jpanels are drag-able as i need,but its overlap each other if i drag 1 jpanel to another jpanel location. If i drag all jpanel at 1 location,it also possible! but i need to set them as they can not overlap each other when mouse released event occurs?


here is code:-
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
(2) and (3) would have the same answer - check the co-ords of the panel when released,
and if outside frame, or overlap another panel, setLocation of panel to something acceptable,
for frame set it so it's against the edge of the frame, for panel adjacent to panel.

(1) for db help you're better off posting in the JDBC forum, but not the whole code,
create a frame with panel, 1 draggable panel, the code to save the co-ords and the code to
retrieve the co-ords (should be no more than 30 or 40 lines long - most people won't read, or
bother with, 450 lines of code)
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 501

(should be no more than 30 or 40 lines long - most people won't read, or
bother with, 450 lines of code)

I agree with you.
I solve most of problem of x,y co-ordinates save & retrieve from sql database!
i create viewtable function & in that i use for loop

if you see code which elaborated by Michael Dunn in 2nd post
anyway, I slapped this together, single .java file

In that jPanel p is present. I added 8 panels more in this program.
Now i can drag & drop all 9 panels, save their old x,y co-ordinates & new one also in sql database. After restarting program i can fetch new x,y co-ordinates & by them i can place all jpanels at respective co-ordinates except panel p.
Why this i cant understand?

Now problem is not jdbc...
Now question is how could i use array for jpanels so code should get smaller & smarter?
I try in 2-3 days, by forum,books,docs etc.
today i try to search in google,
i found people use gridlayout for show array panels.
why that so?
If any clue/idea?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> how could i use array for jpanels so code should get smaller & smarter?

arrays also for the JLabels

I'll use this example as it's the first one where you use setBounds(...)


most of the code is identical to each of the getP..()
to convert to arrays, you'd start with

then you'd create the panels/labels in a for loop
(for simplicity, I'm setting the names 0-based, modify to suit)


and that's all the code you'd need to create 9 panels, 9 labels, all added to where they belong

the only tricky bit is the setBounds, which normally would be a formula based on the value of 'x' in the loop,
but as you're reading the last location in from a db, I'd be storing that info in a Point[] array, so I'd start by
creating one and using it from the start, then, after first use, read the info from the db into the point array


(probably a few typo's in the above, but it should be self-explanatory)
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 501

Thanks Michael,
By your help i done jpanel code smaller & smarter.
Now i can create jpanels as i want, by just changing value in [x].eg as below


using jdbc i can store old & new x,y co-ordinates in sql db. By fetching those i can place jpanels as i need.
if new x,y co-ordnates are 0 in sql db then
by following logic i can set jpanel on default location as i want.
value of 26 because panel size 25,25 & 1 for gap between 2 panels.


Now question 1. how i check whether jpanel present or not at location where the jpanel which i drag & want to drop at certain location at mouse released event? other wise in db i got same location co-ordinate by different jpanels.
it can be done by sql query or java code without sql! Which one will be easy?

question 2. how to set boundary so jpanel not to drag beyond jframe/jpanel. because right now resolution 1280by960 but if i change it then what? or right now jframe's setSize(800,600);
& it re-size-able so if i drag jpanel i goes beyond seen area.

question 3. how i check that how much RAM/processor & more details, this program takes?


Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
almost the same answer for (1) and (2)

you need to understand that when you drag a panel around you're setting it's location at it's top-left corner, the x,y co-oords.
each panel is the same size (25,25), so there is an exclusion zone where the x,y point cannot be set.

e.g. say you have a panel at 200,100 - its 'space' is 200,100 to 225,125 (a square), so the exclusion zone for this panel is
175,75,50,50 (or 175,75 to 225,125) - draw it on paper to see how it extends left and up. now all you have to do is create
a Rectangle of that exclusion zone, create a Point of the x,y new location and check if the rectangle.contains(point). If it
does, you'll need to change it's x,y location to something outside the rectangle. you need to check each panel's exclusion
zone.

In deciding on a method to relocate a panel, be careful of an infinite loop - can't go here, so I'll put it there, can't go there,
so I'll put it (back) here, can't go here etc etc

very similar for the frame bounds, but don't use the frame's size, use the parent container's size - the frame has borders etc


> how i check that how much RAM/processor & more details, this program takes?

far better off posting this in the Jave In General forum. Include Ram/CPU in subject line, and in the topic mention you have a
Swing app that connects to a db, and ask your question. Include a link to this thread if anyone wants to see some code.
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 501

question 2. how to set boundary so jpanel not to drag beyond jframe/jpanel. because right now resolution 1280by960 but if i change it then what? or right now jframe's setSize(800,600);
& it re-size-able so if i drag jpanel i goes beyond seen area.

For my own question i try logic as follows in mouse drag event

Now x & y are always grater than 1 for all panels so virtual boundary set for 0th side.(left vertical & upper horizontal)
but at display resolution 1280 by 1024 pixels
when i drag(maximum but small panel keep fully visible not beyond vision) panel at right upper corner i get x = 1251, y = 2
when i drag panel at right bottom corner i get x = 1253,y = 971

but there is system's windows taskbar also. when i get those co-ordinates JFrame is at maximized state so taskbar is hidden...
Now regarding my old question number 2
i have
question 1:- there are various resolutions available for different displays how i write code so it check max_x -1 & max_y-1 so small panels never go beyond vision & Whenever user change system resolution my code get automatically check it?

question 2:- how to set jframe's setExtendedState as Windows taskbar should visible & frame should not beyond that.
It goes beyond windows taskbar when i write code as below


question 3:- when i checking my program many times i drag small panel position which x is <0 like -24 same for y also
As per my code program immediately save x, y in sql table
for that i write code

so i hope -24 is less than 0 & '||' is for or
but not it help...why?
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 501

As per reply by Michael Dunn at Wednesday, April 18, 2012 12:54:06 PM
(1) for db help you're better off posting in the JDBC forum, but not the whole code,
create a frame with panel, 1 draggable panel, the code to save the co-ords and the code to
retrieve the co-ords (should be no more than 30 or 40 lines long - most people won't read, or
bother with, 450 lines of code)


i create question in jdbc section in this forum. but before i get answer of that Wendy Gibbons (who is Moderators of jdbc section) lock the question!

Its not fair...

As per rules & regulation i study,try then ask...
my question contain more swing but jdbc also important for me.
Only 1 fellow take interest in this question when as per i done google search there is very less forums discuss this type of question. answers are irrelavent also many times.
But this forum helps so i use it. Moderator take look in this matter?
Where i get wrong step?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> i create question in jdbc section in this forum. but before i get answer of that Wendy Gibbons (who is Moderators of jdbc section) lock the question!
> Its not fair...

I've read that post in the jdbc forum twice, and I can't see any question relating to connecting/updating/retrieving info from a db,
the questions/problems seem to be the same as those posted in this forum.

I pointed you to the jdbc forum because you posted this

"Question 1. i can stores x,y co-ordinates (old & new,both).
But i can not read it & set to jpanel.setlocation(x,y); "

which I read as you having problems retrieving the info from the db (specific db help, as in the query)

then, your next post
"Now problem is not jdbc... "

so, it does look like your jdbc-forum post was correctly locked.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> question 1:- there are various resolutions available for different displays how i write code so it check....




> question 2:- how to set jframe's setExtendedState as Windows taskbar should visible & frame should not beyond that.
> It goes beyond windows taskbar when i write code as below

setExtendedState(JFrame.MAXIMIZED_BOTH) works fine for me, taskbar shows.

I seem to recall some time back there was a difference how MAXIMIZED_BOTH was handled between java versions and platforms.
could be the same now - I'm using java 1.6_31 on Win 7.
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 501

Thanks Michael to point out where i done mistake...
so, it does look like your jdbc-forum post was correctly locked.

No issue against moderator's action...its my fault.

I solve jdbc problem partially...i create database,table by sql workbench manually. Then by java code i write update query, so whenever i drag & drop jpanel particular location,x,y store in table each time with comparing jpanels name.
I write code for read table so i can put jpanels on locations which in tables column newx,newy.
till all ok.
Problem come when i edit table manually & set two different jpanels newx,newy same & run the program that two jpanels apears on above the other so backside panel not seen.
so i try to compare two different rows in same table by select statement...but not succeed.
So as i ask in jdbc section
question 1 remains till same for me...i tried within 2-3 days as follows but not with jdbc(i can not find comparison query till now so i can not write it properly & program not works)



explanation for above code as bellow

in this Addp is my class name,Dragger is dm & r_2 is rectangle which declare in class Dragger
for r_2 code as follows in class dragger



where panel_comp is JComponent, final_x & final_y are as follows




now in i create jframe, background panel & jpanels array
& check various conditions for set location of them
i use 1 for loop
in that where db_panel_number is sql tables store values from capturing jpanels name converting string to int.
within if loop i again use if loop for checking following condition

by this i got one partial solution for boundary issue(panels should not go beyond vision) but for only 2 directions where x & y axis goes towards 0.
now for main question panes should not place one above the other i try



where locations is


Actually this 'locations' is for when database values are null manually then jpanels should put on default location.
but i tried for my question
same i tried for

i compare both points in both if loops( 1.when db fill with 0 jpanels set on default location & 2.when db fill with different values,jpanels set location as that values)

but not succeed.
Now question is my jdbc coding is week so i cant understand or i confused about sql, i not getting solution for any jpanel should not place on another jpanels location, then how i get it with jdbc or without it?


now for GraphicsEnvironment suggestion i will try to get solution today for my other two questions.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
this
"any jpanel should not place on another jpanels location"
has absolutely nothing to do with jdbc, nothing at all.

this is how it works:
1)
your program opens, reads the saved x,y co-ords from the db, and displays
the panels at those saved locations.
2)
when you move/drag a displayed panel, the Dragger code, which uses mousePressed
to get the offset, sets the 'moving/dragging' location correctly in mouseDragged
3)
in the Dragger class, you need to use mouseReleased i.e. no longer 'dragging', to
a) check the current/last location of the dragged panel
b) compare to all other panels to ensure no overlapping.
c) if so, reset location of dragged panel to whereever you deem appropriate.
d) when dragged panel's location is confirmed to be non-overlapping, save those co-ords to the db
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 501

By
i succeed to limit the all boundaries to jpanel to they not go beyond the vision.
i write code as follows:-
both declare in class so within class i can call them anywhere.


this code written for Jframe boundary.
but by this i can only limit JFrame not jpanel which i set as background panel
for that

& within background_panel class i write


Following in class dragger
Following code within mouse drag event

Following code within mouse release event


Finally 4 sides of monitor(which ever resolution) are set as boundary, any small jpanel can not drag & drop beyond that.
& if in sql table manually i change values which not satisfy with current resolution then that jpanel set at default location. by if loop


Now remaining one & last question;
i will try as
has absolutely nothing to do with jdbc, nothing at all....


by this questions i learn lots of new things in swing with little bit jdbc.
Now if i finish this, i will try JTabbedPane as background jpanel & in that various tabs which contains jpanels arrays as our main program have.
i succeed in creating 1 small program which contain 3 tabs & 1st tab contain array of jpanels but not tried further because main idea is not completed till now.
But now i hope by Michael Dunn's last message i can complete main program. I try if not succeed then again i ask.
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 501

b) compare to all other panels to ensure no overlapping.


That's the difficult for me.
i try to make an int array for jpanels x & y means at beginning of program when loops check db values & set jpanels corresponding locations, then this int array should grab the x,y from each jpanel & then i compare this array with new_x & new_y which in mouse drag & release event.

i cant to compare with resultset's db_new_x & db_new_y which grab by query due to rs.next() only 1 jpanels x,y i got which i click not all.

how should be do this?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> i cant to compare with resultset's db_new_x & db_new_y which grab by query due to rs.next() only 1 jpanels x,y i got which i click not all

told you earlier, you have to forget about anything to do with the db, in regard to setting the location of any panel after the program starts.

the db stores the 'legal' position (x,y) of each panel, and those co-ords are read in at program start.
when you (later) move/drag a panel, your program has to ensure there's no overlapping, if so, reset the x,y to a
'legal' position (not overlapping). Then, and only then, do you again access the db to save the 'legal' x,y co-ords
of the panel.

very simply, the db is just a storage tool - it keeps the location of each panel, after it has moved (past tense),
not while it is moving
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 501

you have to forget about anything to do with the db


Now i tried to compare actual x,y of jpanels with x,y after drag in mouse released event
i create array of int because i create array of jpanels(panels[x]).


now in mouse drag event i define many int, i confuse from which i should compare anArray_x[x] & anArray_y[x] & how & where (mouse drag and/or released)? (anArray_x[x] & anArray_y[x] stores all x,y properly as many times as x has. in main program if i change x then automatically that many times anArray_x[x] & anArray_y[x] print.)

i again post mouse drag event code
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you keep mixing things up.

mouseDragged does one thing, and one thing only --> it drags the panel to a new location. Nothing else.
mouseReleased is where you check whether that new location of the dragged panel is OK, and if not, reset its location.
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 501

I tried within 2-3 days for avoid overlapping of jpanels which i drag & drop, as follows:
i write following code within mouseReleased event

By this code , all jpanels are stop to overlap to last jpanel which created by array.
means i create array 9 so jpanel name set as 0,1,2,3,4,5,6,7,8.
So above code i can stop overlap jpanel name is less than 8.(if i drag jpanel 0 to 7 on jpanel 8 then this happened)
if i drag panel 3 on jpanel 4 then no use...
same if i drag panel 8 on panel 0 then it not overlap it goes behind to it.

Here i hope that, i not write much complicated code as previously i write...& no db use for avoid overlapping.
now
question 1. If all jpanels which name less than last jpanel's name (which created by array) are avoid to overlap it. Then why not they avoid overlap to each others?
question 2. When jpanel dragged how to avoid jpanels go behind which names are less than others jpanels name...eg jpanel 8 goes behind 7,6,5,4,3,2,1,0. jpanel 7 not go behind 8 but goes behind 6,5,4,3,2,1,0. etc etc?
question 3. By solving this questions is this program(array of jpanels within jpanel with drag & drop facility) comes to conclude that complete without possibility of errors?


>
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
it will not work correctly by just doing this

else
panel_comp.setLocation(new_x+50, new_y+50);

new_x/y + 50 (if size is still 25x25) will set the location to the right and below the overlapped one.
what if there's a panel already there? - it will overlap the other panel.
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 501

what if there's a panel already there? - it will overlap the other panel.

I know this possibility...but i just first to try that if overlap then place somewhere else...its not final value.

now new things come to vision at time of executing this program...following
:-
When program runs as per db all jpanel put at their respective places...no where still click by mouse...

case 1:
when if i choose any panel & mousepressed & mousereleased same/own position of that jpanel then it went to 0,0 location, same to others also...
case 2:
i run program, then drag & drop 2/3 panel somewhere & then press & released any/all panel its own location then that panel goes to panel 1's position.(in db values stores of jpanel 1's x,y for all those click & released at its own position)

case 3:- by knowing above 2 cases i not do any thing for it.. i drag panels with different positions (old & new different)...i.e. ideal conditions i purposely do then no problem...

now i forget about overlapping for some time...i got major(which i feel) errors by case 1 & 2...


I am re posting class Dragger...so any one can help regarding this can get easy...


question 1 where was my mistake in dragger class or it may occur in different class or function?
question 2 when program start if i click on any jpanel & if i capture start_Pt = m.getPoint();
start_Pt.x,start_Pt.y
mousepressed_x=m.getX();
mousepressed_y=m.getY();
means i print all int then all are 0 at first press & release why that so?
i am not printing that values at the beginning where those are declared...i print in if loops or else loops...
now i get big confusion...
after solving this i turnt to overlapping problem now...any hint?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
Like most here, I need to run the code to see 'exactly' what it's doing.

put together a small program (just frame with 2 panels) and your dragger class,
make sure it compiles/runs and produces the problem (get rid of all the Sys.out.println's)
also post the steps to reproduce the problem.

I tried using your Dragger class in my earlier demo code, but you've added things that
make it uncompilable as a stand-alone class. Also, in Dragger you implement MouseListener,
MouseAdapter is already a MouseListener 'class Dragger extends MouseAdapter...'
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 501

put together a small program (just frame with 2 panels) and your dragger class,
make sure it compiles/runs and produces the problem (get rid of all the Sys.out.println's)
also post the steps to reproduce the problem.


Here is code for 2 small jpanels within jpanel,with functionality of

1.we can drag & drop any where those small jpanel within big jpanel.
2. We can store their x,y values in mysql database table.
3.We can not drag jpanels beyond jpanel.(we can not do x<0 or y<0)
4.If we not drag jpanel anywhere after running program just run mouse pressed & released event, panel not goes at default x,y. So user can press & release as much as time want without dragging.
5.by just changing integer withing [] at 2-3 positions we can create as many jpanels we want. & all are getting all fuctionality we added in this program.

just save Addp.java & create manually mysql table 'panel_location' in database name 'manadardatabase'



now problem description...
there are 2 problems, might be there will be more but i identified 2.
problem 1. If you remove following code(in above code,code line number 271 to 286) from above code then 'avoiding of overlapping jpanels' active other wise i dont know how it not work due to somewhere i done mistake in following code.
so for this final problem is it necessary to work both functionality combined.,how?



problem 2
step 1. If you run complete code then if you drag any jpanel & released it, it stay where you released it(its good)(old position != new position )
step 2. but again you press & released without moving jpanel from its position(just click & released) then it goes to original position(old position)!(its bad)
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
I don't have any database (or access to any db) on this pc, so I can't run it.
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 501

I don't have any database (or access to any db) on this pc, so I can't run it.


you can get it from website If you really interested...
1. MySQL Community Server
2. MySQL Connector/J

Its open source...
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> you can get it from website If you really interested...

not a matter of being interested (or not), just not going to install something I don't need,
so I can't run the code.

Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 501

not a matter of being interested (or not), just not going to install something I don't need,
so I can't run the code.


then how we can co ordinate with problems & get solution?...
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you can hope someone with MySql reads this thread,
maybe even start another thread, include MySql in subject, post a link to theis thread and ask them to run your code
(danger of getting 2nd topic locked as duplicate though)
or you can simulate an ArrayList as a database - saving/retrieving the x,y's to the arraylist. this has 2 advantages,
people here can run your code 'as-is', and if there's no problem in that code, the problem has to be in the db stuff
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 501

you can hope someone with MySql reads this thread,


nice solution...
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 501

i ask question in jdbc section also
Problem in jdbc with swing...(old wine in new bottle)
by hoping that some one with mysql & swing expert read & try to help me...
i know
(danger of getting 2nd topic locked as duplicate though)

i take risk of asking question...
moderators try to understand....
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 501

i take risk of asking question...

Before getting answer by this forum...myself tried to find out answer...i explain in topic in jdbc.

Now my new question in this thread...
in eclipse helios
when i put breakpoints in

& debug...i got

Thread [AWT-Shutdown] (Running)
Daemon Thread [AWT-Windows] (Running)
Thread [DestroyJavaVM] (Running)
Thread [AWT-EventQueue-0] (Running)
Daemon Thread [Timer-0] (Running)
Daemon Thread [Timer-1] (Running)
Daemon Thread [Timer-2] (Running)
Daemon Thread [Timer-3] (Running)
Daemon Thread [Timer-4] (Running)
Daemon Thread [Timer-5] (Running)
Daemon Thread [Timer-6] (Running)
Daemon Thread [Timer-7] (Running)
Daemon Thread [Timer-8] (Running)

What is this i tried to find out as follows:-
1. As per What is Daemon thread?
Madhan Sundararajan Devaki said that
Daemon threads are least privileged threads, that can be killed without mercy by the JVM when the application stops or there is no resource such as memory.


2. As per Daemon thread purpose?
Paul Clapham said that
The API documentation (for the setDaemon() method of Thread) says
The Java Virtual Machine exits when the only threads running are all daemon threads.

So that's the purpose of making a thread a daemon thread; a running user thread will prevent the JVM from exiting whereas a running daemon thread will not.

&
Ogeh Ikem said that
A daemon thread is a service provider for other threads running in the same process.


3. As per regarding daemon thread in java
Sandeep Sanaboyina said that
A daemon thread is similar to any other thread in java. But, if a thread is set as a daemon, it will keep running till there is at least one non daemon user defined thread running. Once all the non daemon user defined threads stop, the daemon threads will automatically stop.


4. As per user threads and daemon threads
Henry Wong said that
It's more like Daemon threads serve User threads, when all the User threads terminates, the JVM exits. It is not possible to have a running program with nothing but Daemon threads.

&
User threads and Daemon threads are the same in every other way. The only thing to remember is that the JVM will exit if there is nothing but Daemon threads left.

&
There is no "user thread to act as a daemon thread" or "daemon thread to act as a user thread". A thread is either one or the other. It can be set with the setDaemon() method, and it must be done before the thread is started.


4. As per http://docs.oracle.com
In this
Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.


Now after reading these 4 webpages
my questions are, as per my plan which i explain before in this section & in jdbc section also

question 1. if i create 99 or more jpanels then as per debug result would i get 99 or more daemon threads?

question 2. How much memory will consume by each thread & what's it use(that thread & its memory also)?

question 3. If i integrate my program with another program which already filled with much more complexity & threads/servlet etc much more difficult topics, then this auto-generation of daemon threads, how harmful or helpful?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
answer to Q1, Q2 and Q3

get the program up and running, if there is a performance problem, then worry about Q1, Q2 and Q3
Mandar Khire
Ranch Hand

Joined: Sep 11, 2007
Posts: 501

get the program up and running, if there is a performance problem, then worry about Q1, Q2 and Q3


No right now there is no problem regarding performance...

Now i am trying 2-3 days one last issue which is overlapping...
My total code in mouse released is as per follows

I jumbled in if-else conditions...as per my thought there are 5 if conditions & respected else...each else condition contain few if-else conditions to avoid overlapping...so i just confuse finally how to write proper smart
code to avoid overlapping...
last updated code also available in jdbc

Now as per Michael Dunn's message

this
"any jpanel should not place on another jpanels location"
has absolutely nothing to do with jdbc, nothing at all.

this is how it works:
1)
your program opens, reads the saved x,y co-ords from the db, and displays
the panels at those saved locations.
2)
when you move/drag a displayed panel, the Dragger code, which uses mousePressed
to get the offset, sets the 'moving/dragging' location correctly in mouseDragged
3)
in the Dragger class, you need to use mouseReleased i.e. no longer 'dragging', to
a) check the current/last location of the dragged panel
b) compare to all other panels to ensure no overlapping.
c) if so, reset location of dragged panel to whereever you deem appropriate.
d) when dragged panel's location is confirmed to be non-overlapping, save those co-ords to the db

Only 2 conditions not check from my code...
I need help...
When i run above code..then only last object of array (if array of 9 then 8th) avoid overlapping when other try to overlap on it....
Same time when smaller Array value jpanel dragged on higher value jpanel then it goes behind not upside.(eg if i drag 0th panel on 1st panel from array of 9, then it move from upside of 1st panel...
but when i drag any other panel to 0th panel then it goes behind 0th panel...i dont no why...& also not know how to avid it...all jpanels should move upside of other panels when they drag here & there.
 
Don't get me started about those stupid light bulbs.
 
subject: 3 questions regarding JPanel.