Link to home
Start Free TrialLog in
Avatar of drewmore44
drewmore44

asked on

Java GUI - why isn't this visible?

I have a jtable and a label, an hour ago they were appearing just fine but I did something and now they don't appear, I just see an empty JFrame when I run the project..

dp is the JFrame.


public dp(Shift shift) {
        this.shift = shift;         
       // this.serversTable = serversTable;
        //maybe put this in Shift??
        Object[][] data = new Object[shift.serversOn.size()][6] ;
        for (int curr = 0; curr < shift.serversOn.size(); curr++){
           data[curr][0] = shift.serversOn.get(curr).name;
           data[curr][1] = shift.serversOn.get(curr).totSales;           
           data[curr][2] = shift.serversOn.get(curr).totSales;
           data[curr][3] = shift.serversOn.get(curr).foodSales;
           data[curr][4] = shift.serversOn.get(curr).bevSales;
           data[curr][5] = shift.serversOn.get(curr).covers;
           }
       String [] colNames = {"name","cc tips", "total sales", "food sales", "bev sales", "covers"};   
       DefaultTableModel sm = new DefaultTableModel(data, colNames);
       serversTable = new JTable(sm);
                
      
              initComponents();
        JLabel servers = new JLabel ("servers");
        servers.setVisible(true);
        serversTable.setVisible(true);    
        dp.super.add(servers);
        dp.super.add(new JScrollPane(serversTable));
        dp.super.add(serversTable);
        dp.super.pack(); 
          
               }

Open in new window

Avatar of ksivananth
ksivananth
Flag of United States of America image

where is the container( like JFrame/JDialog )?
Avatar of drewmore44
drewmore44

ASKER

public class dp extends javax.swing.JFrame {
    public JTable serversTable ;
   public Shift shift;
 //  public JTable serversTable;
    /**
     * Creates new form dp
     */
    public dp(Shift shift) {
        this.shift = shift;        
       // this.serversTable = serversTable;
        //maybe put this in Shift??
        Object[][] data = new Object[shift.serversOn.size()][6] ;
        for (int curr = 0; curr < shift.serversOn.size(); curr++){
           data[curr][0] = shift.serversOn.get(curr).name;
           data[curr][1] = shift.serversOn.get(curr).totSales;          
           data[curr][2] = shift.serversOn.get(curr).totSales;
           data[curr][3] = shift.serversOn.get(curr).foodSales;
           data[curr][4] = shift.serversOn.get(curr).bevSales;
           data[curr][5] = shift.serversOn.get(curr).covers;
           }
       String [] colNames = {"name","cc tips", "total sales", "food sales", "bev sales", "covers"};  
       DefaultTableModel sm = new DefaultTableModel(data, colNames);
       serversTable = new JTable(sm);
               
     
              initComponents();
        JLabel servers = new JLabel ("servers");
        servers.setVisible(true);
        serversTable.setVisible(true);    
        dp.super.add(servers);
        dp.super.add(new JScrollPane(serversTable));
        dp.super.add(serversTable);
        dp.super.pack();
         

               }
Avatar of girionis
Try and move the display of the frame outside the constructor, in its own method. And then call it after the constructor has finished initialisation.
use this.setVisible(true); . If you still see any issues, post your entire code.

        dp.super.add(serversTable);
        dp.super.pack();
       
        this.setVisible(true);               <--------
either you have to set a size to the container or set a different layout. the default layout is BoderLayout of JFrame and by default, the components will be added to center of the boder layout which doesn't have a prefered size

another issue is you are just adding all components to center, which will just overlay
see this example to know how border layout lays compoents and how it should be added to specific location

http://www.roseindia.net/java/example/java/awt/borderlayout-example-in-java.shtml
It's still not working... Nothing is visible on the frame at all, although it appears to be the correct size (i.e. the size it was last night when everything was appearing..).

The code is a bit of a mess, as I've been trying everything I can think of to get the components to display..


public class dp2 extends javax.swing.JFrame {
 //   public JTable serversTable ;
   public Shift shift;
 //  public JTable serversTable;
    /**
     * Creates new form dp
     */
    public dp2(Shift shift) {
     //   JFrame dp = new JFrame();
        this.shift = shift;         
       // this.serversTable = serversTable;
        //maybe put this in Shift??
        Object[][] data = new Object[shift.serversOn.size()][6] ;
        for (int curr = 0; curr < shift.serversOn.size(); curr++){
           data[curr][0] = shift.serversOn.get(curr).name;
           data[curr][1] = shift.serversOn.get(curr).totSales;           
           data[curr][2] = shift.serversOn.get(curr).totSales;
           data[curr][3] = shift.serversOn.get(curr).foodSales;
           data[curr][4] = shift.serversOn.get(curr).bevSales;
           data[curr][5] = shift.serversOn.get(curr).covers;
           }
       String [] colNames = {"name","cc tips", "total sales", "food sales", "bev sales", "covers"};   
       DefaultTableModel sm = new DefaultTableModel(data, colNames);
       JTable serversTable = new JTable(sm);
        //     initComponents();
        JLabel servers = new JLabel ("servers");
        servers.setVisible(true);
        serversTable.setVisible(true);    

        add(servers);
        add(new JScrollPane(serversTable));
        add(serversTable);
        setTitle("dp");
        this.pack();        
        this.setVisible(true);
      
             initComponents();

               }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial