Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    atrodeyo is offline Member
    Join Date
    Apr 2012
    Posts
    41
    Rep Power
    0

    Default Please help me with the following code

    can anyone help me please with this problem. When I click on the button in the layerOne Class it creates TestLayerTwo () Below is my TestLayerOne Class where I have TestLayertwo () created under "btnActionPerformed". what I should do there so that LayerTwo shows up.

    Java Code:
    // TestLayerOne Class
    package test.gui;
    
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class TestLayerOne extends JPanel {
    
    	TestLayerTwo layerTwo;
        private JButton btn;
        
        public TestLayerOne() {
            initComponents();
        }
    
    
        private void initComponents() {
        	
        	
            btn = new JButton();
            setLayout(new GridBagLayout());
    
            btn.setText("Button");
            btn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    btnActionPerformed(evt);
                }
            });
            add(btn, new java.awt.GridBagConstraints());
        }
    
        private void btnActionPerformed(ActionEvent evt) {
            
        //	layerTwo
        	layerTwo = new TestLayerTwo ();
        	
        	
        	System.out.println("This is a Test");
        }
        
          
    }
    
    
    // TabPanels Class
    package test.gui;
    
    
    import java.awt.Dimension;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    
    
    public class TabPanels extends JFrame{ 
    
    	JFrame myFrame = null;
    	// main to run and test this part of the program
    	
    public static void main(String [] args)  {  
        new TabPanels(); 
    } 
    
    	public TabPanels() { 
    	    JTabbedPane tp = new JTabbedPane(); 
    	    add(tp); 
    	 
    	    tp.addTab("Tab1",TestLayerOne()
    	    tp.addTab("Tab2", TestLayerOne()); 
    	    tp.addTab("Tab3", TestLayerOne()); 
    	 
    	    setPreferredSize(new Dimension(1360,768));
    	    //   myFrame.setPreferredSize(new Dimension(1360,768)); 
    	    pack();
    	    setVisible(true);
    	} 
    }
    
    // layerTwoClass
    package test.gui;
    
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    
    public class TestLayerTwo extends JPanel {
    
        /**
         * Creates new form TestLayerTwo
         */
        public TestLayerTwo() {
            initComponents();
        }
    
    
        private void initComponents() {
    
            lbl = new JLabel();
    
            setLayout(new GridBagLayout());
    
            lbl.setText("This is a Test ");
            add(lbl, new GridBagConstraints());
        }
        private JLabel lbl;
        
    }
    Last edited by atrodeyo; 04-28-2012 at 08:32 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,392
    Rep Power
    25

    Default Re: Please help me with the following code

    What do you do with the instance you create? You need to add it to something in a GUI that is being shown to be able to see it.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    atrodeyo is offline Member
    Join Date
    Apr 2012
    Posts
    41
    Rep Power
    0

    Default Re: Please help me with the following code

    I created the layout instance ( layerTwo ) but I am totally lost and cant figure out what I should add that instance to inorder it to show up in Tab1 instead of TestLayerOne after clicking on the button. Thats where I got stuck.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,392
    Rep Power
    25

    Default Re: Please help me with the following code

    Have you tried adding to a container that is being shown in the GUI?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    atrodeyo is offline Member
    Join Date
    Apr 2012
    Posts
    41
    Rep Power
    0

    Default Re: Please help me with the following code

    all I have is GridBagLayout and I cant really do

    Java Code:
    layerTwo.add ( new GridBagLayout());
    also I tried with the
    Java Code:
     layerTwo.setLayout (new GridBagLayout ());
    and that did not work.

  6. #6
    atrodeyo is offline Member
    Join Date
    Apr 2012
    Posts
    41
    Rep Power
    0

    Default Re: Please help me with the following code

    someone please help .. i am learing so help me please

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,392
    Rep Power
    25

    Default Re: Please help me with the following code

    what I should do there so that LayerTwo shows up.
    Where do you add the component you are not seeing (layerTwo) to the GUI? You do add ONE component (btn) on line 33.

    BTW The code you posted does not compile.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,392
    Rep Power
    25

    Default Re: Please help me with the following code

    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    atrodeyo is offline Member
    Join Date
    Apr 2012
    Posts
    41
    Rep Power
    0

    Default Re: Please help me with the following code

    Code had its main commented out but I changed it now. Also I tried with
    Java Code:
     add (layerTwo, new GridConstraints());
    but it didnt work either. After refreshing both layerOne and LayerTwo shows up but the target is only the LayerTwo will show up not the layerOne.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,392
    Rep Power
    25

    Default Re: Please help me with the following code

    I can not tell from one statement where it is located in the program. You need to post all code to show where it is and when it is executed.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    atrodeyo is offline Member
    Join Date
    Apr 2012
    Posts
    41
    Rep Power
    0

    Default Re: Please help me with the following code

    I tested and it executes fine for me. Only thing it doesnt do is when I click the button layerTwo does not take over. But I I didnt have any compiling error at all

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,392
    Rep Power
    25

    Default Re: Please help me with the following code

    If you need help with the new code, post it.
    Otherwise mark this thread as solved.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    atrodeyo is offline Member
    Join Date
    Apr 2012
    Posts
    41
    Rep Power
    0

    Default Re: Please help me with the following code

    I already edited my code in this thread and took out the commented part and you should be able to compile now

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,392
    Rep Power
    25

    Default Re: Please help me with the following code

    The code in post #1 will not compile because of line 71:
    tp.addTab("Tab1",TestLayerOne()

    The end of the statement is missing.

    Please post code that compiles, executes and shows the problem.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    atrodeyo is offline Member
    Join Date
    Apr 2012
    Posts
    41
    Rep Power
    0

    Default Re: Please help me with the following code

    I guess when I copy pasted my code I somehow missed that part but still I am pasting my code again.
    Code for TestLayerOne
    Java Code:
    package test.gui;
    
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    
    public class TestLayerOne extends JPanel {
    
    	TestLayerTwo layerTwo;
        private JButton btn;
        
        public TestLayerOne() {
            initComponents();
        }
    
    
        private void initComponents() {
        	
        	
            btn = new JButton();
            setLayout(new GridBagLayout());
    
            btn.setText("Button");
            btn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    btnActionPerformed(evt);
                }
            });
            add(btn, new GridBagConstraints());
        }
    
        private void btnActionPerformed(ActionEvent evt) {
            
        //	layerTwo
        	layerTwo = new TestLayerTwo ();
        	add (layerTwo, new GridBagConstraints());
        	
        	System.out.println("This is a Test");
        }
        
          
    }
    code for TestLayerTwo

    Java Code:
    package test.gui;
    
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    
    public class TestLayerTwo extends JPanel {
    
        /**
         * Creates new form TestLayerTwo
         */
        public TestLayerTwo() {
            initComponents();
        }
    
    
        private void initComponents() {
    
            lbl = new JLabel();
    
            setLayout(new GridBagLayout());
    
            lbl.setText("This is a Test ");
            add(lbl, new GridBagConstraints());
        }
        private JLabel lbl;
        
    }
    code for TabPanels
    Java Code:
    package test.gui;
    
    
    import java.awt.Dimension;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    
    
    public class TabPanels extends JFrame{ 
    
    	JFrame myFrame = null;
    	// main to run and test this part of the program
    	
    public static void main(String [] args)  {  
        new TabPanels(); 
    } 
    
    	public TabPanels() { 
    	    JTabbedPane tp = new JTabbedPane(); 
    	    add(tp); 
    	 
    	    tp.addTab("Tab1",new TestLayerOne());
    	    tp.addTab("Tab2", new TestLayerOne()); 
    	    tp.addTab("Tab3", new TestLayerOne()); 
    	 
    	    setPreferredSize(new Dimension(1360,768));
    	    //   myFrame.setPreferredSize(new Dimension(1360,768)); 
    	    pack();
    	    setVisible(true);
    	} 
    }

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,392
    Rep Power
    25

    Default Re: Please help me with the following code

    After you change the contents of a container by adding a component to it, you need to tell the container the redo the laying out of its components. Look at the Container class API doc for a method that tells the container to lay out its subcomponents again.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    atrodeyo is offline Member
    Join Date
    Apr 2012
    Posts
    41
    Rep Power
    0

    Default Re: Please help me with the following code

    if I use validate (); layerOne (button) still stays

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,392
    Rep Power
    25

    Default Re: Please help me with the following code

    It should stay until you remove it.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    atrodeyo is offline Member
    Join Date
    Apr 2012
    Posts
    41
    Rep Power
    0

    Default Re: Please help me with the following code

    little help with removing please. I tried with layerTwo.remove (btn); but that didnt work also I am thinking there should be a way to remove everything if I have more than one button.

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,392
    Rep Power
    25

    Default Re: Please help me with the following code

    What container contains the component you want to remove?

    What container did you validate?
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 16
    Last Post: 01-31-2012, 08:36 PM
  2. Replies: 3
    Last Post: 08-10-2011, 09:17 AM
  3. Replies: 0
    Last Post: 08-07-2011, 08:32 PM
  4. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 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
  •