Put a JTextfield on a JPanel?

27,230

enter image description here

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

class TT extends JFrame {

    JTextField textField;
    JPanel panel;
    JButton button1;
    JButton button2;

    public TT() {
        //setSize(300, 300);  // better to use pack() (after components added)
        //setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  // better to use
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        //setLocationRelativeTo(null);  // better to use..
        setLocationByPlatform(true);
        setTitle("Bla Blubb");
        setResizable(false);
        //setLayout(null); // better to use layouts with padding & borders

        // set a flow layout with large hgap and vgap.
        panel = new JPanel(new FlowLayout(SwingConstants.LEADING, 10, 10));
        // panel.setBounds(5, 5, 290, 290); // better to pack()
        add(panel);

        //textField = new JTextField(); // suggest a size in columns
        textField = new JTextField(8);
        //textField.setBounds(5, 5, 280, 50); // to get height, set large font
        textField.setFont(textField.getFont().deriveFont(50f));
        panel.add(textField);

        pack(); // make the GUI the minimum size needed to display the content
        setVisible(true);
    }

    public static void main(String[] args) {
        // GUIS should be constructed on the EDT.
        JFrame tt = new TT();
    }
}
Share:
27,230
user3133542
Author by

user3133542

Updated on March 07, 2020

Comments

  • user3133542
    user3133542 about 4 years

    Why the textfield is not appearing on my panel which is inside my frame? I mean is there some additional action necessary to make the components of the panel visible?

    I hope somebody can help me....

    public class example1  {
    
        public static void main(String[] args) {
    
        JFrame tt=new TT();
        }
    }
    class TT extends JFrame {
    
        JTextField textField;
        JPanel panel;
        JButton button1;
        JButton button2;
    
        public TT() {
            setSize(300, 300);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setTitle("Bla Blubb");
            setResizable(false);
            setLayout(null);
    
            panel=new JPanel();
            panel.setBounds(5, 5, 290, 290);
            add(panel);
    
            textField=new JTextField();
            textField.setBounds(5, 5, 280, 50);
            panel.add(textField);
    
                setVisible(true);
    
          }
    }
    
  • nachokk
    nachokk about 10 years
    +1 great answer but if he still want to use null layout i think that his problem is he didn't suggest a size in columns
  • Andrew Thompson
    Andrew Thompson about 10 years
    @nachokk "if he still want to use null layout.." ..he loses my help, since I don't have the time or patience to work around the innumerable problems with null layouts.
  • nachokk
    nachokk about 10 years
    Ok, only 2 things... you have to ensure to be executed in EDT with invokeLater and is not necessary at all to extend JFrame in this case.
  • Andrew Thompson
    Andrew Thompson about 10 years
    @nachokk I mentioned the first point in passing, though I didn't correct that part (lazy). Good point on the 2nd part, which I forgot to mention. If using a button, we typically just create a button rather than extend it. The same applies to JFrame. The only time to extend frame is when adding functionality. Of the last 1000 frame based apps. I've seen, exactly 0 added functionality to the standard frame.
  • nachokk
    nachokk about 10 years
    it's the netbeans mattisse defacto way of creating a jframe
  • Andrew Thompson
    Andrew Thompson about 10 years
    @nachokk I've always wondered if there is a way to use the GUI builders so that they don't extend frame or panel but instead use an instance.. Otherwise I think it is one reason not to use them.
  • nachokk
    nachokk about 10 years
    another thing why default package visibility :P , private it's ok :D, you have to get 10 votes at least for this answer :)
  • Andrew Thompson
    Andrew Thompson about 10 years
    @nachokk "why default package visibility" That's laziness combined with SSCCE/MCTaRE. One source file can only contain one public class. Since I do many examples that have multiple classes in the same source file (for an SSCCE/MCTaRE), I typically demote them all to default access.