1

I am curious to ask that is it possible to transfer a data from one JPanel to another. I'm currently doing a swing java project which has a profile page and a profile edit page. I wanted to try out creating a method to get the text in a textfield in the profile edit page and then return the textfield.getText();. Then I will follow by calling the exact method from the profile page and setting it as a JLabel. However, I wasn't sure how to do this... I updated the codes below. Here is the Profile Edit Page:

package Project;
import java.awt.Color;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.border.LineBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ProfileEdit extends MasterPanel {
    private JTextField txtbxFullName;
    private JTextField txtAdmissionNo;
    private JTextField txtbxContactNo;
    private JTextField txtFieldDiploma;

    /**
     * Create the panel.
     */
    public ProfileEdit(JFrame mf) {
        super(mf);
        
        JLabel lblUserProfile = new JLabel("");
        lblUserProfile.setBorder(BorderFactory.createTitledBorder(null,"User Profile",0,0, new Font("Tahoma", Font.PLAIN, 15), Color.WHITE)); 
        lblUserProfile.setForeground(Color.WHITE);
        lblUserProfile.setFont(new Font("Tahoma", Font.PLAIN, 15));
        lblUserProfile.setBounds(60, 103, 514, 297);
        add(lblUserProfile);
        
        JLabel lblDisplayPic = new JLabel("");
        lblDisplayPic.setBorder(new LineBorder(Color.WHITE));
        lblDisplayPic.setIcon(new ImageIcon(ProfileEdit.class.getResource("/javaProject/images/default-avatar.png")));
        lblDisplayPic.setBounds(100, 132, 90, 100);
        add(lblDisplayPic);
        
        JLabel lblFullName = new JLabel("Full Name:");
        lblFullName.setForeground(Color.WHITE);
        lblFullName.setFont(new Font("Tahoma", Font.PLAIN, 13));
        lblFullName.setBounds(235, 132, 76, 14);
        add(lblFullName);
        
        
        
        JLabel lblGender = new JLabel("Gender: ");
        lblGender.setForeground(Color.WHITE);
        lblGender.setFont(new Font("Tahoma", Font.PLAIN, 13));
        lblGender.setBounds(235, 168, 60, 14);
        add(lblGender);
        
        JRadioButton rdbtnMale = new JRadioButton("Male");
        rdbtnMale.setBackground(Color.DARK_GRAY);
        rdbtnMale.setFont(new Font("Tahoma", Font.PLAIN, 13));
        rdbtnMale.setForeground(Color.WHITE);
        rdbtnMale.setBounds(326, 164, 68, 23);
        add(rdbtnMale);
        
        JRadioButton rdbtnFemale = new JRadioButton("Female");
        rdbtnFemale.setBackground(Color.DARK_GRAY);
        rdbtnFemale.setFont(new Font("Tahoma", Font.PLAIN, 13));
        rdbtnFemale.setForeground(Color.WHITE);
        rdbtnFemale.setBounds(396, 164, 77, 23);
        add(rdbtnFemale);
        
        ButtonGroup genderRdBtn = new ButtonGroup();
        genderRdBtn.add(rdbtnFemale);
        genderRdBtn.add(rdbtnMale);
        
        JLabel lblAdmissionNo = new JLabel("Admission No:");
        lblAdmissionNo.setForeground(Color.WHITE);
        lblAdmissionNo.setFont(new Font("Tahoma", Font.PLAIN, 13));
        lblAdmissionNo.setBounds(235, 203, 97, 14);
        add(lblAdmissionNo);
        
        JLabel lblContactNo = new JLabel("Contact No:");
        lblContactNo.setForeground(Color.WHITE);
        lblContactNo.setFont(new Font("Tahoma", Font.PLAIN, 13));
        lblContactNo.setBounds(235, 240, 77, 14);
        add(lblContactNo);
        
        JLabel lblDiploma = new JLabel("Diploma In:");
        lblDiploma.setForeground(Color.WHITE);
        lblDiploma.setFont(new Font("Tahoma", Font.PLAIN, 13));
        lblDiploma.setBounds(235, 275, 76, 14);
        add(lblDiploma);
        
        txtbxFullName = new JTextField();
        txtbxFullName.setBounds(326, 130, 147, 20);
        add(txtbxFullName);
        txtbxFullName.setColumns(10);
        
        txtAdmissionNo = new JTextField();
        txtAdmissionNo.setBounds(326, 201, 147, 20);
        add(txtAdmissionNo);
        txtAdmissionNo.setColumns(10);
        
        txtbxContactNo = new JTextField();
        txtbxContactNo.setBounds(326, 238, 147, 20);
        add(txtbxContactNo);
        txtbxContactNo.setColumns(10);
        
        txtFieldDiploma = new JTextField();
        txtFieldDiploma.setBounds(326, 273, 147, 20);
        add(txtFieldDiploma);
        txtFieldDiploma.setColumns(10);
        
        JButton btnSubmit = new JButton("SUBMIT");
        btnSubmit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Home home = new Home(mf);
                mf.setContentPane(home);
                mf.setVisible(true);
            }
        });
        btnSubmit.setBounds(235, 336, 114, 23);
        add(btnSubmit);
        
        JButton btnCancel = new JButton("CANCEL");
        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Home home = new Home(mf);
                mf.setContentPane(home);
                mf.setVisible(true);
            }
        });
        btnCancel.setBounds(359, 336, 114, 23);
        add(btnCancel);
        
        JButton btnBrowse = new JButton("Browse");
        btnBrowse.setBounds(101, 247, 89, 23);
        add(btnBrowse);
    }
    
    
    
}

