Results 1 to 6 of 6
  1. #1
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default How do you make a JPanel a child Widget?

    I've made a RisingPanel which is a subclass of JPanel.
    I added RisingPanel to the ChatClient's GUI, where I wanted it to appear.

    The problem is that the RisingPanel is not visible on the main JFrame. If you look at the RisingPanel code below, you'll see i've added log output on each line so I know the rising panel's instruction set is executed - but I just can't see it.

    I'd like to know how to make it visible. I'm pretty sure it has something to do with somehow communicating that RisingPanel is a child widget of ChatClient GUI.

    Secondly, how could I find out the parent widget's height and weight. (I think the answer is pretty obvious after the answer to the latter question).

    Thanks!


    Java Code:
    public class RisingPanel extends JPanel implements Runnable{
    	private int width;
    	private int height;
    	private String information;
    	private boolean grow;
    	
    	public RisingPanel(Composite parent, String message,int width)
    	{
    		//set height and placement of items
    		
    		this.setLayout(null);
    		this.setBackground(Color.BLUE);
    		this.width = width;
    		this.height = 50;
    		this.setSize(this.width, 5);
    		
    		final JTextField inputField = new JTextField("");
    		inputField.requestFocus(true);
    		int w = 100;
    		int h = 24;
    		int x = (this.width/2 - w/2);
    		int y = (this.height/2 - h/2);
    		inputField.setBounds(x,y,w,h);
    		this.add(inputField);
    		
    		JButton storeInformationButton = new JButton("OK");
    		storeInformationButton.addActionListener(
    				new ActionListener()
    				{
    					public void actionPerformed(ActionEvent event)
    					{
    						while(information.isEmpty()){
    							information = inputField.getText();
    						}
    						shrink();
    					}
    				}
    		);
    		storeInformationButton.setBounds((x+w+10),y,40,25);
    		this.add(storeInformationButton);
    		
    		JLabel label = new JLabel(message);
    		System.out.printf("Log: Label text set to \"%s\".\n",message);
    		label.setBounds(x,(y-30),100,25);
    		this.add(label);
    		
    		this.setVisible(true);
    	}
    	
    	public void grow()
    	{
    		this.grow=true;
    		Thread grower = new Thread(this);
    		grower.start();
    	}
    	
    	private void shrink()
    	{
    		this.grow = false;
    		Thread shrinker = new Thread(this);
    		shrinker.start();
    	}
    	
    	public void run()
    	{
    		if(this.grow==true)
    		{
    			System.out.printf("Log: Growing panel from %d to %d.\n",this.getHeight(),this.height);
    			int height=5;
    			while(this.getHeight()<this.height)
    			{
    				this.setSize(this.width, height);
    				height+=5;
    				try{
    					Thread.sleep(10);
    				}catch(InterruptedException e)
    				{
    					e.printStackTrace();
    				}
    			}
    			System.out.printf("Log: Panel has grown. Size: %d.\n",this.getHeight());
    		}else
    		{
    			System.out.printf("Log: Shrinking panel from %d to %d.\n",this.getHeight(),5);
    			while(this.getHeight()>5)
    			{
    				this.setSize(this.width, height);
    				height-=5;
    				try{
    					Thread.sleep(10);
    				}catch(InterruptedException e)
    				{
    					e.printStackTrace();
    				}
    			}
    			System.out.printf("Log: Panel has shrunk. Size: %d.\n",this.getHeight());
    		}
    	}
    	
    	public String getInputFromRisingPanel()
    	{
    		return this.information;
    	}
    }
    P.s. Any improvements to the code would be appreciatred.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,360
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by eLancaster View Post
    I've made a RisingPanel which is a subclass of JPanel.
    I added RisingPanel to the ChatClient's GUI, where I wanted it to appear.

    The problem is that the RisingPanel is not visible on the main JFrame. If you look at the RisingPanel code below, you'll see i've added log output on each line so I know the rising panel's instruction set is executed - but I just can't see it.

    I'd like to know how to make it visible. I'm pretty sure it has something to do with somehow communicating that RisingPanel is a child widget of ChatClient GUI.

    Secondly, how could I find out the parent widget's height and weight. (I think the answer is pretty obvious after the answer to the latter question).
    In Swing every JComponent is a Container so that it can contain other JComponents; there are a few 'top level' components: Windows and Dialogs; a Frame (or JFrame) is an extension of a Window; in order for a JComponent to be visible it has to be part of a tree of Components ending at a top level component at the top or root, say a JFrame. If it isn't, it won't be visible. You have to stick your extension of the JPanel somewhere in a tree of Containers ending at a top level component, I don't see that happening in your code.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    More important than the code you've shown us is the code for the component that holds and displays the RisingPanel. What layout managers are you using for this container? Are you revalidating and repainting it whenever you add, remove or change the size of a component? Are you using println debug statements to show the size of the RisingPanel instance during the program run? I see you using a background thread to try to change its size -- would you be better off using a Swing Timer to be sure that Swing code (such as your setSize method call) are called on the EDT? Do you need to call setPreferredSize instead of setSize?

  4. #4
    eLancaster is offline Member
    Join Date
    Jan 2011
    Posts
    93
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    More important than the code you've shown us is the code for the component that holds and displays the RisingPanel. What layout managers are you using for this container?
    Although Layout Managers are useful, I find them particularly annoying and prefer to use the setBounds() method instead.

    Quote Originally Posted by Fubarable View Post
    Are you revalidating and repainting it whenever you add, remove or change the size of a component?
    The contents of the Rising Panel never get changed. It only exists for a few minutes to get one piece of information - then i dispose it. I could have used JOptionPane but I was going for style and something i'd never tried before.

    Quote Originally Posted by Fubarable View Post
    Are you using println debug statements to show the size of the RisingPanel instance during the program run?
    Yup

    Quote Originally Posted by Fubarable View Post
    I see you using a background thread to try to change its size -- would you be better off using a Swing Timer to be sure that Swing code (such as your setSize method call) are called on the EDT?
    I haven't learnt Swing Timers yet - which is why i haven't used them but I have heard about them and do plan to implement them into the code once I understand how they work.

    Quote Originally Posted by Fubarable View Post
    Do you need to call setPreferredSize instead of setSize?
    I believe setPreferredSize is something that LayoutManagers use. Since I'm not using a Layout manager - i didn't use setPreferred Size.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by eLancaster View Post
    Although Layout Managers are useful, I find them particularly annoying and prefer to use the setBounds() method instead.
    And this is why you're running into this problem. They are only annoying until you learn and understand them, but once you do understand them and use them properly, solving your current problem will be trivial (and in fact this is where they shine). Until you do, solving your problem will be very difficult.

    Best of luck.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Also, regarding this code:

    Java Code:
          JButton storeInformationButton = new JButton("OK");
          storeInformationButton.addActionListener(
                new ActionListener()
                {
                   public void actionPerformed(ActionEvent event)
                   {
                      while(information.isEmpty()){
                         information = inputField.getText();
                      }
                      shrink();
                   }
                }
          );

    The ActionListener code is counter to the concepts of event-driven programming since you have a while (true) loop present, and this will freeze Swing's main thread, the event dispatch thread, or EDT. Better to simply return from the method if the input field is empty:

    Java Code:
          JButton storeInformationButton = new JButton("OK");
          storeInformationButton.addActionListener(
                new ActionListener()
                {
                   public void actionPerformed(ActionEvent event)
                   {
                      information = inputField.getText().trim();
                      if (information.isEmpty()) {
                         return;
                      }
    
                      shrink();
                   }
                }
          );

Similar Threads

  1. Widget issue
    By soundprizm in forum New To Java
    Replies: 0
    Last Post: 04-21-2011, 06:24 PM
  2. SWT Transition Widget - STW
    By fossilman2005 in forum SWT / JFace
    Replies: 0
    Last Post: 01-02-2010, 11:16 PM
  3. How to make a JPanel focusable
    By toymachiner62 in forum AWT / Swing
    Replies: 10
    Last Post: 10-21-2009, 10:09 AM
  4. Simple widget advice...
    By billy341 in forum SWT / JFace
    Replies: 0
    Last Post: 04-04-2009, 08:58 PM
  5. How to make a JPanel highlighted on mouseclick
    By aneesahamedaa in forum AWT / Swing
    Replies: 3
    Last Post: 09-01-2008, 09:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •