javajinternalframeinvisibleaddchildjdesktoppane

JInternalFrame does not show up


from my MainFrame I create a internal Frame and add that to my desktop pane, but the internal Frame just won't show up. I tried to figure it out for 3 hours now and I kinda need some help with it. Before I implemented the observer pattern it just worked fine. So I thought it got something to do with that and changed some stuff and double checked the crucial code. No solution so far. I just realized how crappy my english is sometimes. Sorry for that!

The 2 code blocks are already shortened. Sorry that they are still pretty long. But I am just not sure where I made the mistake. If you need more code or anything else, just tell me. Thank you very much in advance.

This is the code of my main frame:

public class LangtonsAmeise extends JFrame implements ActionListener {
private JDesktopPane desk;
private JPanel panelButtons;
JMenuBar jmb;
JMenu file,modus;
JMenuItem load,save, exit, mSetzen,mMalen,mLaufen;
JSlider slider;
static int xInt, yInt,xFrame=450,yFrame=450;
static boolean bSetzen = false, bMalen = false, running = false;


Random randomGenerator = new Random();

JLabel xLabel, yLabel, speed, statusText,status;
JButton start, stop, addAnt;
JTextField xField, yField;

public LangtonsAmeise() {
    // Desktop
    desk = new JDesktopPane();
    getContentPane().add(desk, BorderLayout.CENTER);
/*       Those 4 lines work just fine. Internal Frame gets displayed.
    JInternalFrame j = new JInternalFrame();
    desk.add(j);
    j.setSize(new Dimension(300,300));
    j.setVisible(true);
    */


    speed = new JLabel("Geschwindigkeit");

    xLabel = new JLabel("x:");
    yLabel = new JLabel("y:");
    xLabel.setHorizontalAlignment(JLabel.RIGHT);
    yLabel.setHorizontalAlignment(JLabel.RIGHT);
    xLabel.setOpaque(true);
    yLabel.setOpaque(true);
    xField = new JTextField();
    yField = new JTextField();

    xField.setDocument(new IntegerDocument(2));
    yField.setDocument(new IntegerDocument(2));

    start = new JButton("Fenster erstellen");
    stop = new JButton("Stopp");

    start.setMargin(new Insets(0, 0, 0, 0));
    stop.setMargin(new Insets(0, 0, 0, 0));
    // Buttons
    panelButtons = new JPanel();
    panelButtons.setLayout(new GridLayout());       

    panelButtons.add(start);
    panelButtons.add(xLabel);
    panelButtons.add(xField);
    panelButtons.add(yLabel);
    panelButtons.add(yField);
    panelButtons.add(speed);
    panelButtons.add(slider);
    panelButtons.add(new Panel());
    panelButtons.add(stop);

    start.addActionListener(this);
    stop.addActionListener(this);
    add(panelButtons, BorderLayout.NORTH);

    statusText = new JLabel("Aktueller Modus:");
    status = new JLabel("Stopp");
    // JMenuBar
    jmb = new JMenuBar();
    setJMenuBar(jmb);
    file = new JMenu("Datei");
    modus = new JMenu("Modus");
    mLaufen = new JMenuItem("Laufen");
    mMalen = new JMenuItem("Malen");
    mSetzen = new JMenuItem("Setzen");
    load = new JMenuItem("Simulation laden");
    save = new JMenuItem("Simulation speichern");

    mSetzen.addActionListener(this);
    mMalen.addActionListener(this);
    mLaufen.addActionListener(this);
    exit = new JMenuItem("Exit");

    file.add(save);
    file.add(load);
    file.addSeparator();
    file.add(exit);

    save.addActionListener(this);
    load.addActionListener(this);

    modus.add(mLaufen);
    modus.add(mSetzen);
    modus.add(mMalen);

    jmb.add(file);
    jmb.add(modus);
    for (int i = 0; i < 20; i++) {
        jmb.add(new JLabel("    "));            
    }

    jmb.add(statusText);
    jmb.add(status);
    setSize(new Dimension(1000, 900));

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    this.setLocation(dim.width / 2 - this.getSize().width / 2, dim.height
            / 2 - this.getSize().height / 2);
    xField.setText("5");
    yField.setText("5");
    setVisible(true);
}

public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("Fenster erstellen")) {

        if (xField.getText().equals("") || yField.getText().equals("")) {
            new popUpWindow(
                    "Sie müssen eine Zahl zwischen 2 und 99 angeben!!");
        } else {
            xInt = Integer.parseInt(xField.getText());
            yInt = Integer.parseInt(yField.getText());
            state s = new state();
            kindFenster k = new kindFenster(s, xInt, yInt);
            s.addObserver(k);
            addChild(k, this.getSize().width, this.getSize().height);
        }

    }

    if (e.getActionCommand().equals("Stopp")) {
        running=!running;
        status.setText("Stopp");
    }
}

