-2

My JTextField isn't showing on, only the paintComponent

public static final int WIDTH = 800;
public static final int HEIGHT = 600;

private JTextField txt;

public Painel(){
    super();
    setFocusable(true);
    setPreferredSize(new Dimension(WIDTH, HEIGHT));
    setLayout(new FlowLayout());
    txt = new JTextField();
    txt.setBounds(400, 300, 50, 20);
}
Dormin
  • 23
  • 1
  • 6
  • 4
    All you did is create a `JTextField` object, you never added it to your panel in any way. – CollinD Jan 30 '17 at 02:53
  • 4
    And `setBounds` will be pointless under the control of layout manager – MadProgrammer Jan 30 '17 at 02:53
  • 1
    This `setLayout(new FlowLayout());` & this `txt.setBounds(400, 300, 50, 20);` are incompatible. The position of a text field is controlled by the layout (+ layout padding and borders). The size of a text field is determined partly by the layout, and also by the number of columns set for it, and the font size. – Andrew Thompson Jan 30 '17 at 02:55
  • Sorry, I could not put the entire class there, but this class is extending JPanel, who is been instantiated in another class extending JFrame – Dormin Jan 30 '17 at 02:55
  • 1
    At least show us 1) what layout manager your JPanel is using, and 2) the code where you do in fact add the JTextField to your JPanel. You will want to create and post a valid [mcve] to show us these things, code we can compile, test and run. Otherwise how can we guess what might be wrong? – Hovercraft Full Of Eels Jan 30 '17 at 02:57
  • 1
    *"Sorry.."* Who are you apologizing to? Tip: Add @MadProgrammer (or whoever, the `@` is important) to *notify* the person of a new comment. *"..I could not put the entire class.."* We never want a 'code dump', but for better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jan 30 '17 at 02:57
  • 1
    You did or didn't add the textfield? I was about to close the question as a duplicate, now I guess I'll vote to close to as it fails to provide a runnable example – MadProgrammer Jan 30 '17 at 02:58
  • `setPreferredSize(new Dimension(WIDTH, HEIGHT));` See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) As a general rule, it is better to layout the components logically (e.g. this component at top of panel, the other component at the bottom) then call `pack()` on the window that contains the panel. – Andrew Thompson Jan 30 '17 at 03:00
  • I didn't, since the class is extending JPanel I thought it shouldn't. Btw, thank you @Andrew Thompson for the tip – Dormin Jan 30 '17 at 03:02
  • *"I didn't, since the class is extending JPanel I thought it shouldn't."* Didn't ..what? Shouldn't ..what? Now is a good time for more words, not less. You are talking to us as if we were sitting behind you, looking over your shoulder at the code. – Andrew Thompson Jan 30 '17 at 03:05
  • 1
    I didn't add JTextField to JPanel since the class is extending JPanel because I thought it shouldn't needed – Dormin Jan 30 '17 at 03:07
  • It **is** needed to add **every** component to the panel (or another panel that is also added). – Andrew Thompson Jan 30 '17 at 03:09
  • @AndrewThompson thank you ! I'm going to remember that – Dormin Jan 30 '17 at 03:20
  • 4
    Programming languages are very dumb -- they only do what you tell them to, and so if you create a component but don't add it to anything, Java will not magically add your component to anything. Even more important, you're asking about use of Swing components but by this question show that you haven't looked in the tutorial first, something you should always do before asking such questions. You really don't want to use "program by guessing" as your heuristic for creating programs. Really. – Hovercraft Full Of Eels Jan 30 '17 at 03:32
  • 3
    @HovercraftFullOfEels While *"program by guessing"* is more accurate, I prefer to think of it as "programming by magic". Great comment from start to finish. – Andrew Thompson Jan 30 '17 at 03:44
  • If anyone knows a better answer than this please go ahead and post it rather keep criticizing this. Everyone responds based on their prior experience and potential and there's nothing to shame on it. No answer is absolutely wrong. So nothing to be shame on at all. You guys should shame on yourself for not helping some one who seeks for help. – Ravindra Ranwala Jan 30 '17 at 06:59
  • @RavindraRanwala You're right. This is the first question I ask on this site and the avalanche of answers so gross that came to impressed me ... I think it would be better to have no answers than to read all this ridiculous language of the majority that came to try to "help" – Dormin Jan 30 '17 at 21:55

1 Answers1

0

You have to set the number of columns in your text field or give a default text to it. The following code should work for you. I have updated the previous answer to use the Gridbag layout. However still you need to set the number of columns in JTextField to render it.

    public class TestFrame extends JFrame {

    public static void main(String[] args) {
        new TestFrame();
    }

    private TestFrame() throws HeadlessException {
        super();

        this.setLocationByPlatform(true);
        JPanel contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[] { 100, 0 };
        gbl_contentPane.rowHeights = new int[] { 0, 0, 0 };
        gbl_contentPane.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
        gbl_contentPane.rowWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
        contentPane.setLayout(gbl_contentPane);

        JTextField textField = new JTextField();
        GridBagConstraints gbc_textField = new GridBagConstraints();
        gbc_textField.insets = new Insets(0, 0, 5, 0);
        gbc_textField.fill = GridBagConstraints.HORIZONTAL;
        gbc_textField.gridx = 1;
        gbc_textField.gridy = 0;
        contentPane.add(textField, gbc_textField);
        textField.setColumns(10);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setVisible(true);
    }
}

Hope this helps. Happy coding !

Ravindra Ranwala
  • 20,744
  • 6
  • 45
  • 63
  • IT WORKED !!! Thank.You.A.LOT !!! How can I thank you for the help? I'm new on this site – Dormin Jan 30 '17 at 03:10
  • `setPreferredSize(new Dimension(WIDTH, HEIGHT));` this is simply a guess at the required size. `txt.setBounds(400, 300, 50, 20);` the layout will ignore that. – Andrew Thompson Jan 30 '17 at 03:11
  • I just set the Layout to null and the JTextField went to the correct position, thanks !! – Dormin Jan 30 '17 at 03:15
  • 2
    I was undecided as to whether to down-vote this answer, but @HovercraftFullOfEels' comment convinced me I should. It is perpetuating bad practices, and ended in a very non-optimal solution by the OP. – Andrew Thompson Jan 30 '17 at 03:47
  • 1
    @AndrewThompson: fyi, answer improved. Vote reversed. – Hovercraft Full Of Eels Jan 31 '17 at 01:30
  • Note that `setBounds(100, 100, 450, 300);` would better be replaced with `setLocation(100, 100);` given the size part will be negated by `pack()`. But even better than either, is `setLocationByPlatform(true);`. You can see the effect & explanation in [this answer](http://stackoverflow.com/a/7143398/418556). @HovercraftFullOfEels thanks for the heads up. Answer now good enough for an up vote. – Andrew Thompson Jan 31 '17 at 03:05