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).
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?
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?
Code java:
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.
Re: How to make a class be static if it's the main class.
Quote:
I was aiming to make sure that only one instance could be shown at a time.
Look at the Singleton Pattern
Re: How to make a class be static if it's the main class.
Quote:
Originally Posted by
Norm
Look at the Singleton Pattern
What's a Singleton Pattern? I've heard that phrase before.
Re: How to make a class be static if it's the main class.
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?
Re: How to make a class be static if it's the main class.
Try it and see what happens.
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.
Code java:
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.
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.
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.
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.
Re: How to make a class be static if it's the main class.
Quote:
Originally Posted by
javapenguin
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).
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.
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.
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.
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.
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
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'.