Am I Calling My Class Correctly?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 1107 Views - Last Post: 03 May 2011 - 10:54 AM Rate Topic: -----

Topic Sponsor:

#1 RandomlyKnighted  Icon User is offline

  • Microsoft Student Partner
  • member icon

Reputation: 103
  • View blog
  • Posts: 1,215
  • Joined: 14-January 10

Am I Calling My Class Correctly?

Posted 02 May 2011 - 09:28 AM

I'm trying to call my main class from another java file that is in my project. Here is the class I'm trying to call:

package gwatoolbox;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout.*;
import javax.swing.JButton.*;
import javax.swing.JCheckBox.*;
import javax.swing.JFrame.*;
import javax.swing.JPanel.*;
import javax.swing.JTabbedPane.*;
import javax.swing.*;

/*
 *  @author Tyler
 */
public class GWAToolbox extends JFrame
{    
    public void GWAToolbox()
    {  
        // Puts the application name at the top of the window.
        setTitle("GWA Toolbox");  
        setSize(300,300);

        // Creates a tabbed window.
        JTabbedPane jtp = new JTabbedPane();
        getContentPane().add(jtp);

        JPanel jp1 = new JPanel();
        JPanel jp2 = new JPanel();
        JPanel jp3 = new JPanel();
                
        JCheckBox checkbox1 = new JCheckBox();
        checkbox1.setText("Clear Temporary Internet Files");
        jp2.add(checkbox1);
        
        JCheckBox checkbox2 = new JCheckBox();
        checkbox2.setText("Clear Browser History");
        jp2.add(checkbox2);
        
        JCheckBox checkbox3 = new JCheckBox();
        checkbox3.setText("Clear Browser Cookies");
        jp2.add(checkbox3);
        
        JButton button1 = new JButton();
        button1.setText("Shred!");
        jp2.add(button1);
        
        jtp.addTab("Home", jp1);
        jtp.addTab("Privacy Tune Up", jp2);
        jtp.addTab("Utility Tests", jp3);
       
    }

    public static void main(String[] args)
    {
        GWAToolbox gwa = new GWAToolbox();
        gwa.GWAToolbox();
        gwa.setSize(800, 400);
        gwa.setVisible(true);

    }
}



And here is the class that I am trying to call it from:

package gwatoolbox;
import javax.swing.JCheckBox.*;
import javax.swing.JFrame.*;
import javax.swing.JPanel.*;
import javax.swing.JTabbedPane.*;
import javax.swing.*;

/*
 *  @author Tyler
 */
public class CheckOS
{    
    public void CheckOS()
    {
        // Gather GUI information from GWAToolbox.java
        GWAToolbox toolbox = new GWAToolbox();
        toolbox.GWAToolbox();
        
        // Determine which operating system the user is running.
        JLabel label1 = new JLabel();
        String OSname = System.getProperty("os.name");
        
        if (OSname.startsWith("Windows"))
        {
            label1.setText("You are a commoner, because you are running Windows.");
            //jp1.add(label1);
        }
        else if (OSname.equals("Mac OS X"))
        {
            label1.setText("You must have a lot of money, because you have Mac.");
            //jp1.add(label1);
        }
        else if (OSname.equals("Linux"))
        {
            label1.setText("You must be thrifty, because you are running Linux, which is free.");
            //jp1.add(label1);
        }
    }
}



I'm getting a Symbol Not Found error under jp1 in the 2nd class so I know that it has something to do with the way I'm calling my main class since I created the JPanel in the main class. Any help will be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Am I Calling My Class Correctly?

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1407
  • View blog
  • Posts: 6,022
  • Joined: 20-September 08

Re: Am I Calling My Class Correctly?

Posted 02 May 2011 - 09:34 AM

Quote

toolbox.GWAToolbox();


Makes no sense - you should remove it

This post has been edited by g00se: 02 May 2011 - 09:35 AM

Was This Post Helpful? 0
  • +
  • -

#3 v0rtex  Icon User is offline

  • I Will Try Give You (0x3A28213A)ers
  • member icon

Reputation: 179
  • View blog
  • Posts: 664
  • Joined: 02-June 10

Re: Am I Calling My Class Correctly?

Posted 02 May 2011 - 09:37 AM

I would recommend rather over-riding the constructor of your GWAToolbox class like so:

package gwatoolbox;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout.*;
import javax.swing.JButton.*;
import javax.swing.JCheckBox.*;
import javax.swing.JFrame.*;
import javax.swing.JPanel.*;
import javax.swing.JTabbedPane.*;
import javax.swing.*;

