ThatCreepyUncle
ThatCreepyUncle

Reputation: 29

JTextField Not Appearing

I am trying to add a status bar to my JFrame. I have tried to add it but it wont appear on my JFrame. This is being called cause the JFrame appears with the exitButton but it the status bar is no where in site! Please help!

JFrame Code:

package AnimalThing;

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class window {
    public static JFrame frame;
    public static void create(){
        frame = new JFrame("Probe Controller: Exodus I");
        frame.setLayout(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(1600, 900);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setForeground(Color.CYAN);
        frame.getContentPane().setBackground(Color.DARK_GRAY);
        winComp.Add(frame); 
    }
}

winComp Code:

package AnimalThing;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDateTime;

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

public class winComp {
    public static JTextField statusBar;
    public static JButton exitButton, clickedButton;
    public static void Add(JFrame frame){
            AddExitButton(frame);
            AddstatusBar(frame);
    }

    private static void AddstatusBar(JFrame frame) {
            statusBar = new JTextField(1);
            statusBar.setBounds(500, 500, 100, 100);
            statusBar.setText("Hello");
            frame.add(statusBar);
    }

    public static void AddExitButton(JFrame frame){
        exitButton = new JButton("Exit");
        exitButton.setBounds(1, 1, 100, 33);
        exitButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                System.out.println("Exit Button Hit: " + LocalDateTime.now());
                System.exit(0);
            }});
        Font buttonFont = new Font("Thing", Font.BOLD, 16);
        exitButton.setFont(buttonFont);
        exitButton.setFocusPainted( false );
        exitButton.setBackground(Color.BLACK);
        exitButton.setForeground(Color.RED);
        exitButton.setBorderPainted(false);
        frame.add(exitButton);


    }

}

Upvotes: 0

Views: 598

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347324

Basically, you've called setVisible BEFORE you've added any components. Change frame.setResizable(false); to frame.setResizable(true); and try resizing the frame, the fields should appear. You can either call setVisible AFTER you've added the fields or call repaint, which "might" work

I'd encourage you to avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

Generally speaking, winComp.Add(frame); doesn't make sense as it looks like you're trying to add the frame to winComp and is somewhat confusing. The name of the method could change to something else to make it easier to understand the methods intentions.

I'd also encourage you to take a look at Code Conventions for the Java Programming Language because your code is confusing to read

Upvotes: 2

Related Questions