-1

I've searched the internet for solutions and I don't seem to find one that works for me... In case you're wondering, I'm new to Swing. So, here's the thing, JButton appears, but JTextArea doesn't. I don't know what to do to solve this... Help me out guys...

public class FrameCreation
{
    public JFrame createFrame(int width, int height, String name) 
    {
        JFrame frame = new JFrame(name);
        frame.setVisible(true);
        frame.setSize(width, height);
        frame.setLayout(null);
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.getContentPane().setBackground(Color.DARK_GRAY);
        return frame;
    }

    public JButton createButton(int width, int height, int xPos, int yPos, String text) 
    {
        JButton button = new JButton(text);
        button.setBounds(xPos, yPos, width, height);
        button.setBackground(Color.GRAY);
        button.setForeground(Color.WHITE);
        return button;
    }

    public JTextArea createTextArea(int width, int height, int xPos, int yPos)
    {
        JTextArea txt = new JTextArea();
        txt.setVisible(true);
        txt.setBounds(xPos, yPos, width, height);
        txt.setText("Help this poor JTextArea to appear on the frame...");
        return txt;
    }
}

public class Main 
{
    public static void main(String[] args) 
    {
        FrameCreation mainFrame = new FrameCreation();
        JFrame f = new FrameCreation().createFrame(600, 600, "My Frame");
        f.add(mainFrame.createButton(100, 40, 10, 10, "Click me!"));
        f.add(mainFrame.createTextArea(200, 200, 390, 10));
    }
}

3 Answers3

0

Found your real problem; please don't use frame.setLayout(null);. Here is a post explaining why null layout is bad. Instead I have used flow layout and it's working as it supposed to. Here is the code(I have changed them into two classes):

Main.java

import javax.swing.*;

public class Main
{
    public static void main(String[] args)
    {
        FrameCreation mainFrame = new FrameCreation();
        JFrame f = new FrameCreation().createFrame(600, 600, "My Frame");
        f.add(mainFrame.createButton(100, 40, 10, 10, "Click me!"));
        f.add(mainFrame.createTextArea(300, 300, 390, 10));
        f.setVisible(true);
    }
}

FrameCreation.java

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

public class FrameCreation
{
    public JFrame createFrame(int width, int height, String name)
    {
        JFrame frame = new JFrame(name);
        frame.setVisible(true);
        frame.setSize(width, height);
        frame.setLayout(new FlowLayout());
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().setBackground(Color.DARK_GRAY);
        return frame;
    }

    public JButton createButton(int width, int height, int xPos, int yPos, String text)
    {
        JButton button = new JButton(text);
        button.setBounds(xPos, yPos, width, height);
        button.setBackground(Color.GRAY);
        button.setForeground(Color.WHITE);
        return button;
    }

    public JTextArea createTextArea(int width, int height, int xPos, int yPos)
    {
        JTextArea txt = new JTextArea();
        txt.setBounds(xPos, yPos, width, height);
        txt.setText("Help this poor JTextArea to appear on the frame...");
        return txt;
    }
}

Here is the output:

enter image description here

Community
  • 1
  • 1
PseudoAj
  • 5,234
  • 2
  • 17
  • 37
0

Remove the frame.setVisible(true); from FrameCreation class, and add it f.setVisible(true); in the end of main method.

public static void main(String[] args) {
    FrameCreation mainFrame = new FrameCreation();
    JFrame f = new FrameCreation().createFrame(600, 600, "My Frame");
    f.add(mainFrame.createButton(100, 40, 10, 10, "Click me!"));
    f.add(mainFrame.createTextArea(200, 200, 390, 10));
    //This new line
    f.setVisible(true);     
}
Jalal Kiswani
  • 738
  • 5
  • 11
0

When you add components to a visible GUI you need to tell the frame to repaint itself.

So you need to add:

f.revalidate();
f.repaint();

at the end of the main() method.

However, this is NOT the proper solution and is should not be used. You need to redesign your class.

I'm new to Swing

There are too many things wrong with the code to list here. So instead I suggest you start by reading the Swing Tutorial for Swing basics.

Maybe start with the section on How to Use Text Areas. The TextDemo will show you how to better structure your code so that you:

  1. create the Swing components on the Event Dispatch Thread.
  2. create the JTextArea with a reasonable size
  3. use layout managers.
  4. pack() the frame and use setVisible( true ) AFTER all components have been added to the frame.
camickr
  • 321,443
  • 19
  • 166
  • 288