Java take input string in one Jframe and display in another

I have to get a String input in one JFrame and display in another. My second task is to flash the given string in a larger font in the second frame, at an interval of 1sec. How to proceed?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Input{

    String hinput; 
    private JFrame mainFrame;
    private JLabel headerLabel;
    private JLabel statusLabel;
    private JPanel controlPanel;

    private void prepareGUI(){
         mainFrame = new JFrame("STRING");
         mainFrame.setSize(500,100); 
         headerLabel = new JLabel("", JLabel.CENTER);        
         controlPanel = new JPanel();
         controlPanel.setLayout(new FlowLayout());
         mainFrame.add(headerLabel);
         mainFrame.add(controlPanel);
         mainFrame.setVisible(true);  
    }

    private void showTextField(){

         JLabel  stringlabel= new JLabel("String ", JLabel.RIGHT);
         final JTextField userText = new JTextField(20);    
         JButton submitButton = new JButton("Submit");
         submitButton.addActionListener(new mylistener());
         submitButton.setActionCommand("open");
         controlPanel.add(stringlabel);
         controlPanel.add(userText);
         controlPanel.add(submitButton);
         mainFrame.setVisible(true);  

    }
    private class mylistener implements ActionListener{
        public void actionPerformed(ActionEvent e){
        String cmd = e.getActionCommand();
        if(cmd.equals("open")){
            mainFrame.dispose();
            NewJFrame nj= new NewJFrame(hinput);
        }
        }
    }
    public static void main(String args[]){
         Input  Inp = new Input();      
         Inp.prepareGUI();
         Inp.showTextField();
    }
}
class NewJFrame{

   JFrame mainFrame;
   String text;
   JLabel l1;
   JTextField tb1;
   public NewJFrame(String t){

          text=t;
          mainFrame=new JFrame("STRING");
          mainFrame.setSize(800,800);

          mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          l1=new JLabel("Entered string");
          tb1.setText(text);
          mainFrame.add(l1);
          mainFrame.add(tb1);
          mainFrame.setVisible(true);
   }
}    

I am getting traceback after i click ‘submit’ button. Please point out the errors.

Answer

You can get rid of the error by instatiating tb1 in your NewJFrame class like so:

class NewJFrame{

   JFrame mainFrame;
   String text;
   JLabel l1;
   JTextField tb1;
   public NewJFrame(String t){

          text=t;
          mainFrame=new JFrame("STRING");
          mainFrame.setSize(800,800);

          // *** must init tb1!!! ***///
          JTextField tb1 = new JTextField();

          mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          l1=new JLabel("Entered string");
          tb1.setText(text);
          mainFrame.add(l1);
          mainFrame.add(tb1);
          mainFrame.setVisible(true);
   }
} 

As for getting text typed in one JFrame to open in another, I have a slightly modified solution. Maybe have text entered in a JTextField on one JPanel display in another JPanel. To do that, you could use the following code:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.FlowLayout;

public class SimpleGUI extends JFrame {

    private final JPanel firstPanel;
    private final JPanel secondPanel;
    private final JButton submitButton;
    private final JTextField textField;
    private final JLabel secondPanelLabel;

    public SimpleGUI() {

        // sets the title of the JFrame
        super("SimpleGUI");

        setLayout(new FlowLayout());

        // inits both JPanels
        firstPanel = new JPanel();
        secondPanel = new JPanel();

        // inits empty second JLabel and adds to the secondPanel
        secondPanelLabel = new JLabel();
        secondPanel.add(secondPanelLabel);

        // makes the secondPanel invisible for the time being
        secondPanel.setVisible(false);

        // inits the submit button
        submitButton = new JButton("Submit");

        // event-handler for submit button, will set the text in the
        // secondPanelLabel to the text in the JTextField the user types
        // into. It then makes the firstPanel (with the text field and button),
        // invisible, and then makes the second panel visible.
        submitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                secondPanelLabel.setText(textField.getText());
                firstPanel.setVisible(false);
                secondPanel.setVisible(true);
            }
        });

        // inits the textField
        textField = new JTextField(10);

        // adds the button and the text field to the firstPanel
        firstPanel.add(submitButton);
        firstPanel.add(textField);

        // adds both panels to this JFrame
        this.add(firstPanel);
        this.add(secondPanel);
    }
}

And here is a class with a main method that constructs the SimpleGUI so you can test it out for yourself:

import javax.swing.JFrame;

public class SimpleGUITest {
    public static void main(String[] args) {
        SimpleGUI frame = new SimpleGUI();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *