File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 504

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: 504

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: 504

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: 504

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: 504

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: 504

(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: 504

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: 504

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: 504

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: 504

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: 504

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: 504

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),