0

My Jframe is not viewable during executing my java program.After i just resize the frame, all fields are viewable .After i run the java file from eclipse, the output shows only empty frame and then i resize the window all fields are viewable.

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.table.JTableHeader;

public class DBtoExcel extends JFrame { 

    JLabel l1, l2, l3, l4, l5, l6, l7, l8, l9;
    JTextField tf1, tf2,tf3, tf4, tf5, tf6;
    JButton btn1, btn2, upload ,saveAsExcel;
    //JPasswordField p1, p2;
    JTextArea tf7;
    JTable table;
    JScrollPane scrollPane;
    Vector columnNamesVector;
    Vector dataVector;
    JComboBox combo = new JComboBox();
    JScrollPane pane = new JScrollPane(table);
    Vector<String> v = new Vector<String>();
    DBtoExcel(){
        combo.setEditable(true);
        pack();
        //setContentPane(pane);
        setLocationByPlatform(true);
        setResizable(true);
        setVisible(true);
        setSize(600, 800);

        setLayout(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("CUSTOMER INFO");

        l1 = new JLabel("Add customer details");
        l1.setForeground(Color.blue);
        l1.setFont(new Font("Serif", Font.BOLD, 20));

        l2 = new JLabel("Firstname:");
        l3 = new JLabel("Secondname:");
        l4 = new JLabel("Contact No:");
        l5 = new JLabel("Email-ID:");
        l6 = new JLabel("Country:");
        l7 = new JLabel("State:");
        l8 = new JLabel("Description:"); 
        l9 = new JLabel("Config upload:"); 
        tf1 = new JTextField();
        tf2 = new JTextField();
        tf3 = new JTextField();
        tf4 = new JTextField();
        //p1 = new JPasswordField();
        //p2 = new JPasswordField();
        tf5 = new JTextField();
       tf6 = new JTextField();
        tf7 = new JTextArea();









            //create table with data
            JTable table = new JTable(dataVector, columnNamesVector);
            JTableHeader header = table.getTableHeader();
            //;





        btn1 = new JButton("Submit");
        btn2 = new JButton("Clear");
        upload = new JButton("Browse");
        saveAsExcel = new JButton("Save as Excel");
       /* btn1.addActionListener(this);
        btn2.addActionListener(this);
        upload.addActionListener(this);
        saveAsExcel.addActionListener(this);*/
        l1.setBounds(100, 30, 400, 30);
        l2.setBounds(80, 70, 200, 30);
        l3.setBounds(80, 110, 200, 30);
        l4.setBounds(80, 150, 200, 30);
        l5.setBounds(80, 190, 200, 30);
        l6.setBounds(80, 230, 200, 30);
        l7.setBounds(80, 270, 200, 30);
        l8.setBounds(80, 310, 200, 30);
        l9.setBounds(80, 400, 200, 30);
        tf1.setBounds(300, 70, 200, 30);
        tf2.setBounds(300, 110, 200, 30);
        tf3.setBounds(300, 150, 200, 30);
        tf4.setBounds(300, 190, 200, 30);
        //p1.setBounds(300, 150, 200, 30);
        //p2.setBounds(300, 190, 200, 30);
        //tf5.setBounds(300, 235, 200, 30);
        //combo.setBounds(300, 235, 200, 30);
        tf6.setBounds(300, 270, 200, 30);
        tf7.setBounds(300, 310, 200, 70);



        upload.setBounds(300, 400, 100, 30);



        btn1.setBounds(170, 450, 100, 30);
        btn2.setBounds(280, 450, 100, 30);
        saveAsExcel.setBounds(450, 450, 150, 30);
        header.setBounds(80, 500, 800, 30);
        table.setBounds(80, 530, 800, 250);
        table.setBackground(Color.pink);
        add(l1);
        add(l2);
        add(tf1);
        add(l3);
        add(tf2);
        //add(l4);
        //add(p1);
        //add(l5);
        //add(p2);
        add(l4);
        add(tf3);
        add(l5);
        add(tf4);
        add(l6);
        //add(tf5);
        add(l7);
        add(tf6);
        add(l8);
        add(tf7);
        add(l9);
        add(btn1);
        add(upload);
        add(btn2);
        add(saveAsExcel);
        add(header);
        add( table );
       //add(pane);


      }
    public static void main(String args[]){
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        }
        catch (Exception ex){
         ex.printStackTrace();
        }
        System.out.println("enter");
    //new Registration();
        new DBtoExcel();
    }
}`
  • 1
    Move pack() and SetVisible(true) down as the last thing you do in the JFrame constructor – Scherling Feb 01 '16 at 12:18
  • `setLayout(null);` This is at the heart of the problem, in that it must be fixed (by using layouts) before the rest of the problems can be fixed. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Feb 01 '16 at 14:30
  • @A.S.Karthick Take a look at my solution. – user3437460 Feb 01 '16 at 15:01

1 Answers1

3

The reason for the components not showing up despite you already set the bounds for the components is because you are implementing your GUI in a bad manner and in the wrong order.

  • First of all, it is not advisable to extend a class to JFrame unless you have a valid reason doing so, instead you may extend to a JPanel and add your customized JPanel to the JFrame. It is quite rare you will need a customized JFrame.

  • Now, the main reason the components are not showing up is because you set the size of the JFrame first, then you add the components. Java paint manager repaints all components in the JFrame when it detects a change of state (such as being resized / mouse-over). So when you set the size of the JFrame, it will repaint all the components (and at this stage, no components are added yet in your case, so nothing shows up).

  • Since you added the components after setting the size, you can't see it until you manually mouse-over them or resize the JFrame. You can try setting the size after you add all the components, then you will see what I mean. (Components will now show up, but this is not solving the root of the problem.)

You can have a construct like this:

class MainPanel extends JPanel
{
    //Add your members and components in here..
}

public class MainClass{
    public static void main(String[] args){
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new MainPanel());
        frame.pack();  //resize, thus repaint will occur here
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

Last but not least, you may want to use a layout for your containers. The above code is shown for simplicity and clarity. You would want to run the UI in a Event Dispatch Thread instead.

user3437460
  • 17,253
  • 15
  • 58
  • 106
  • frame.setVisible(true) should be the last statement. Otherwise, you could get image flicker. – Gilbert Le Blanc Feb 01 '16 at 15:35
  • and then missing there really used invokeLater for Initial Tread, not just (not the same) - `You would want to run the UI in a Event Dispatch Thread instead.` – mKorbel Feb 01 '16 at 15:35