Results 1 to 11 of 11
  1. #1
    LovJava is offline Member
    Join Date
    Jan 2010
    Posts
    24
    Rep Power
    0

    Default Add panel to parent panel

    Hi Everybody,

    I want to add panel to my parent panel dynamically as and when i change my combo box. I tried with action listener but thats not working . Please help me.

    regards,
    LovJava

  2. #2
    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 LovJava View Post
    I want to add panel to my parent panel dynamically as and when i change my combo box. I tried with action listener but thats not working . Please help me.
    Hello. I have a feeling that we're going to need to see code in order to understand what you're doing wrong. The best code to post would be a small compilable and runnable program that attempts do to just this -- add a new JPanel into an existing JPanel on button push -- but does no more, an SSCCE (please see the link).

    Often problems of this type stem from using the wrong layout managers, or not calling revalidate and repaint on the JPanel that acts as the container after adding or removing components.

  3. #3
    LovJava is offline Member
    Join Date
    Jan 2010
    Posts
    24
    Rep Power
    0

    Default

    can't we do without revalidating and repainting.
    i am calling the below mentioned class in the action listener.
    Java Code:
     
    private class ReadMessage extends AbstractAction {
    
    		private Integer m_session;
    
    		private ReadMessage(Integer sessionValue) {
    			m_session = sessionValue;
    		}
    
    		@Override
    		public void actionPerformed(ActionEvent arg0) {
    			// JComboBox comboBox = (JComboBox)arg0.getSource();
    			int y = 20;
    			for (int i = 0; i < 4; i++) {
    				JPanel panel = new JPanel();
    				panel.setPreferredSize(new Dimension(500, 150));
    				panel.setLayout(new BorderLayout());
    				panel.setBorder(BorderFactory.createBevelBorder(
    						javax.swing.border.BevelBorder.RAISED, Color.LIGHT_GRAY,
    						Color.LIGHT_GRAY));
    				
    				panel.add(createSession());
    				panel1.add(panel);
    				panel.setBounds(10, y, 500, 150);
    				y += 160;
    			}
    			panel1.setPreferredSize(new Dimension(500, ((Integer) m_cmbNoOfSessions
    					.getSelectedItem() * 160 + 20)));
    			//ShiftMasterPanel.this.m_txtNoOfSessionsActionPerformed(arg0, m_session);
    		}
    
    	}
    Last edited by LovJava; 08-31-2010 at 05:54 AM.

  4. #4
    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 LovJava View Post
    can't we do without revalidating and repainting.

    What would motivate you to not revalidate and repaint if it would help solve your problem? It's like saying "can't we do without breathing".


    i am calling the below mentioned class in the action listener.
    Java Code:
     
    private class ReadMessage extends AbstractAction {
    
    		private Integer m_session;
    
    		private ReadMessage(Integer sessionValue) {
    			m_session = sessionValue;
    		}
    
    		@Override
    		public void actionPerformed(ActionEvent arg0) {
    			// JComboBox comboBox = (JComboBox)arg0.getSource();
    			int y = 20;
    			for (int i = 0; i < 4; i++) {
    				JPanel panel = new JPanel();
    				panel.setPreferredSize(new Dimension(500, 150));
    				panel.setLayout(new BorderLayout());
    				panel.setBorder(BorderFactory.createBevelBorder(
    						javax.swing.border.BevelBorder.RAISED, Color.LIGHT_GRAY,
    						Color.LIGHT_GRAY));
    				
    				panel.add(createSession());
    				panel1.add(panel);
    				panel.setBounds(10, y, 500, 150);
    				y += 160;
    			}
    			panel1.setPreferredSize(new Dimension(500, ((Integer) m_cmbNoOfSessions
    					.getSelectedItem() * 160 + 20)));
    			//ShiftMasterPanel.this.m_txtNoOfSessionsActionPerformed(arg0, m_session);
    		}
    
    	}
    Your code won't compile or run for me, so I find it hard to see what you're doing wrong. Again, please read the link I provided above. Yes, it will take some effort for you to create and post an SSCCE, but the effort will help us to see where your problems are and to help us help you, and thus are worth it.

    Again, much luck.

  5. #5
    LovJava is offline Member
    Join Date
    Jan 2010
    Posts
    24
    Rep Power
    0

    Default

    What would motivate you to not revalidate and repaint if it would help solve your problem? It's like saying "can't we do without breathing".
    i mean it with the visiblity mode?

  6. #6
    LovJava is offline Member
    Join Date
    Jan 2010
    Posts
    24
    Rep Power
    0

    Default

    Initially in the parent class constructor i am defining the resourseMap for labels . In the previous defined class createSession() , inturn gives the panel with some fields .In the main parent when i select combo box with values 1-5 .The action listner calls that previous class. Now i am able to add dynamically but the problem is am not able to view the resources(labels).

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

    Default

    Again, without a small testable example program, one that just shows JLabels or JPanels on button push and does nothing else (no database connection, no internet connection), I don't think that there's much help that we have to offer. If you are still stuck, again please look at this link: SSCCE, try to create and post one, and your chances of our being able to help you will increase greatly.

    I wish you much luck!

  8. #8
    LovJava is offline Member
    Join Date
    Jan 2010
    Posts
    24
    Rep Power
    0

    Default

    I have JPanels has some components(JTextField, Jbutton ) .I am iterating that panel for some number. i wanted how do i set the value for appropriate action for appropriate panel.Pls help me for this.

  9. #9
    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 LovJava View Post
    I have JPanels has some components(JTextField, Jbutton ) .I am iterating that panel for some number. i wanted how do i set the value for appropriate action for appropriate panel.Pls help me for this.
    Have you not read anything I've posted? Without an SSCCE, I'm afraid that you're just wasting your time and ours.

  10. #10
    LovJava is offline Member
    Join Date
    Jan 2010
    Posts
    24
    Rep Power
    0

    Default

    Just give me some tips .Usually in your case how would u do?. I read your sscce ,but in my case there severl other refered class i am calling.So it is difficult to do create example .

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Quote Originally Posted by LovJava View Post
    So it is difficult to do create example .
    Goodbye then.

    db

Similar Threads

  1. Panel in a panel?
    By pahiker in forum AWT / Swing
    Replies: 1
    Last Post: 07-05-2010, 11:42 AM
  2. Adding a panel to a panel
    By rclausing in forum New To Java
    Replies: 7
    Last Post: 02-02-2010, 05:56 AM
  3. how to set the location of a panel?
    By jboy in forum New To Java
    Replies: 5
    Last Post: 10-27-2009, 07:24 PM
  4. How to set Image in Panel ?
    By sudmitra in forum New To Java
    Replies: 0
    Last Post: 09-12-2009, 10:07 PM
  5. panel resizing
    By simo_mon in forum AWT / Swing
    Replies: 1
    Last Post: 08-15-2009, 02:09 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
  •