/*
 *  @author Tyler
 */
public class GWAToolbox extends JFrame
{    
   public GWAToolbox() 
    {  
        // Puts the application name at the top of the window.
        setTitle("GWA Toolbox");  
        setSize(800,400); //put setSize method here

        // Creates a tabbed window.
        JTabbedPane jtp = new JTabbedPane();
        getContentPane().add(jtp);

        JPanel jp1 = new JPanel();
        JPanel jp2 = new JPanel();
        JPanel jp3 = new JPanel();
                
        JCheckBox checkbox1 = new JCheckBox();
        checkbox1.setText("Clear Temporary Internet Files");
        jp2.add(checkbox1);
        
        JCheckBox checkbox2 = new JCheckBox();
        checkbox2.setText("Clear Browser History");
        jp2.add(checkbox2);
        
        JCheckBox checkbox3 = new JCheckBox();
        checkbox3.setText("Clear Browser Cookies");
        jp2.add(checkbox3);
        
        JButton button1 = new JButton();
        button1.setText("Shred!");
        jp2.add(button1);
        
        jtp.addTab("Home", jp1);
        jtp.addTab("Privacy Tune Up", jp2);
        jtp.addTab("Utility Tests", jp3);
      //after all graphical elements are made, show them here  
        setVisible(true);
    }

    public static void main(String[] args)
    {
        new GWAToolbox(); /*note if you need to change stuff then simply change your constructor
        public GWAToolbox(int xsize, int ysize, boolean visible = 800, 400, true */
    }
}



then at your checkOS code, you simply call the GWAToolbox constructor again and that should be fine:

package gwatoolbox;
import javax.swing.JCheckBox.*;
import javax.swing.JFrame.*;
import javax.swing.JPanel.*;
import javax.swing.JTabbedPane.*;
import javax.swing.*;

/*
 *  @author Tyler
 */
public class CheckOS
{    
    public void CheckOS()
    {
        // Gather GUI information from GWAToolbox.java
        new GWAToolbox();
        
        
        // Determine which operating system the user is running.
        JLabel label1 = new JLabel();
        String OSname = System.getProperty("os.name");
        
        if (OSname.startsWith("Windows"))
        {
            label1.setText("You are a commoner, because you are running Windows.");
            //jp1.add(label1);
        }
        else if (OSname.equals("Mac OS X"))
        {
            label1.setText("You must have a lot of money, because you have Mac.");
            //jp1.add(label1);
        }
        else if (OSname.equals("Linux"))
        {
            label1.setText("You must be thrifty, because you are running Linux, which is free.");
            //jp1.add(label1);
        }
    }
}



I hope this helps,
v0rtex

This post has been edited by v0rtex: 02 May 2011 - 09:39 AM

Was This Post Helpful? 1
  • +
  • -

#4 RandomlyKnighted  Icon User is offline

  • Microsoft Student Partner
  • member icon

Reputation: 103
  • View blog
  • Posts: 1,215
  • Joined: 14-January 10

Re: Am I Calling My Class Correctly?

Posted 02 May 2011 - 09:47 AM

Ok here is what I have now:

package gwatoolbox;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout.*;
import javax.swing.JButton.*;
import javax.swing.JCheckBox.*;
import javax.swing.JFrame.*;
import javax.swing.JPanel.*;
import javax.swing.JTabbedPane.*;
import javax.swing.*;

/*
 *  @author Tyler
 */
public class GWAToolbox extends JFrame
{    
    public void GWAToolbox()
    {  
        // Puts the application name at the top of the window.
        setTitle("GWA Toolbox");  
        setSize(800,400);

        // Creates a tabbed window.
        JTabbedPane jtp = new JTabbedPane();
        getContentPane().add(jtp);

        JPanel jp1 = new JPanel();
        JPanel jp2 = new JPanel();
        JPanel jp3 = new JPanel();
                
        JCheckBox checkbox1 = new JCheckBox();
        checkbox1.setText("Clear Temporary Internet Files");
        jp2.add(checkbox1);
        
        JCheckBox checkbox2 = new JCheckBox();
        checkbox2.setText("Clear Browser History");
        jp2.add(checkbox2);
        
        JCheckBox checkbox3 = new JCheckBox();
        checkbox3.setText("Clear Browser Cookies");
        jp2.add(checkbox3);
        
        JButton button1 = new JButton();
        button1.setText("Shred!");
        jp2.add(button1);
        
        jtp.addTab("Home", jp1);
        jtp.addTab("Privacy Tune Up", jp2);
        jtp.addTab("Utility Tests", jp3);
        setVisible(true);
       
    }