Here is the Profile Page :

package Project;
import java.awt.Color;
import Project.ProfileEdit;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.border.LineBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import Project.ProfileEdit;

public class Home extends MasterPanel {

    /**
     * Create the panel.
     */
    public Home(JFrame mf) {
        super(mf);
        
        
        JLabel lblUserProfile = new JLabel("");
        //createTitledBorder(border, string, int, int, font, color)
        lblUserProfile.setBorder(BorderFactory.createTitledBorder(null,"User Profile",0,0, new Font("Tahoma", Font.PLAIN, 15), Color.WHITE)); 
        //setBounds(left, top, right, bottom)
        lblUserProfile.setBounds(60, 103, 510, 267);
        add(lblUserProfile);
        
        JLabel lblDisplayPic = new JLabel("");
        lblDisplayPic.setBorder(new LineBorder(Color.WHITE));
        lblDisplayPic.setIcon(new ImageIcon(Home.class.getResource("/javaProject/images/default-avatar.png")));
        lblDisplayPic.setBounds(108, 142, 90, 100);
        add(lblDisplayPic);
        
        JLabel lblFullName = new JLabel("Full Name:");
        lblFullName.setForeground(Color.WHITE);
        lblFullName.setFont(new Font("Tahoma", Font.PLAIN, 13));
        lblFullName.setBounds(251, 131, 76, 14);
        add(lblFullName);
        
        
        
        JLabel lblGender = new JLabel("Gender: ");
        lblGender.setForeground(Color.WHITE);
        lblGender.setFont(new Font("Tahoma", Font.PLAIN, 13));
        lblGender.setBounds(251, 151, 60, 14);
        add(lblGender);
        
        JLabel lblAdmissionNo = new JLabel("Admission No:");
        lblAdmissionNo.setForeground(Color.WHITE);
        lblAdmissionNo.setFont(new Font("Tahoma", Font.PLAIN, 13));
        lblAdmissionNo.setBounds(251, 176, 97, 14);
        add(lblAdmissionNo);
        
        JLabel lblContactNo = new JLabel("Contact No:");
        lblContactNo.setForeground(Color.WHITE);
        lblContactNo.setFont(new Font("Tahoma", Font.PLAIN, 13));
        lblContactNo.setBounds(251, 195, 77, 14);
        add(lblContactNo);
        
        JLabel lblDiploma = new JLabel("Diploma In:");
        lblDiploma.setForeground(Color.WHITE);
        lblDiploma.setFont(new Font("Tahoma", Font.PLAIN, 13));
        lblDiploma.setBounds(251, 218, 76, 14);
        add(lblDiploma);
        
        JButton btnEdit = new JButton("Edit ");
        btnEdit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                
                ProfileEdit ProfileEdit = new ProfileEdit(mf);
                mf.setContentPane(ProfileEdit);
                mf.setVisible(true);
                
            }
        });
        btnEdit.setBounds(251, 309, 89, 23);
        add(btnEdit);
    }
    
}

and Lastly, here is the Master Panel if you guys need it :

package Project;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;

