Question

I am trying to create a simple swing GUI for a Java application that I'm writing, but I'm having a bit of trouble getting things to display on the JPanel, and I was wondering if anyone could point out what I'm doing wrong?

I have the following code in my Gui.java class:

package openDIS;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Gui extends JFrame{

public Gui(){
    setTitle("DIS Filter");
    setSize(1000, 500);
    setLocation (10, 10);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    initGui();
}

/*public quitButton(){
    initGui();
} */

private void initGui(){
    //JFrame frame = new JFrame();
    JPanel panel = new JPanel();

    this.getContentPane().add(panel);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("DIS Filter");
    this.setSize(1000, 500);

    panel.setLayout(null);
    /*Add a JTextArea to display the output DIS information */
    JTextArea displayOutput = new JTextArea();
    panel.add(displayOutput);

    //String data = EspduReceiver.espdu;
    int n = EspduReceiver.entitySite.size();
    for (int i = 0; i < n; i++){
        EspduReceiver.receivePdu();
        System.out.println(EspduReceiver.entitySite.get(i));
        System.out.println(EspduReceiver.entityApplication.get(i));
        System.out.println(EspduReceiver.entity.get(i));
        displayOutput.append(EspduReceiver.entitySite.get(i).toString());
        displayOutput.append(EspduReceiver.entityApplication.get(i).toString());
        displayOutput.append(EspduReceiver.entity.get(i).toString());
    }       





    JButton quitButton = new JButton("Quit");
    panel.add(quitButton);
    quitButton.setBounds(875, 400, 80, 30); /*Set the location of the button in the window, and its size */

    quitButton.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
            System.exit(0);
        }
    });


    panel.add(quitButton);
    //setTitle("Quit");
    //setSize(60,30); /*This line was overwriting the previously set values for the size of the window */
    setLocationRelativeTo(null);
    panel.repaint();
    setDefaultCloseOperation(EXIT_ON_CLOSE);        
}

public static void main(String[] args){ /* I probably don't need a main method here- I have one in EspduReceiver.java */
    SwingUtilities.invokeLater(new Runnable(){
        @Override
        public void run(){
            Gui gui = new Gui();
            gui.setVisible(true);
        }
    });
}




}

Currently, when I run the class, a window opens up that's titled "DIS Filter", and has a 'Quit' button in the bottom right hand corner- the quit button functions correctly.

However, no matter what I try, I can't seem to get the JTextArea to display... can anyone point out what I'm doing wrong here?

Thanks!

Was it helpful?

Solution

You still need a layout, use FlowLayout as a simplest one. Also you have to set JTextArea size.

panel.setLayout(new FlowLayout());

/* Add a JTextArea to display the output DIS information */
JTextArea displayOutput = new JTextArea(50, 50);
panel.add(displayOutput);

OTHER TIPS

Don't use a null layout; give the text area a preferred size, and pack() the enclosing Window.

JTextArea displayOutput = new JTextArea(3, 16);

try this:

displayOutput.setRows(20);
displayOutput.setColumns(5);
displayOutput.setVisible(true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top