    public static void main(String[] args)
    {
        new GWAToolbox();
    }
}



package gwatoolbox;
import javax.swing.JCheckBox.*;
import javax.swing.JFrame.*;
import javax.swing.JPanel.*;
import javax.swing.JTabbedPane.*;
import javax.swing.*;

/*
 *  @author Tyler
 */
public class CheckOS
{    
    public void CheckOS()
    {
        // Gather GUI information from GWAToolbox.java
        new GWAToolbox();
        
        // Determine which operating system the user is running.
        JLabel label1 = new JLabel();
        String OSname = System.getProperty("os.name");
        
        if (OSname.startsWith("Windows"))
        {
            label1.setText("You are a commoner, because you are running Windows.");
            jp1.add(label1);
        }
        else if (OSname.equals("Mac OS X"))
        {
            label1.setText("You must have a lot of money, because you have Mac.");
            jp1.add(label1);
        }
        else if (OSname.equals("Linux"))
        {
            label1.setText("You must be thrifty, because you are running Linux, which is free.");
            jp1.add(label1);
        }
    }
}



It's still telling me Cannot Find Symbol when I try to add the labels in the 2nd class.
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1407
  • View blog
  • Posts: 6,022
  • Joined: 20-September 08

Re: Am I Calling My Class Correctly?

Posted 02 May 2011 - 09:58 AM

Quote

new GWAToolbox();


alone will do no good at all - there's no usable reference to the window. What exactly are you attempting to do?


Quote

// Gather GUI information from GWAToolbox.java


'Gather' from where? I don't see any input fields...
Was This Post Helpful? 0
  • +
  • -

#6 v0rtex  Icon User is offline

  • I Will Try Give You (0x3A28213A)ers
  • member icon

Reputation: 179
  • View blog
  • Posts: 664
  • Joined: 02-June 10

Re: Am I Calling My Class Correctly?

Posted 02 May 2011 - 10:01 AM

remove the void when over-riding the GWAToolbox constructor:
public GWAToolbox()


Then you can declare your Jpanels global and instantiate a object in your checkOS class and then call the panels from there, I made some changes to your code, you still need to call checkOS.

GWAToolbox.java



package gwatoolbox;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout.*;
import javax.swing.JButton.*;
import javax.swing.JCheckBox.*;
import javax.swing.JFrame.*;
import javax.swing.JPanel.*;
import javax.swing.JTabbedPane.*;
import javax.swing.*;

/*
 *  @author Tyler
 */
public class GWAToolbox extends JFrame
{
    public JPanel jp1, jp2, jp3;
    public  GWAToolbox()
    {
        // Puts the application name at the top of the window.
        setTitle("GWA Toolbox");
        setSize(800,400);

        // Creates a tabbed window.
        JTabbedPane jtp = new JTabbedPane();
        getContentPane().add(jtp);

        jp1 = new JPanel();
         jp2 = new JPanel();
        jp3 = new JPanel();

        JCheckBox checkbox1 = new JCheckBox();
        checkbox1.setText("Clear Temporary Internet Files");
        jp2.add(checkbox1);

        JCheckBox checkbox2 = new JCheckBox();
        checkbox2.setText("Clear Browser History");
        jp2.add(checkbox2);

        JCheckBox checkbox3 = new JCheckBox();
        checkbox3.setText("Clear Browser Cookies");
        jp2.add(checkbox3);

        JButton button1 = new JButton();
        button1.setText("Shred!");
        jp2.add(button1);

        jtp.addTab("Home", jp1);
        jtp.addTab("Privacy Tune Up", jp2);
        jtp.addTab("Utility Tests", jp3);
        setVisible(true);

    }

    public static void main(String[] args)
    {
        new GWAToolbox();
    }
}



CheckOS.java

import javax.swing.JCheckBox.*;
import javax.swing.JFrame.*;
import javax.swing.JPanel.*;
import javax.swing.JTabbedPane.*;
import javax.swing.*;

/*
 *  @author Tyler
 */
public class checkOS
{
    GWAToolbox gw;
    public void CheckOS()
    {
        // Gather GUI information from GWAToolbox.java


        // Determine which operating system the user is running.
        JLabel label1 = new JLabel();
        String OSname = System.getProperty("os.name");

        if (OSname.startsWith("Windows"))
        {
            label1.setText("You are a commoner, because you are running Windows.");
            gw.jp1.add(label1);
        }
        else if (OSname.equals("Mac OS X"))
        {
            label1.setText("You must have a lot of money, because you have Mac.");
            gw.jp1.add(label1);
        }
        else if (OSname.equals("Linux"))
        {
            label1.setText("You must be thrifty, because you are running Linux, which is free.");
            gw.jp1.add(label1);
        }
    }
}