import java.awt.Color;
import javax.swing.JSeparator;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MasterPanel extends JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    protected JFrame myFrame = null;

    /**
     * Create the panel.
     */
    public MasterPanel(JFrame mf) {
        myFrame = mf;
        setBackground(Color.DARK_GRAY);
        setLayout(null);
        
        //set a new menubar for home
        JMenuBar menuBarHome = new JMenuBar();
        menuBarHome.setForeground(Color.BLACK);
        menuBarHome.setBackground(Color.WHITE);
        // x,y,width,height
        // >x = right, <x = left
        // >y = bottom, <y = up
        menuBarHome.setBounds(80, 40, 97, 21);
        //length and height
        menuBarHome.setSize(48, 20);
        add(menuBarHome);
        JMenu home = new JMenu("HOME");
        home.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
            
                Home home = new Home(mf);
                mf.setContentPane(home);
                mf.setVisible(true);
                
            }
        });
        menuBarHome.add(home);
        
        //set a new menubar for calendar
        JMenuBar menuBarCalendar = new JMenuBar();
        menuBarCalendar.setForeground(Color.BLACK);
        menuBarCalendar.setBackground(Color.WHITE);
        menuBarCalendar.setBounds(145, 40, 97, 21);
        menuBarCalendar.setSize(75, 20);
        add(menuBarCalendar);
        JMenu calendar = new JMenu("CALENDAR");
        calendar.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                
                Calendar calendar = new Calendar(mf);
                mf.setContentPane(calendar);
                mf.setVisible(true);
                
            }
        });
        menuBarCalendar.add(calendar);
        
        JMenuBar menuBarAcademic = new JMenuBar();
        menuBarAcademic.setForeground(Color.BLACK);
        menuBarAcademic.setBackground(Color.WHITE);
        menuBarAcademic.setBounds(235, 40, 220, 21);
        menuBarAcademic.setSize(72, 20);
        add(menuBarAcademic);
        JMenu academic = new JMenu("ACADEMIC");
        menuBarAcademic.add(academic);

        //add sub menu into the academic
        JMenuItem lectureNotes = new JMenuItem("Lecture Notes");
        lectureNotes.setForeground(Color.BLACK);
        lectureNotes.setBackground(Color.WHITE);
        lectureNotes.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                LectureNotes LectureNotes = new LectureNotes(mf);
                mf.setContentPane(LectureNotes);
                mf.setVisible(true);
            }       
        });
        academic.add(lectureNotes);
        
        
        
        JMenuItem resulttracker = new JMenuItem("Result Tracker");
        resulttracker.setForeground(Color.BLACK);
        resulttracker.setBackground(Color.WHITE);
        resulttracker.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                ResultTracker resulttracker = new ResultTracker(mf);
                mf.setContentPane(resulttracker);
                mf.setVisible(true);
            }       
        });
        academic.add(resulttracker);
        
        JMenuItem studyplan = new JMenuItem("Study Planner");
        studyplan.setForeground(Color.BLACK);
        studyplan.setBackground(Color.WHITE);
        studyplan.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                StudyPlanner studyplanner = new StudyPlanner(mf);
                mf.setContentPane(studyplanner);
                mf.setVisible(true);
            }
        });
        
        academic.add(studyplan);
        
        JMenuItem timetable = new JMenuItem("Timetable");
        timetable.setForeground(Color.BLACK);
        timetable.setBackground(Color.WHITE);
        timetable.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                TimeTable timetable = new TimeTable(mf);
                mf.setContentPane(timetable);
                mf.setVisible(true);
            }
        });
        
        academic.add(timetable);
        
        JMenuBar menuBarEmail = new JMenuBar();
        menuBarEmail.setForeground(Color.BLACK);
        menuBarEmail.setBackground(Color.WHITE);
        menuBarEmail.setBounds(320, 40, 97, 21);
        menuBarEmail.setSize(47, 20);
        add(menuBarEmail);
        JMenu email = new JMenu("EMAIL");
        menuBarEmail.add(email);
        
        JMenuItem composeemail = new JMenuItem("Compose");
        composeemail.setForeground(Color.BLACK);
        composeemail.setBackground(Color.WHITE);
        composeemail.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                Email email = new Email(mf);
                mf.setContentPane(email);
                mf.setVisible(true);
            }
        });
        email.add(composeemail);
        JMenuItem inboxemail = new JMenuItem("Inbox");
        inboxemail.setForeground(Color.BLACK);
        inboxemail.setBackground(Color.WHITE);
        email.add(inboxemail);
        
        
        JMenuBar menuBarContactus = new JMenuBar();
        menuBarContactus.setForeground(Color.BLACK);
        menuBarContactus.setBackground(Color.WHITE);
        menuBarContactus.setBounds(380, 40, 97, 21);
        menuBarContactus.setSize(85, 20);
        add(menuBarContactus);
        JMenu contactus= new JMenu("CONTACT US");
        contactus.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e)  {
                ContactUs contactus = new ContactUs(mf);
                mf.setContentPane(contactus);
                mf.setVisible(true);
            }
        });
        menuBarContactus.add(contactus);
        
        
        JMenuBar menuBarLogout = new JMenuBar();
        menuBarLogout.setForeground(Color.BLACK);
        menuBarLogout.setBackground(Color.WHITE);
        menuBarLogout.setBounds(480, 40, 97, 21);
        menuBarLogout.setSize(60, 20);
        add(menuBarLogout);
        JMenu logout = new JMenu("LOGOUT");
        logout.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Login login = new Login(mf);
                mf.setContentPane(login);
                mf.setVisible(true);
                
            }
        });
        menuBarLogout.add(logout);
        
        JSeparator separatorTop = new JSeparator();
        separatorTop.setBounds(50, 23, 524, 2);
        add(separatorTop);
        
        JSeparator separatorBottom = new JSeparator();
        separatorBottom.setBounds(50, 79, 524, 2);
        add(separatorBottom);
        
        
    }
    
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Irminrics
  • 21
  • 7
  • Possible duplicate of [*How to wire one pane to another*](http://stackoverflow.com/q/10523343/230513). – trashgod Dec 06 '16 at 15:21
  • @trashgod thanks! I will have a look there to find out what information I can pick. – Irminrics Dec 06 '16 at 15:49
  • 2
    Also don't use `null layout`, read: [Null layout is evil](http://www.leepoint.net/GUI/layouts/nulllayout.html) and [Why is it frowned upon to use null layout in swing?](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) – Frakcool Dec 06 '16 at 17:00
  • The easiest way is to create a plain Java getter / setter Profile class and pass an instance of that class to your JPanels. – Gilbert Le Blanc Apr 05 '21 at 15:23

0 Answers0