0

i have a small problem the component in the JFrame are not showing until i re-size the frame then all component are visible there are something is missing i don't know where can any one tell me where is the problem

import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.JToolBar;
    import javax.swing.border.TitledBorder;

    /**
     *
     * @author isslam
     */
    public class swingInterface extends JFrame{
        JToolBar bar;
        //toolbar button
        JButton openButton,exitButton;
        JTable table;
        public swingInterface(){
        JFrame frame = new JFrame();
        frame.setVisible(true);
        frame.setSize(500,500);
        frame.setLocation(400, 100);
        frame.setLayout(new BorderLayout());
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
         // declration of bar
        bar = new JToolBar("still dragble");

        openButton = new JButton();
        exitButton = new JButton();
        openButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/open-file-icon.png")));
        exitButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/logout-icon.png")));
        bar.add(openButton);
        bar.add(exitButton);
        //panel for jtable
        String[] columnNames = {"First Name",
                            "Last Name",
                            "Sport",
                            "# of Years",
                            "Vegetarian"};
        Object[][] data = {
        {"Kathy", "Smith",
         "Snowboarding", new Integer(5), new Boolean(false)},
        {"John", "Doe",
         "Rowing", new Integer(3), new Boolean(true)},
        {"Sue", "Black",
         "Knitting", new Integer(2), new Boolean(false)},
        {"Jane", "White",
         "Speed reading", new Integer(20), new Boolean(true)},
        {"Joe", "Brown",
         "Pool", new Integer(10), new Boolean(false)}
    };
        table = new JTable(data,columnNames);
        JScrollPane spanel = new  JScrollPane(table);
        TitledBorder titled = new TitledBorder("Title");
        JPanel panel = new JPanel(new BorderLayout());
        panel.setBorder(titled); 
        panel.setBackground(pink);
         panel.add(spanel,BorderLayout.CENTER);


         frame.add(bar,BorderLayout.NORTH);
         frame.add(panel,BorderLayout.CENTER);

        }
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
loverBoy
  • 125
  • 2
  • 13
  • 1
    Because you're setting it visible before all components are added. Notes: 1) Call `pack()` after components are added and before `setVisible(true)` 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 3) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson May 01 '15 at 01:35
  • 1
    thank u now i am reading SSCCE and it work for me now – loverBoy May 01 '15 at 01:42
  • Glad you got it sorted. :) – Andrew Thompson May 01 '15 at 01:47
  • FYI Swing is lazy, this is intentionally, as it makes it faster. This allows you to make a number of modifications, quickly and the post a single update when you're ready, instead of Swing trying to makes updates on each add/remove – MadProgrammer May 01 '15 at 01:57

1 Answers1

2

You are calling your frame's setVisible(true) method before you add all of the desired components to it. Move the call to the end of your constructor.

public swinginterface {
    // adding components to the JFrame
    frame.setVisible(true);
}
TNT
  • 2,900
  • 3
  • 23
  • 34