Was This Post Helpful? 1
  • +
  • -

#7 RandomlyKnighted  Icon User is offline

  • Microsoft Student Partner
  • member icon

Reputation: 103
  • View blog
  • Posts: 1,215
  • Joined: 14-January 10

Re: Am I Calling My Class Correctly?

Posted 02 May 2011 - 02:47 PM

Here is an update of what I have as of right now:

package gwatoolbox;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout.*;
import javax.swing.JButton.*;
import javax.swing.JCheckBox.*;
import javax.swing.JFrame.*;
import javax.swing.JPanel.*;
import javax.swing.JTabbedPane.*;
import javax.swing.*;

/*
 *  @author Tyler
 */
public class GWAToolbox extends JFrame
{   
    public JPanel jp1, jp2, jp3;
    GWAToolbox CheckPlatform;
    
    public GWAToolbox()
    {  
        // Puts the application name at the top of the window.
        setTitle("GWA Toolbox");  
        setSize(800,400);

        // Creates a tabbed window.
        JTabbedPane jtp = new JTabbedPane();
        getContentPane().add(jtp);

        JPanel jp1 = new JPanel();
        JPanel jp2 = new JPanel();
        JPanel jp3 = new JPanel();
                
        JCheckBox checkbox1 = new JCheckBox();
        checkbox1.setText("Clear Temporary Internet Files");
        jp2.add(checkbox1);
        
        JCheckBox checkbox2 = new JCheckBox();
        checkbox2.setText("Clear Browser History");
        jp2.add(checkbox2);
        
        JCheckBox checkbox3 = new JCheckBox();
        checkbox3.setText("Clear Browser Cookies");
        jp2.add(checkbox3);
        
        JButton button1 = new JButton();
        button1.setText("Shred!");
        jp2.add(button1);
        
        jtp.addTab("Home", jp1);
        jtp.addTab("Privacy Tune Up", jp2);
        jtp.addTab("Utility Tests", jp3);
        setVisible(true);
       
    }

    public static void main(String[] args)
    {
        new GWAToolbox();
    }
}



package gwatoolbox;
import javax.swing.JCheckBox.*;
import javax.swing.JFrame.*;
import javax.swing.JPanel.*;
import javax.swing.JTabbedPane.*;
import javax.swing.*;

/*
 *  @author Tyler
 */
public class CheckOS
{    
    GWAToolbox toolbox;
    
    public CheckOS()
    {        
        // Determine which operating system the user is running.
        JLabel label1 = new JLabel();
        String OSname = System.getProperty("os.name");
        
        if (OSname.startsWith("Windows"))
        {
            label1.setText("You are a commoner, because you are running Windows.");
            toolbox.jp1.add(label1);
        }
        else if (OSname.equals("Mac OS X"))
        {
            label1.setText("You must have a lot of money, because you have Mac.");
            toolbox.jp1.add(label1);
        }
        else if (OSname.equals("Linux"))
        {
            label1.setText("You must be thrifty, because you are running Linux, which is free.");
            toolbox.jp1.add(label1);
        }
    }
}



I do not have any errors anymore, but the labels do not show up for some reason. I don't understand it because I've added the label to my JPanel.
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1407
  • View blog
  • Posts: 6,022
  • Joined: 20-September 08

Re: Am I Calling My Class Correctly?

Posted 02 May 2011 - 03:00 PM

You shouldn't normally add components to a gui once it's been created. Much better to use an existing label and set its text. You need extra getters and setters for external access
Was This Post Helpful? 0
  • +
  • -

#9 RandomlyKnighted  Icon User is offline

  • Microsoft Student Partner
  • member icon

Reputation: 103
  • View blog
  • Posts: 1,215
  • Joined: 14-January 10

Re: Am I Calling My Class Correctly?

Posted 02 May 2011 - 04:06 PM

View Postg00se, on 02 May 2011 - 05:00 PM, said:

You shouldn't normally add components to a gui once it's been created. Much better to use an existing label and set its text. You need extra getters and setters for external access

I'm guessing that it is good programming practice to do this?
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7497
  • View blog
  • Posts: 28,838
  • Joined: 27-December 08

Re: Am I Calling My Class Correctly?

Posted 02 May 2011 - 06:18 PM

