0

I'm now creating a simple program with Frame. I, however, face one unknown error that any contents do not appear ,except Panel sized 90 * 90 on the frame. when I adjust windows size by dragging , then the contents appear as I expected. I've tired few methods by googling.. all the same.. not working .

For making this clear, I will attach few screenshots.

  • Before mouse drag enter image description here

  • After mouse drag enter image description here

Java code :

    import java.awt.Color;
import java.awt.Container;
import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

class menuListener implements MenuListener {

    @Override
    public void menuSelected(MenuEvent e) {
        // TODO Auto-generated method stub
        System.out.println("menu clicked !");
        System.exit(0);
    }

    @Override
    public void menuDeselected(MenuEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void menuCanceled(MenuEvent e) {
        // TODO Auto-generated method stub

    }

}

public class pcMain extends JFrame implements Runnable {
    userSeat us;
    int user;
    JPanel[] userSeat;
    Container con;
    JPanel leftPane, rightPane;
    JMenuBar jm;
    JMenu exit;

    public pcMain(int user) {

        super("Pc Management Application");

        con = getContentPane();
        con.setBackground(new Color(0, 128, 128));

        setVisible(true);
        setSize(1100, 500);
        // setLayout(null);
        this.setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Menu bar
        jm = new JMenuBar();

        // Menu items
        exit = new JMenu("EXIT");
        exit.addMenuListener(new menuListener());

        // letPanel
        leftPane = new JPanel();
        leftPane.setLayout(null);
        // leftPane.setSize(800, 470);
        leftPane.setBorder(BorderFactory.createLineBorder(Color.black));
        leftPane.setOpaque(false);
        leftPane.setBounds(0, 0, 800, 500);

        // rightPanel
        rightPane = new JPanel();
        // rightPane.setSize(300, 470);
        rightPane.setBorder(BorderFactory.createLineBorder(Color.red));
        rightPane.setOpaque(false);
        rightPane.setBounds(800, 0, 300, 500);

        // adding all components
        this.user = user;
        jm.add(exit);
        us = new userSeat(user);
        userSeat = us.getSeat();
        setJMenuBar(jm);
        // add(leftPane);
        // add(rightPane);

    }

    @Override
    public void run() {

        int seatX = 10;
        int seatY = 10;

        // Add user seats into leftPane
        for (int i = 0; i < user; i++) {

            if (i != 0 && i % 5 == 0) {
                // Increase y value
                System.out.println("if : " + i);
                seatX = 10;
                seatY += 100;
            }

            // Increase x values
            userSeat[i].setLocation(seatX, seatY);
            leftPane.add(userSeat[i]);
            // add(leftPane);
            System.out.println("else :" + i);
            seatX += 95;

        }

        add(leftPane);

    }

    public static void main(String[] args) {
        new Thread(new pcMain(10)).start();

    }

}
SengJoonHyun
  • 73
  • 1
  • 9
  • 3
    1. Swing is single threaded. Do not modify swing components outside the event dispatch thread. 2. Setting the frame visible should be done *last* after adding all the components and packing the frame. (3. in case an already visible component needs new ones added or removed it needs revalidation and repainting) – kiheru Dec 07 '15 at 15:31
  • @Kiheru, Thank you so much ! this is working as I expected !! Have a nice day ! – SengJoonHyun Dec 07 '15 at 15:44
  • 2
    Why not [answer your own question](http://meta.stackoverflow.com/q/17463/163188) and show how @kiheru's comment helped. – Catalina Island Dec 07 '15 at 15:52
  • 2
    And please **DON'T** use a Null Layout, instead use a [Layout Manager](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). Swing was designed to work with them. For more information see [Why is it frowned upon to use a null layout in Swing](http://stackoverflow.com/questions/6592468/why-is-it-frowned-upon-to-use-a-null-layout-in-swing) and OliverPoulin's [answer](http://stackoverflow.com/a/31270051/2180785). Most of times, this kind of bugs appear because you're using a null layout. – Frakcool Dec 07 '15 at 15:54
  • Thank you, all ! I will do that :) – SengJoonHyun Dec 07 '15 at 15:57

1 Answers1

2

As @Kiheru said I moved the method,setVisibe() to the last and to make it work, I added two methods repaint() & revalidate() after adding the leftPane.

see as below:

enter image description here

Thank you, all :)

SengJoonHyun
  • 73
  • 1
  • 9