7 Replies - 5411 Views - Last Post: 03 October 2010 - 07:40 AM Rate Topic: -----

#1 sahildave1991  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 45
  • Joined: 18-September 10

want to add Jpanel over another Jpanel at a specific position

Posted 01 October 2010 - 10:11 AM

i have to add a jpanel of size 200,300 at coordinate 550,50 over a jpanel of size 800,400...i have tried many things....setLocation is not working, setPreferredSize gives an error...
pls help
Is This A Good Question/Topic? 0
  • +

Replies To: want to add Jpanel over another Jpanel at a specific position

#2 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: want to add Jpanel over another Jpanel at a specific position

Posted 01 October 2010 - 11:12 AM

If you really feel the need to do this (for which I can think of no reason) you need to setLayout(null) on the first panel and setBounds(Dimension) on the second panel befroe adding it to the first.

You would probably do well, however, telling us what it is you are actually trying to accomplish with this rather than attempting to hammer through your perceived solution. We may be able to suggest something better.
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: want to add Jpanel over another Jpanel at a specific position

Posted 01 October 2010 - 02:47 PM

and by doing that you defeat all Java nice features about layout automatic Components resizing
Better to you VB if you want that type of architecture
Was This Post Helpful? 0
  • +
  • -

#4 sahildave1991  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 45
  • Joined: 18-September 10

Re: want to add Jpanel over another Jpanel at a specific position

Posted 01 October 2010 - 08:40 PM

ohh is that so,
i wanted to do something like....when i click any menu item from the menu...some details and option come in a box which is located on the right side of windows...
what should i do then ?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: want to add Jpanel over another Jpanel at a specific position

Posted 02 October 2010 - 09:04 AM

You put the box on the right side of the panel
you put the components in and set tem visible(false)
when the button is clicked you set visible the corresponding component

Here is an easy one with the buttons changing the visibility of a corresponding JLabel
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class FourBox extends JFrame implements ActionListener {

	JButton b1, b2;
	JLabel lab1, lab2;
	
	FourBox() {
		super("Hide and show");
		JPanel p = new JPanel(new GridLayout(2,2));
		b1 = new JButton("Buttton 1");
		b1.addActionListener(this);
		p.add(b1);
		lab1 = new JLabel("This is button 1 text");
		lab1.setVisible(false);
		p.add(lab1);
		b2 = new JButton("Buttton 2");
		b2.addActionListener(this);
		p.add(b2);
		lab2 = new JLabel("This is button 2 text");
		lab2.setVisible(false);
		p.add(lab2);
		add(p, BorderLayout.CENTER);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
	
	public void actionPerformed(ActionEvent e) {
		JLabel toChange;
		if(e.getSource() == b1)
			toChange = lab1;
		else
			toChange = lab2;
		toChange.setVisible(!(toChange.isVisible()));
	}
	
	public static void main(String[] args) {
		JFrame f = new FourBox();
		f.setBounds(25, 25, 300, 100);
		f.setVisible(true);
	}
}


Was This Post Helpful? 0
  • +
  • -

#6 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: want to add Jpanel over another Jpanel at a specific position

Posted 03 October 2010 - 01:03 AM

View Postsahildave1991, on 02 October 2010 - 03:40 AM, said:

ohh is that so,

Yes, it is.

Quote

i wanted to do something like....when i click any menu item from the menu...some details and option come in a box which is located on the right side of windows...
what should i do then ?

Above is one method, actually thinking about it without thinking in terms of absolute positioning (which defeats all flexibility and most creativity/productivity) is, of course, also an answer.
Was This Post Helpful? 0
  • +
  • -

#7 sahildave1991  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 45
  • Joined: 18-September 10

Re: want to add Jpanel over another Jpanel at a specific position

Posted 03 October 2010 - 01:30 AM

Quote

JLabel toChange;
if(e.getSource() == b1)
toChange = lab1;
else
toChange = lab2;
toChange.setVisible(!(toChange.isVisible()));


pls can u explain me this toChange label part ???
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: want to add Jpanel over another Jpanel at a specific position

Posted 03 October 2010 - 07:40 AM

If you follow along the loop, you see that if you click b1, you set lab1 = toChange (so that you change it later, or if you click b2, you set lab2 = toChange. Now, toChange could be EITHER one base on the button that you click, but whichever one it is, you change its visibility to the opposite of what it was. That means that if you could see it before, now you can't and vise versa.

Do you understand?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1