Hi,I've started learning Java to enhance my programming knowledge and i've written code for individual GUIs which are working perfectly. Within each GUI i have common Labels,Buttons all in a JPanel that are the same in all of the GUIs which are linked together. What I want to basically do is get rid of the common Panels into one method outside the all of the GUIs code and when each GUI is being made made, call that method and finish building the GUI.

In other words, i have two GUIs, both of which create a JLabel in the center borderlayout. How would i go about creating a separate .java file that when i run one of the GUIs it creates that JLabel but also will create the same JLabel when I click on a button, sets the visibility of the 1st one to false and the 2nd GUI is created.

I Hope I explained myself properly as to what im trying to do. Is this possible? if so, what do I have to look into and if possible a sample code.

Thanks to anyone that can help.

I've tried using that syntax but I get errors and will not run & I can't seem to find examples from the two links, can someone give me a direct link to one?

Ive used the syntax above as shown below but im not sure what I am doing wrong.

import javax.swing.*;
import java.awt.*;

public class test {
	//private JFrame guiFrame;
	public class testing extends JPanel{
	
	//JPanel staticText = new JPanel();
	staticText.setLayout(new BorderLayout());
	JLabel label = new JLabel("Welcome Screen", Label.CENTER);
	staticText.add(label, BorderLayout.CENTER);
	guiFrame.add(staticText, BorderLayout.NORTH);
	}

}

and then calling this in my main code

private void initTop() {
		test example = new test();
		 example.testing();
	}

Can anyone tell me what I am doing wrong?
What im trying to do is have the first code as a separate .java file that its method will be called when I need to create that JPanel with the JLabel in the North region of my GUI.

Thanks to anyone that posts.

public class TestPanel extends JPanel{
//create JPanel and add Some JComponents
}

and then

public class Test {
//declare
private TestPanel myTestPanel
.
.
.
public Test() {


myTestPanel = new TestPanel()
myTestPanel.setLayout...
myTestPanel.setpreferredSize...
myFrame.add(myTestPanel, BorderLayout.CENTER);
myFrame.pack();
myFrame.setVisible(true);
}
   public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                Test tP = new Test();
            }
        });
    }
}

Edited 5 Years Ago by mKorbel: code tags and main class

Thanks mKorbel but for some reason I am still getting errors in my code when I try implementing the example you have given me.
What I dont fully understand from the example code is that i would suppose in the first one, i would just declare the Panel and Label like:

public class test extends JPanel{	 //get an error line in test
//saying The serializable class test does not declare a static final serialVersionUID field of type long

	JPanel staticText = new JPanel();
	JLabel label = new JLabel("Welcome Screen", Label.CENTER);
	}

and then

import java.awt.*;
import javax.swing.*;

public class testfinish {
	
		private JFrame guiFrame;
		private test myTestPanel;
		
		
		
		public testfinish() {
			guiFrame = new JFrame("ATM Project GUI");
			guiFrame.setLayout(new BorderLayout());
			initTop();
						
		}
			

		private void initTop() {
			myTestPanel = new test();
			myTestPanel.setLayout(new BorderLayout());
			guiFrame.add(myTestPanel, BorderLayout.NORTH);
			guiFrame.pack();
			guiFrame.setVisible(true);
			
			
		}
		 public static void main(String args[]) {
		        java.awt.EventQueue.invokeLater(new Runnable() {
		 
		            @Override
		            public void run() {
		                test ntesting = new test(); //get an error line in ntesting saying
                            //The local variable ntesting is never read
		            }
		        });
		    }

}

where I am getting errors is mentioned in the code.
I am using Eclipse as my programming application.

I'm not test that maybe someone, but there are plenty of beginers mistakes

1/ ClassName have to starts with Test not test same for TestFinish not testfinish, be sure and check that always, hmmm its very ... to speak about that something

void name is correct :-)

extends meaning to Create this Object or JComponent,

a/ remove JPanel staticText = new JPanel();
b/ add(staticText, , BorderLayout.CENTER);

2/ you forgot to add JLabel

a/ if you remove JPanel staticText = new JPanel();, then just add(label, BorderLayout.CENTER);

b/ if you add(staticText, , BorderLayout.CENTER);, then yoou have to wrote
staticText.add(label, BorderLayout.CENTER);

3/ you have to set preferredSize

a/ if you are using BorderLayout, then you can
- myTestPanel.setPeferredSize(new Dimension (400, 200));

OR

- guiFrame.setPeferredSize(new Dimension (400, 200));
b/ otherwise
- guiFrame.setPeferredSize(new Dimension (400, 200));
c/ using beginers attack guiFrame.setSize((400, 200));

but very well for usage of BorderLayout(), try and add add same JPanel NORTH, CENTER and SOUTH, size is just about PeferredSize

Edited 5 Years Ago by mKorbel: n/a

This article has been dead for over six months. Start a new discussion instead.