public void addChild(JInternalFrame kind, int xPixel, int yPixel) {
    // kind.setSize(370, 370);
    kind.setLocation(randomGenerator.nextInt(xPixel - kind.getSize().height),
            randomGenerator.nextInt(yPixel - kind.getSize().height - 100));
    kind.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
    desk.add(kind);

    kind.setVisible(true);
    kind.repaint();
    kind.validate();
}

public static void main(String[] args) {
    LangtonsAmeise hauptFenster = new LangtonsAmeise();

}

}

And this is my Internal Frame:

    public class kindFenster extends JInternalFrame implements ActionListener,
    Serializable,Observer  {
/**
 * 
 */
private static final long serialVersionUID = 8939449766068226519L;
static int nr = 0;
static int x,y,xScale,yScale,xFrame,yFrame;
static int sleeptime;
state s;
ArrayList<JButton> jbArray = new ArrayList<>();
ArrayList<ImageIcon> ameisen = new ArrayList<ImageIcon>();
public static ArrayList<AmeiseThread> threadList = new ArrayList<>();

Color alteFarbe, neueFarbe;
JButton save, addAnt;
JPanel panelButtonsKind;
JSlider sliderKind;
public JPanel panelSpielfeld;

static SetzenActionListener sal = new SetzenActionListener();
static MouseMotionActionListener mmal = new MouseMotionActionListener();

public kindFenster(state s,int x, int y) {
    super("Kind " + (++nr), true, true, true, true);
    setLayout(new BorderLayout());
    this.s=s;

    setSize(new Dimension(xFrame, yFrame));

    panelSpielfeld = new JPanel();
    panelSpielfeld.setLayout(new GridLayout(y, x));

    panelButtonsKind = new JPanel();
    panelButtonsKind.setLayout(new GridLayout(1, 3));

    save = new JButton("Simulation speichern");
    addAnt = new JButton("Ameise hinzufügen");
    save.setMargin(new Insets(0, 0, 0, 0));
    addAnt.setMargin(new Insets(0, 0, 0, 0));

    addAnt.addActionListener(this);
    save.addActionListener(this);

    sliderKind = new JSlider(JSlider.HORIZONTAL, 1, 10, 5);
    sliderKind.setSnapToTicks(true);
    sliderKind.setPaintTicks(true);
    sliderKind.setPaintTrack(true);
    sliderKind.setMajorTickSpacing(1);
    sliderKind.setPaintLabels(true);

    sliderKind.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
            if (threadList.size() >= 0) {
                for (AmeiseThread t : threadList) {
                    JSlider source = (JSlider) e.getSource();
                    if (!source.getValueIsAdjusting()) {
                        int speed = source.getValue();
                        sleeptime = 1000 / speed;
                    }
                }
            }
        }
    });

    panelButtonsKind.add(save);
    panelButtonsKind.add(sliderKind);
    panelButtonsKind.add(addAnt);
    add(panelButtonsKind, BorderLayout.NORTH);
    add(panelSpielfeld, BorderLayout.CENTER);
    this.addComponentListener(new MyComponentAdapter());

    setVisible(true);
    repaint();
    validate();
}



public void actionPerformed(ActionEvent e) {

    if (e.getActionCommand().equals("Ameise hinzufügen")) {
        threadList.add(new AmeiseThread(this));
        threadList.get(threadList.size() - 1).start();
    }
    if (e.getActionCommand().equals("Simulation speichern")) {

        OutputStream fos = null;
        try {
            fos = new FileOutputStream("test");
            ObjectOutputStream o = new ObjectOutputStream(fos);
            o.writeObject(this  );
        } catch (IOException e1) {
            System.err.println(e1);
        } finally {
            try {
                fos.close();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }
}

Solution

  • I think it is because you didn't set size for your JInternalFrame. In your code it is as a comment:

    //kind.setSize(370, 370);
    

    when I remove "//" it works for me.