Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: How to make a class be static if it's the main class.

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default How to make a class be static if it's the main class.

    I've found that it's okay to have an inner class be static but it throws a fit when I try to make the main class static.

    I was aiming to make sure that only one instance could be shown at a time.

    If I make all the methods static, will that do it? I don't think making it final will do much more than make it sterile( unable to have children).


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: How to make a class be static if it's the main class.

    Even if you could, which you can't; I really don't get what you would need it for.
    What's wrong with just using a class with static methods? or why can't you just use a normal singleton class?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to make a class be static if it's the main class.

    I'm trying to make a FontChooser.

    Also, my ExamplePanel isn't being added right now. Is it because all of my stuff is static?

       import java.awt.event.*;
       import java.awt.*;
       import java.util.*;
       import javax.swing.event.*;
       import javax.swing.*;
       import java.io.*;
     
       public class JFontChooser extends JFrame
       {
     
     
          private Vector<Font> fonts;
          private Vector<String> fontFamilies;
          private Vector<String> fontNames;
          private JList fontList;
          private JScrollPane fontListScroll;
          private JPanel contentPane;
          private JComboBox sizes;
          private Vector<Integer> sizes2;
          private ExamplePanel example;
     
          private class ExamplePanel extends JPanel
          {
     
             private Font f;
             public ExamplePanel(Font f)
             {
                setVisible(true);
                setFont2(f);
                setBackground(Color.WHITE);
             }
     
             public void setFont2(Font f)
             {
                this.f = f;
             }
     
             public Font getFont2()
             {
                return f;
             }
     
     
     
     
             protected void paintComponent(Graphics g)
             {
                String exampleText = "Sample text.";
                g.setFont(getFont2());
                g.drawString(exampleText, 50, 50);
     
     
             }
     
          }
     
          public static Font[] getAllFonts()
          {
             GraphicsEnvironment local = GraphicsEnvironment.getLocalGraphicsEnvironment();
          // get all fonts
             Font[] fonts = local.getAllFonts();
             return fonts;
          }
     
          public static String[] getFontFamilies()
          {
             GraphicsEnvironment local = GraphicsEnvironment.getLocalGraphicsEnvironment();
     
          // get all available font families
             String[] fontFamilies = local.getAvailableFontFamilyNames();
             return fontFamilies;
          }
     
          public  JFontChooser()
          {
             fonts = new Vector<Font>();
             sizes2 = new Vector<Integer>();
             sizes2.add(6);
             sizes2.add(7);
             sizes2.add(8);
             sizes2.add(10);
             sizes2.add(12);
             sizes2.add(14);
             sizes2.add(16);
             sizes2.add(18);
             sizes2.add(20);
             sizes2.add(22);
             sizes2.add(24);
             sizes2.add(36);
             sizes2.add(48);
             sizes2.add(60);
             sizes2.add(72);
     
             sizes = new JComboBox(sizes2);
     
     
             for (int i =0; i < getAllFonts().length; i++)
             {
                fonts.add(getAllFonts()[i]);
     
             }
     
             fontNames = new Vector<String>();
             for (int i = 0; i < fonts.size(); i++)
             {
                fontNames.add(fonts.get(i).getName());
             }
     
             fontList = new JList(fontNames);
             fontListScroll = new JScrollPane(fontList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
             contentPane = new JPanel();
             contentPane.add(fontListScroll);
             contentPane.add(sizes);
     
             setVisible(true);
             example = new ExamplePanel(new Font("Times New Roman", Font.BOLD, 12));
     
             contentPane.add(example);
     
             setContentPane(contentPane);
     
          }
     
          public static void main(String[] args)
          {
             new JFontChooser();
          }
     
     
     
     
       }

    I can get it to come up by setting the example to be the contentPane instead but it won't show the first part and it won't set the background to white like I told it to.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to make a class be static if it's the main class.

    I was aiming to make sure that only one instance could be shown at a time.
    Look at the Singleton Pattern

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to make a class be static if it's the main class.

    Quote Originally Posted by Norm View Post
    Look at the Singleton Pattern
    What's a Singleton Pattern? I've heard that phrase before.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to make a class be static if it's the main class.

    try google

  7. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to make a class be static if it's the main class.

    How would I ensure that the getInstance() can recognize if another instance is told to be made by some outside application?

    Would I always have to check for it in the code of that other application or is there so way you can do it inside the called application itself?

    Does the body of the private constructor have to be blank for it to work?

    I know volatile means temporary, or it means permanent. I've heard of volatile memory before.

    But what do the keywords volatile and synchronized mean?

    If I make it a Singleton, how do I access some of the class methods from another class?

    Is the Calendar class a Singleton by any chance?
    Last edited by javapenguin; January 6th, 2012 at 04:47 PM.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How to make a class be static if it's the main class.

    Try it and see what happens.

  9. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to make a class be static if it's the main class.

    Haven't tried calling it in another class but it's compiling.

       import java.awt.event.*;
       import java.awt.*;
       import java.util.*;
       import javax.swing.event.*;
       import javax.swing.*;
       import java.io.*;
     
       public class JFontChooser extends JFrame
       {
     
     
          private Vector<Font> fonts;
          private Vector<String> fontFamilies;
          private Vector<String> fontNames;
          private JList fontList;
          private JScrollPane fontListScroll;
          private JPanel contentPane;
          private JComboBox sizes;
          private Vector<Integer> sizes2;
          private ExamplePanel example;
     
          private class ExamplePanel extends JPanel
          {
     
             private Font f;
             public ExamplePanel(Font f)
             {
                setVisible(true);
                setFont2(f);
                setBackground(Color.WHITE);
             }
     
             public void setFont2(Font f)
             {
                this.f = f;
             }
     
             public Font getFont2()
             {
                return f;
             }
     
     
     
     
             protected void paintComponent(Graphics g)
             {
                super.paintComponent(g);
                String exampleText = "Sample text.";
                g.setFont(getFont2());
                g.drawString(exampleText, 50, 50);
     
     
             }
     
          }
     
          public static Font[] getAllFonts()
          {
             GraphicsEnvironment local = GraphicsEnvironment.getLocalGraphicsEnvironment();
          // get all fonts
             Font[] fonts = local.getAllFonts();
             return fonts;
          }
     
          public static String[] getFontFamilies()
          {
             GraphicsEnvironment local = GraphicsEnvironment.getLocalGraphicsEnvironment();
     
          // get all available font families
             String[] fontFamilies = local.getAvailableFontFamilyNames();
             return fontFamilies;
          }
     
          private  JFontChooser()
          {
             fonts = new Vector<Font>();
             sizes2 = new Vector<Integer>();
             sizes2.add(6);
             sizes2.add(7);
             sizes2.add(8);
             sizes2.add(10);
             sizes2.add(12);
             sizes2.add(14);
             sizes2.add(16);
             sizes2.add(18);
             sizes2.add(20);
             sizes2.add(22);
             sizes2.add(24);
             sizes2.add(36);
             sizes2.add(48);
             sizes2.add(60);
             sizes2.add(72);
     
             sizes = new JComboBox(sizes2);
     
     
             for (int i =0; i < getAllFonts().length; i++)
             {
                fonts.add(getAllFonts()[i]);
     
             }
     
             fontNames = new Vector<String>();
             for (int i = 0; i < fonts.size(); i++)
             {
                fontNames.add(fonts.get(i).getName());
             }
     
             fontList = new JList(fontNames);
             fontListScroll = new JScrollPane(fontList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
             contentPane = new JPanel();
             contentPane.add(fontListScroll);
             contentPane.add(sizes);
     
             setVisible(true);
             example = new ExamplePanel(new Font("Times New Roman", Font.BOLD, 12));
     
             //add(contentPane, BorderLayout.NORTH);
             add(example, BorderLayout.SOUTH);
     
             setContentPane(contentPane);
     
          }
     
          public static void main(String[] args)
          {
             new JFontChooser();
          }
     
       	/**
            * SingletonHolder is loaded on the first execution of Singleton.getInstance() 
            * or the first access to SingletonHolder.INSTANCE, not before.
            */
          private static class SingletonHolder { 
             public static final JFontChooser instance = new JFontChooser();
          }
     
          public static JFontChooser getInstance() {
             return SingletonHolder.instance;
          }
       }

    Where is my JPanel subclass ExamplePanel going? I told it to add it via BorderLayout.SOUTH but it won't show up. How do you add a JPanel to another JPanel?

    -----Edit------
    Perhaps things might go better if I actually called the super inside that paintComponent.......

    Did that and now only a small white rectangle is there.

    Ok, I tried replacing the paint thing with a JLabel instead and it worked. However, why wouldn't it work with the paintComponent() and the g.drawString()?

    Ok, it appears I've been drawing it out of bounds. Ok, so figured that out.

    Now I need help with layouts. My BorderLayout stuff isn't working the way I'd hoped.
    Last edited by javapenguin; January 6th, 2012 at 05:50 PM.

  10. #10
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to make a class be static if it's the main class.

    Does anybody have any idea how to access the individual boxes in a JList, a Choice, or a JComboBox? I'm trying to make the font chooser and I was trying to have the first one with an entry of Regular, and one for Bold that would have a bold font and one for italic that would have an Italic font and one for Bold-Italic that would have Font.BOLD + Font.ITALIC, but it's not working. I tried it with JLabels but it only shows the toString() and won't even do the fonts correctly then it seems.

    Also, more related to my original topic, I'm trying to figure out how to make the class itself sort of be static and return a Font.

    Like how JOptionPane.showInputDialog(bla bla bla) returns a String and how JColorChooser returns a Color. I want the class itself to ultimately either return nothing if the user hits cancel (and do nothing, not return null and screw up the calling applications) or to return the selected Font from the JList with the selected size from the JComboBox with the selected style from the Choice. But how do you make a class static so it can return it that way?

    The class should already be a Singleton but will that be enough? A constructor, especially a private one!, cannot return anything other than an object of the class type.
    Last edited by javapenguin; January 6th, 2012 at 10:06 PM.

  11. #11
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: How to make a class be static if it's the main class.

    It's unfortunate that swing does not have a built in Font chooser dialog. This class is easy to use should do everything you need.

  12. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    javapenguin (January 6th, 2012)

  13. #12
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to make a class be static if it's the main class.

    Is there any way to get Java to accept Windings 1, 2, and 3, Webdings, Symbol, and fonts like that? Right now they're registering as little rectangles for every character when I try those.

  14. #13
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: How to make a class be static if it's the main class.

    Quote Originally Posted by javapenguin View Post
    Is there any way to get Java to accept Windings 1, 2, and 3, Webdings, Symbol, and fonts like that? Right now they're registering as little rectangles for every character when I try those.
    Sorry I can't help you with that one. Out of interest - why? The only use I have ever found for those symbolic fonts is practical jokes (swapping windings with arial on my colleagues machine and watching the chaos unfold).

  15. #14
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to make a class be static if it's the main class.

    Mainly just because if anybody wanted to make a Notepad program that included those, like the Windows Notepad does, and they used Java, they'd have a slight problem it seems.

  16. #15
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: How to make a class be static if it's the main class.

    There is an old bug report - wrong glyphs in symbol fonts (e.g., Wingding) which explains what is happening. Wingdings is not unicode, you can still access the symbols via their Dingbats ASCII character code but not as a font. This is not a bug, it's a side effect of internationalization.
    Last edited by ChristopherLowe; January 8th, 2012 at 02:06 AM.

  17. #16
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to make a class be static if it's the main class.

    I have a question, when you make something a Singleton, how would you make sure that that particular calling application can only have one instance yet also ensure that more than one instance for ALL applications is possible? (i.e. Not allow a notepad program to call more than one font selector but allow two separate notepad instances to each have a single font selector running at the same time.

  18. #17
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: How to make a class be static if it's the main class.

    If the notepad instances are in separate JVMs there isn't a problem: they would each use the singleton instance of their own JVM.

    On the other hand the notepad instances might all be running in the same JVM. In that case if the font selector is able to support multiple instances why should it care how many instances of it are created and by whom? I guess the font selector could expose a factory method that keeps track of the notepad instances that call it, but why bother? (and the notepad instances could be sneaky and share one another's font selector anyway.) More straight forward, if the notepad instances are concerned that they use a single font selector for all of their windows then they should have a single font selector instance variable and just use it whenever it is needed.

  19. #18
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to make a class be static if it's the main class.

    I meant that if one Notepad is running a font selector and I have designed the font selector to be a singleton, what happens if I open another notepad and that notepad tries to create a separate font selector instance?

    A.) It won't let it since there is already one being used by the first notepad and only 1 is allowed.
    B.) It will allow it provided it doesn't try to open another font selector with that same notepad application
    C.) It will take over the font selector from the previous notepad that called it.
    D.) None of the above

  20. #19
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: How to make a class be static if it's the main class.

    Try it and find out. I suspect 'D' - both will have their own singleton font selector since they are separate processes.

    Speaking of separate processes - this is the third question you have asked on this thread. Why don't you mark it as 'solved' and start a new thread for new questions. It's unlikely you will get answers on singleton design patterns and font selection on a thread titled 'How to make a class static'.

Similar Threads

  1. How do I call a class in a main class
    By Leprechaun_hunter in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2011, 03:11 AM
  2. Static class that reads from file -> performance issues?
    By rbk in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: April 18th, 2011, 09:59 AM
  3. Help setting a private static class variable
    By kyuss in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 26th, 2010, 08:09 AM
  4. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM
  5. could not find the main class
    By Tisofa in forum Object Oriented Programming
    Replies: 1
    Last Post: September 27th, 2009, 02:58 AM