Search code examples
javaswingjframejpaneljtextfield

Add JTextField in A new JFrame


I have a JFrame with a JButton , this button open a new JFrame where there should be a text box ( JTextField ) that I will use for a search , the problem is that I don't know how to insert it . I came up with this :

N.B I'm a beginner, sorry in advance for the easy question :)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class MainWindow {




// Seconda Finestra
public static void NuovaFinestra (JPanel panel) {

    panel.setLayout(null);



    JButton Ricerca = new JButton("Ricerca");
    Ricerca.setBounds(100, 100, 200, 50);
    panel.add(Ricerca);


    Ricerca.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e) {
             JFrame FinestradiRicerca = new JFrame("Finestra di Ricerca");
             FinestradiRicerca.setBounds(300, 300, 500, 500);

             FinestradiRicerca.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             JPanel riquadroRicerca = new JPanel();
             FinestradiRicerca.add(riquadroRicerca);
             FinestradiRicerca.setVisible(true);
             JTextField ciao;
                ciao = new JTextField ();
                 }
    });

}




//Main  
public static void main(String[] args) {

    //Finestra Principale
    JFrame finestra = new JFrame("Finestra principale");
    finestra.setSize(500, 500);
    finestra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


//JPanel della finestra principale
    JPanel riquadro = new JPanel();
    finestra.add(riquadro);
    finestra.setVisible(true);

    NuovaFinestra(riquadro);

}





}

Solution

  • You needed to add your new elements to riquadroRicerca BEFORE adding the Panel to FinestradiRicerca, I recommend you NOT to use null layout but a Layout Manager or combinations of them. If you insist on keeping null layout then see below example. But for this kind of app I'd suggest a CardLayout.

    I also suggest not using multiple JFrames since they will open multiple windows on task bar which is user unfriendly. See: Use of multiple JFrames, Good / Bad Practice

    As an aside note, follow Java naming conventions. For example you called a JFrame as FinestradiRicerca instead rename it to: finestradiRicerca (1st letter of a variable in lowercase).

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class MainWindow {
        // Seconda Finestra
        public static void NuovaFinestra (JPanel panel) {
            panel.setLayout(null);
            JButton Ricerca = new JButton("Ricerca");
            Ricerca.setBounds(100, 100, 200, 50);
            panel.add(Ricerca);
            Ricerca.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    JFrame FinestradiRicerca = new JFrame("Finestra di Ricerca");
                    FinestradiRicerca.setBounds(300, 300, 500, 500);
                    //If you don't want to close whole app when closing this windo change it to: JFrame.DISPOSE_ON_CLOSE
                    FinestradiRicerca.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    JPanel riquadroRicerca = new JPanel();
                    JTextField ciao;
                    JLabel myLabel = new JLabel("Here goes your label text");
                    ciao = new JTextField ();
                    ciao.setColumns(20);
                    riquadroRicerca.add(myLabel);
                    riquadroRicerca.add(ciao);
                    FinestradiRicerca.add(riquadroRicerca);
                    FinestradiRicerca.setVisible(true);
                }
            });
        }
    
        //Main  
        public static void main(String[] args) {
            //Finestra Principale
            JFrame finestra = new JFrame("Finestra principale");
            finestra.setSize(500, 500);
            finestra.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //JPanel della finestra principale
            JPanel riquadro = new JPanel();
            finestra.add(riquadro);
            finestra.setVisible(true);
            NuovaFinestra(riquadro);
        }
    }
    

    So, your code after a few modifications, to make JLabel and JTextField visible gives the following output:

    enter image description here

    However, please follow my above recomendations.