3

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);

      }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user3133542
  • 1,695
  • 4
  • 21
  • 42
  • 3
    `setLayout(null);` 1) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). 2) Provide ASCII art (or an image with a simple drawing) of the GUI as it should appear in smallest size and (if resizable) with extra width/height. – Andrew Thompson Mar 07 '14 at 02:05
  • i know that layoutmanager are better ,its only for training...nevertheless i do not understand why the textfield on the panel is not appearing. from my point of view j have made all in the right way.... – user3133542 Mar 07 '14 at 02:09
  • *"its only for training."* If this training is "Why we should not use null layouts" you've completed part of it. If it is "how to make a GUI with a null layout" it is difficult and pointless. – Andrew Thompson Mar 07 '14 at 02:19
  • 1
    _"its only for training."_. Help yourself by training with layout managers. Start with [Laying out Components in a Container](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) – Paul Samsotha Mar 07 '14 at 02:21

1 Answers1

12

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();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • +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 – nachokk Mar 07 '14 at 02:24
  • @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. – Andrew Thompson Mar 07 '14 at 02:27
  • 1
    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. – nachokk Mar 07 '14 at 02:32
  • @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. – Andrew Thompson Mar 07 '14 at 02:37
  • it's the netbeans mattisse defacto way of creating a jframe – nachokk Mar 07 '14 at 02:40
  • @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. – Andrew Thompson Mar 07 '14 at 02:51
  • another thing why default package visibility :P , `private` it's ok :D, you have to get 10 votes at least for this answer :) – nachokk Mar 07 '14 at 13:17
  • @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. – Andrew Thompson Mar 08 '14 at 02:47