Yes. Dynamically adding JComponents to a GUI once it is visible can get messy, and you have to deal with revalidation of the UI. Much better to manipulate the content being displayed than the display. :)
Was This Post Helpful? 1
  • +
  • -

#11 RandomlyKnighted  Icon User is offline

  • Microsoft Student Partner
  • member icon

Reputation: 103
  • View blog
  • Posts: 1,215
  • Joined: 14-January 10

Re: Am I Calling My Class Correctly?

Posted 02 May 2011 - 06:47 PM

Already fixed. My JLabel is still not appearing. Any ideas why?
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7497
  • View blog
  • Posts: 28,838
  • Joined: 27-December 08

Re: Am I Calling My Class Correctly?

Posted 02 May 2011 - 06:52 PM

Post your revised code.
Was This Post Helpful? 0
  • +
  • -

#13 RandomlyKnighted  Icon User is offline

  • Microsoft Student Partner
  • member icon

Reputation: 103
  • View blog
  • Posts: 1,215
  • Joined: 14-January 10

Re: Am I Calling My Class Correctly?

Posted 02 May 2011 - 06:53 PM

package gwatoolbox;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout.*;
import javax.swing.JButton.*;
import javax.swing.JCheckBox.*;
import javax.swing.JFrame.*;
import javax.swing.JPanel.*;
import javax.swing.JTabbedPane.*;
import javax.swing.*;

/*
 *  @author Tyler
 */
public class GWAToolbox extends JFrame
{   
    public JPanel jp1, jp2, jp3;
    public JLabel label1;
    GWAToolbox CheckPlatform;
    
    public GWAToolbox()
    {  
        // Puts the application name at the top of the window.
        setTitle("GWA Toolbox");  
        setSize(800,400);

        // Creates a tabbed window.
        JTabbedPane jtp = new JTabbedPane();
        getContentPane().add(jtp);

        JPanel jp1 = new JPanel();
        JPanel jp2 = new JPanel();
        JPanel jp3 = new JPanel();
        JLabel label1 = new JLabel();
                
        JCheckBox checkbox1 = new JCheckBox();
        checkbox1.setText("Clear Temporary Internet Files");
        jp2.add(checkbox1);
        
        JCheckBox checkbox2 = new JCheckBox();
        checkbox2.setText("Clear Browser History");
        jp2.add(checkbox2);
        
        JCheckBox checkbox3 = new JCheckBox();
        checkbox3.setText("Clear Browser Cookies");
        jp2.add(checkbox3);
        
        JButton button1 = new JButton();
        button1.setText("Shred!");
        jp2.add(button1);
        
        jtp.addTab("Home", jp1);
        jtp.addTab("Privacy Tune Up", jp2);
        jtp.addTab("Utility Tests", jp3);
        setVisible(true);
       
    }

    public static void main(String[] args)
    {
        new GWAToolbox();
    }
}



package gwatoolbox;
import javax.swing.JCheckBox.*;
import javax.swing.JFrame.*;
import javax.swing.JPanel.*;
import javax.swing.JTabbedPane.*;
import javax.swing.*;

/*
 *  @author Tyler
 */
public class CheckOS
{    
    GWAToolbox toolbox;
    
    public CheckOS()
    {        
        // Determine which operating system the user is running.
        String OSname = System.getProperty("os.name");
        
        if (OSname.startsWith("Windows"))
        {
            toolbox.label1.setText("You are a commoner, because you are running Windows.");
            toolbox.jp1.add(toolbox.label1);
        }
        else if (OSname.equals("Mac OS X"))
        {
            toolbox.label1.setText("You must have a lot of money, because you have Mac.");
            toolbox.jp1.add(toolbox.label1);
        }
        else if (OSname.equals("Linux"))
        {
            toolbox.label1.setText("You must be thrifty, because you are running Linux, which is free.");
            toolbox.jp1.add(toolbox.label1);
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon


Reputation: 7497
  • View blog
  • Posts: 28,838
  • Joined: 27-December 08

Re: Am I Calling My Class Correctly?

Posted 02 May 2011 - 08:59 PM

You don't add the JLabel to any of the JPanels. That's why it isn't being displayed.
Was This Post Helpful? 0
  • +
  • -

#15 RandomlyKnighted  Icon User is offline

  • Microsoft Student Partner
  • member icon

Reputation: 103
  • View blog
  • Posts: 1,215
  • Joined: 14-January 10

Re: Am I Calling My Class Correctly?

Posted 02 May 2011 - 09:03 PM

In my CheckOS class I add it to the Panel 1 during each If Statement.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3