Hi Everybody!! I'm new to java and am experementing with GUI and would like if someone could please help me, because I can't figure out how to fix the problem. The part in red is the part where I cannot figure out how to fix it because it says it's an illegal start of an expression. Thanks in advance and taking the time to help and read!

Here it is:

public class MyNew extends JFrame implements ActionListener
 {
    public static void main(String[] args)
    {
    publicMyNew start= newMyNew();

    {

      super("Text Fields");
      JPanel pane=(JPanel) getContentPane();
      pane.setLayout(new BorderLayout());
      givenName=newJTextField(10);
      familyName=newJTextField(10);
      fullName=newJTextField(10);
      fullName=setEditable(false);

      JPanel inFieldPane=newJPanel();
      inFieldPane.setLayout(new GridLayout(2,2));
      inFieldPane.add(newJLabel("Given name"));
      inFieldPane.add(givenName);
      givenName.addActionListener(this);
      inFieldpane.add(new JLabel("Family Name"));
      inFieldPane.add(familyName);
      familyName.addActionListener(this);
      pane.add(inFieldPane,BorderLayout.NORTH);

      JPanel submitPane= newJPanel();
      submitPane.setLayout(new FlowLayout());
      submitPane.add(new JLabel("Press button to enter names"));
      JButton submitButton=newJButton("Submit");
      submitButton.addActionListener(this);
      submitPane.add(submitButton);
      pane.add(submitPane,BorderLayout.CENTER);

      JPanel outFieldPane= new JPanel();
      outFieldPane.setLayout(new GridLayout(1,2));
      outFieldPane.add(newJLabel("Full Name"));
      outFieldPane.add(fullName);
      pane.add(outFieldpane,BorderLayout.SOUTH);


      pack();
      setVisible(true);
    }

    [COLOR="red"]public void actionPerformed (ActionEvent e)[/COLOR]
    {
        if(e.getActionCommand().equals("Submit"))
        {
          String fullString=familyName.getText().trim()+","
                                +givenName.getText(). trim();
            fullName.setText(fullString);
        }   
    }
    }

Recommended Answers

All 4 Replies

please use code tags :-)

public class MyNew extends JFrame implements ActionListener
 {
     public static void main(String[] args)
     {
     publicMyNew start= newMyNew();
                 {
     
      super("Text Fields");
      JPanel pane=(JPanel) getContentPane();
      pane.setLayout(new BorderLayout());
      givenName=newJTextField(10);
      familyName=newJTextField(10);
      fullName=newJTextField(10);
      fullName=setEditable(false);
      
      JPanel inFieldPane=newJPanel();
      inFieldPane.setLayout(new GridLayout(2,2));
      inFieldPane.add(newJLabel("Given name"));
      inFieldPane.add(givenName);
      givenName.addActionListener(this);
      inFieldpane.add(new JLabel("Family Name"));
      inFieldPane.add(familyName);
      familyName.addActionListener(this);
      pane.add(inFieldPane,BorderLayout.NORTH);
      
      JPanel submitPane= newJPanel();
      submitPane.setLayout(new FlowLayout());
      submitPane.add(new JLabel("Press button to enter names"));
      JButton submitButton=newJButton("Submit");
      submitButton.addActionListener(this);
      submitPane.add(submitButton);
      pane.add(submitPane,BorderLayout.CENTER);
      
      JPanel outFieldPane= new JPanel();
      outFieldPane.setLayout(new GridLayout(1,2));
      outFieldPane.add(newJLabel("Full Name"));
      outFieldPane.add(fullName);
      pane.add(outFieldpane,BorderLayout.SOUTH);
      
      
      pack();
      setVisible(true);
     }
} //try this
     
     public void actionPerformed (ActionEvent e)
     {
         if(e.getActionCommand().equals("Submit"))
         {
           String fullString=familyName.getText().trim()+","
                                 +givenName.getText(). trim();
             fullName.setText(fullString);
         }    
     }
     }

thanks.I compiled it and it showed me like 32 errors in the whole program for example cannot find symbol variable/method. Thanks a lot though. Appreciate it.

thanks.I compiled it and it showed me like 32 errors in the whole program for example cannot find symbol variable/method. Thanks a lot though. Appreciate it.

That 32 errors are your mistakes while writing program, namy of them like this

inFieldPane.add(newJLabel("Given name"));

OK, bellow is working version of your program. I mayde some changes to program structure as do prefer create GUI on it own and then just simly call it from main. Also I will recommend to use some other layout manager then BorderLayout, which is not suitable in your case. Check out BoxLayout and GridBagLayout

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

public class MyNew implements ActionListener
{
	JTextField givenName;
	JTextField familyName;
	JTextField fullName;
		
	MyNew()		
	{		
		JFrame jfrm = new JFrame("Text Fields");
		jfrm.setLayout(new BorderLayout());
		jfrm.setSize(200, 200);
		jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		givenName = new JTextField(10);
		familyName = new JTextField(10);
		fullName = new JTextField(10);
		fullName.setEditable(false);
		
		// Set first panel to retrive data from user
		JPanel inFieldPane = new JPanel();
		inFieldPane.setLayout(new GridLayout(2,2));
		inFieldPane.add(new JLabel("Given name"));
		inFieldPane.add(givenName);
		givenName.addActionListener(this);
		inFieldPane.add(new JLabel("Family Name"));
		inFieldPane.add(familyName);
		familyName.addActionListener(this);
		jfrm.add(inFieldPane,BorderLayout.NORTH);
		
		//Set second panel to submit data for processing
		JPanel submitPane = new JPanel();
		submitPane.setLayout(new FlowLayout());
		submitPane.add(new JLabel("Press button to enter names"));
		JButton submitButton = new JButton("Submit");
		submitButton.addActionListener(this);
		submitPane.add(submitButton);
		jfrm.add(submitPane,BorderLayout.CENTER);
		
		// Set third panel to display processed data
		JPanel outFieldPane= new JPanel();
		outFieldPane.setLayout(new GridLayout(1,2));
		outFieldPane.add(new JLabel("Full Name"));
		outFieldPane.add(fullName);
		jfrm.add(outFieldPane,BorderLayout.SOUTH);		
			
		jfrm.setVisible(true);
	}
		
	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand().equals("Submit"))
		{
			String fullString = familyName.getText().trim()+", " +givenName.getText(). trim();
			fullName.setText(fullString);
		}
	}
	
	public static void main(String[] args)
	{
		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				new MyNew();
			}
		});
	}
}

THANKS A LOT. It now works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.