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 2 of 2

Thread: How to make a FontChooser return a font in a Notepad.

  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 FontChooser return a font in a Notepad.

    I'm making a Notepad. I'm making the components to it one at a time.

    Though not 100% functional yet, I have an idea of what it'll do. However, I can't get it to return a Font directly, or, more accurately, to know when the OK button in the font chooser has been hit in the JFontChooser so it'll return a Font instead of null, which it'll return if I tell it to get the selectedFont right away, or it'll always return the default that the font chooser originally starts with.

       import java.awt.event.*;
       import java.awt.*;
       import java.util.*;
       import javax.swing.event.*;
       import javax.swing.*;
       import java.io.*;
       import java.text.*;
       import javax.swing.JList;
     
       public class JFontChooser extends JFrame implements ActionListener
       {
     
          private JScrollPane contentScroll;
          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 SamplePanel example;
          private JButton ok;
          private JButton cancel;
          private Choice styles;
          private JTextField fontSearcher;
          private Vector<JLabel> labelVector;
          private JList labelList;
          private JPanel sizePanel;
          private JLabel sizeLabel;
          private FontPanel fontPanel;
          private JLabel fontLabel;
          private JTextField sizeField;
          private String fontName;
          private Integer fontSize;
          private int fontStyle;
          private JTextField styleField;
          private JLabel styleLabel;
          private JPanel stylePanel;
          private JPanel buttonPanel;
          private Font selectedFont;
          private JTextField sampleField;
          private JLabel sampleLabel;
          private JLabel sample;
          private JList ObjectList;
          private Vector<Object> objects;
     
     
          private class SamplePanel extends JPanel implements ActionListener
          {
     
             private Font f;
     
             private JPanel topPanel;
             private JPanel bottomPanel;
     
             public SamplePanel()
             {
                setLayout(new GridLayout(1,3));               
     
                add(sampleLabel);
                add(sampleField);
                sampleField.addActionListener(this);
                setVisible(true);
     
                // setBackground(Color.WHITE);
                setSize(100,100);
     
                sample.setFont(f);
                add(sample);
                sample.setBackground(Color.WHITE);
     
             }
     
             public void actionPerformed(ActionEvent e)
             {
                if (e.getSource().equals(sampleField))
                {
     
     
                   sample.setText(sampleField.getText());
                   if (sample.getText().equals(""))
                      sample.setText("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz");
     
     
     
     
                }
             }
     
     
     
     
     
     
             protected void paintComponent(Graphics g)
             {
                super.paintComponent(g);
                sample.setFont(new Font(getSelectedFontName(), Font.PLAIN, 13));
     
     
             }
     
             public void paintBorder(Graphics g)
             {
                super.paintBorder(g);
                g.setColor(Color.BLACK);
                g.drawRect(0, 0, getWidth()- 1, getHeight() -1 );
     
             }
     
          }
     
          private class FontPanel extends JPanel implements ListSelectionListener
          {
     
             public FontPanel()
             {
                setLayout(new BorderLayout());
                add(fontLabel, BorderLayout.NORTH);
                add(fontSearcher, BorderLayout.CENTER);
                add(fontListScroll, BorderLayout.SOUTH);
     
                fontList.addListSelectionListener(this);
             }
     
             public void valueChanged(ListSelectionEvent e)
             {
                setSelectedFontName(fontList.getSelectedValue().toString());
                fontSearcher.setText(fontList.getSelectedValue().toString());
     
             }
     
          }
     
          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()
          {
             super("Choose a font");
             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(26);
             sizes2.add(28);
             sizes2.add(30);
             sizes2.add(36);
             sizes2.add(48);
             sizes2.add(60);
             sizes2.add(72);
     
             sizes = new JComboBox(sizes2);
             sizes.setBackground(Color.WHITE);
             sizeLabel = new JLabel("Font sizes: ");
             sizePanel = new JPanel();
             sizePanel.setLayout(new BorderLayout());
             sizePanel.add(sizeLabel, BorderLayout.NORTH);
             sizeField = new JTextField(2);
             sizePanel.add(sizeField, BorderLayout.CENTER);
             sizePanel.add(sizes, BorderLayout.SOUTH);
             sizePanel.setBackground(new Color(20, 160, 240));
     
             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);
             fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
             fontLabel = new JLabel("Font: ");
             fontSearcher = new JTextField(55);
             fontListScroll = new JScrollPane(fontList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
             fontPanel = new FontPanel();
             fontPanel.setBackground(new Color(50,150,250));
     
     
     
     
     
             contentPane = new JPanel();
             contentPane.setBackground(new Color(185,185,185));
             contentPane.add(fontPanel);
             contentPane.add(sizePanel);
     
             setVisible(true);
             sampleLabel = new JLabel("Enter text");
             sampleLabel.setBackground(Color.CYAN);
     
             sampleField = new JTextField(100);
             sample = new JLabel("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz");
             example = new SamplePanel();
     
             //add(contentPane, BorderLayout.NORTH);
     
     
             contentPane.add(example);
             //contentPane.setLayout(new FlowLayout());
             buttonPanel = new JPanel();
     
             ok = new JButton("Ok");
             cancel = new JButton("Cancel");
             buttonPanel.setLayout(new GridLayout(1,2));
             buttonPanel.add(ok);
             buttonPanel.add(cancel);
     
             styles = new Choice();
             //styles.setBackground(Color.WHITE);
             styles.add("Regular");
             styles.add("Bold");
             styles.add("Italic");
             styles.add("Bold Italic");
     
             // styles.setFont(new Font("Times New Roman", Font.BOLD, 12));
             styles.select("Bold");
             styles.setLocation(900,900);
             System.out.println(styles.getSelectedObjects()[0].getClass());
             stylePanel = new JPanel();
             stylePanel.setLayout(new BorderLayout());
             styleLabel = new JLabel("Font styles: ");
             styleField = new JTextField(15);
             stylePanel.add(styleLabel, BorderLayout.NORTH);
             stylePanel.add(styleField, BorderLayout.CENTER);
             stylePanel.add(styles, BorderLayout.SOUTH);
             contentPane.add(stylePanel);
             contentPane.add(buttonPanel);
     
             System.out.println(Arrays.toString(styles.getSelectedObjects()));
     
             //contentPane.setLayout(new GridBagLayout());
             //GridBagLayout gbl = new GridBagLayout();
             contentPane.setLayout(new GridLayout(4,5));
             // contentScroll = new JScrollPane(contentPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
     
     
     
             setContentPane(contentPane);
             //gbl.layoutContainer(contentScroll);
          }
     
          public static void main(String[] args)
          {
             new JFontChooser();
          }
     
          public void setSelectedFont(Font selectedFont)
          {
     
             this.selectedFont = selectedFont;
          }
     
          public Font getSelectedFont()
          {
             return selectedFont;
          }
     
          public String getSelectedFontName()
          {
             return fontName;
     
          }
     
          public Integer getSelectedFontSize()
          {
             return fontSize;
          }
     
          public int getSelectedFontStyle()
          {
             return fontStyle;
          }
     
          public void setSelectedFontName(String fontName)
          {
             this.fontName = fontName;
          }
     
          public void setSelectedFontStyle(int fontStyle)
          {
             this.fontStyle = fontStyle;
          }
     
          public void setSelectedFontSize(Integer fontSize)
          {
             this.fontSize = fontSize;
          }
     
     
          public void actionPerformed(ActionEvent e)
          {
             if (e.getSource().equals(ok))
             {
     
             }
     
     
          }
     
     
       	/**
            * 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;
          }
       }

     
       import java.awt.event.ActionListener;
       import javax.swing.JMenuBar;
       import javax.swing.JMenu;
       import javax.swing.JMenuItem;
       import javax.swing.JPanel;
       import javax.swing.JCheckBoxMenuItem;
       import javax.swing.JTextArea;
       import javax.swing.JScrollPane;
       import java.awt.BorderLayout;
       import java.awt.GridLayout;
       import javax.swing.JFrame;
       import java.awt.event.ActionEvent;
       import javax.swing.JOptionPane;
       import java.awt.event.ItemListener;
       import java.awt.event.ItemEvent;
       import java.util.Date;
       import java.util.Calendar;
       import java.sql.Timestamp;
       import java.awt.Color;
       import java.awt.Component;
       import javax.swing.undo.UndoManager;
       import javax.swing.event.UndoableEditListener;
       import javax.swing.event.UndoableEditEvent;
       import javax.swing.plaf.basic.BasicMenuBarUI;
       import javax.swing.plaf.metal.MetalMenuBarUI;
       import javax.swing.plaf.multi.MultiMenuBarUI;
       import java.awt.Insets;
       import java.awt.Graphics;
       import javax.swing.JToolBar;
       import javax.swing.JTextField;
       import javax.swing.JLabel;
     
     
     
     
       public class TextBasedDocumentMenu extends JPanel
       {
     
          private JMenuBar menuBar;
          private JMenu file, edit, help, save, find;
          private JMenuItem newDocument, load, save2, saveAs, print, exit;
          private JMenuItem selectAll, copy, paste, cut, find2, findReplace, setTabs, setMargins;
          private JMenuItem help2;
          private JMenu format;
          private JCheckBoxMenuItem wordWrap, lineWrap;
          private JTextArea textArea;
          private JScrollPane scrollPane;
          private JMenuItem timeStamp;
          private UndoManager undoManager;
          private JMenuItem undo, redo;
          private UndoRedoListener udl;
          private JMenuItem font;
          private JFontChooser jfc;
          private TimeStampListener tsl;
     
          public TextBasedDocumentMenu()
          {
             super(new BorderLayout());
             menuBar = new JMenuBar();
             add(menuBar, BorderLayout.PAGE_START);
             file = new JMenu("File");
             menuBar.add(file);
             newDocument = new JMenuItem("New");
             file.add(newDocument);
             file.addSeparator();
             load = new JMenuItem("Open");
             file.add(load);
             file.addSeparator();
             save = new JMenu("Save");
             file.add(save);
             file.addSeparator();
             save2 = new JMenuItem("Save");
             save.add(save2);
             save.addSeparator();
             saveAs = new JMenuItem("Save As");
             save.add(saveAs);
             print = new JMenuItem("Print");
             file.add(print);
             file.addSeparator();
             exit = new JMenuItem("Ext");
             file.add(exit);
             edit = new JMenu("Edit");
             menuBar.add(edit);
             cut = new JMenuItem("Cut");
             edit.add(cut);
             edit.addSeparator();
             copy = new JMenuItem("Copy");
             edit.add(copy);
             edit.addSeparator();
             paste = new JMenuItem("Paste");
             edit.add(paste);
             edit.addSeparator();
             selectAll = new JMenuItem("Select All");
             edit.add(selectAll);
             edit.addSeparator();
             find = new JMenu("Find");
             edit.add(find);
             edit.addSeparator();
             find2 = new JMenuItem("Find");
             find.add(find2);
             find.addSeparator();
             findReplace = new JMenuItem("Find/Replace");
             find.add(findReplace);
             setTabs = new JMenuItem("Set Tabs");
     
             setMargins = new JMenuItem("Set Margins");
     
             format = new JMenu("Format");
             format.add(setTabs);
             format.addSeparator();
             format.add(setMargins);
             format.addSeparator();
             menuBar.add(format);
     
             wordWrap = new JCheckBoxMenuItem("Word Wrap");
             format.add(wordWrap);
             format.addSeparator();
             lineWrap = new JCheckBoxMenuItem("Line wrap");
             format.add(lineWrap);
             format.addSeparator();
             textArea = new JTextArea(300,300);
             scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
             add(scrollPane, BorderLayout.CENTER);
     
             file.addSeparator();
     
             timeStamp = new JMenuItem("Time Stamp");
             edit.add(timeStamp);
             edit.addSeparator();
     
     
     
             undoManager = new UndoManager();
             textArea.getDocument().addUndoableEditListener(undoManager);
             menuBar.setBorderPainted(true);
     
             undo = new JMenuItem("Undo");
             edit.add(undo);
             udl = new UndoRedoListener();
             undo.addActionListener(udl);
             edit.addSeparator();
             redo = new JMenuItem("Redo");
             edit.add(redo);
             edit.addSeparator();
             tsl = new TimeStampListener();
             timeStamp.addActionListener(tsl);
             menuBar.setBackground(new Color(menuBar.getBackground().getRed() - 10, menuBar.getBackground().getGreen() - 5, menuBar.getBackground().getBlue() - 3));
             menuBar.setMargin(new Insets(0,1,0,1));
     
             menuBar.setAlignmentX(BOTTOM_ALIGNMENT);
             font = new JMenuItem("Font");
             format.add(font);
             format.addSeparator();
     
     
     
     
     
     
          }
     
     
          private class ExitListener implements ActionListener
          {
             public void actionPerformed(ActionEvent e)
             {
     
     
     
             }
     
          }
     
          private class TimeStampListener implements ActionListener
          {
     
             public void actionPerformed(ActionEvent e)
             {
                Date d = new Date();
                textArea.append(d.toString());
     
             }
          }
     
          private class UndoRedoListener implements ActionListener
          {
     
             public void actionPerformed(ActionEvent e)
             {
                if (e.getActionCommand().equals("Undo"))
                {
                   undoManager.undo();
     
                }
     
                if (e.getActionCommand().equals("Redo"))
                {
                   undoManager.redo();
                }
     
             }
     
          }
     
          private class ItemListeners implements ItemListener
          {
     
             public void itemStateChanged(ItemEvent e)
             {
     
     
             }
     
          }
       //It was called getJMenuBar2 to deal with the chance of somebody
       // calling setJMenuBar and then possibly having getJMenuBar point to the wrong
       // value.  This returns the JMenuBar.
          public JMenuBar getJMenuBar2()
          {
             return menuBar;
          }
     
          public JMenu getFileMenu()
          {
             return file;
          }
     
          public JMenu getEditMenu()
          {
             return edit;
          }
     
          public JMenu getHelpMenu()
          {
             return help;
          }
     
          public JMenu getSaveSubMenu()
          {
             return save;
          }
     
          public JMenu getFindSubMenu()
          {
             return find;
          }
     
          public JTextArea getTextArea()
          {
             return textArea;
          }
     
          public JScrollPane getScrollPane()
          {
             return scrollPane;
          }
     
     
     
          public static void main(String[] args)
          {
             TextBasedDocumentMenu tbdm = new TextBasedDocumentMenu();
             tbdm.setVisible(true);
             JFrame jf = new JFrame();
             jf.setContentPane(tbdm);
             jf.setVisible(true);
     
          }
     
     
     
     
       }

    Any ideas how to resolve this dilemma of trying to return a Font from the font chooser and not return the default every time, or even worse, return null always.



    ----Edit-------



    Hmmmm, what if I told it to only set the Font to the getSelectedFont() after the Ok button was hit (and set up handlers if the Cancel Button or the X out button are hit instead to avoid a Null Pointer Exception. Will that work?)

    I'm going to try it. I'll be needed help later on this Notepad for sure. Especially with the Tabs when I make it a tabbed notepad later.

    However, I have to make sure that it's not out of scope when the Ok button is hit, but only merely invisible, especially as the JFontChooser is a Singleton, though it shouldn't matter whether it is or not.
    Last edited by javapenguin; January 14th, 2012 at 12:40 AM.


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

    Smile Re: How to make a FontChooser return a font in a Notepad.

    Ok, here's a problem. I have trouble figuring out GroupLayout. I was able to add two JTextFields and two JLabels but when I tried to add some JButtons and stuff, it started throwing IlegalStateExceptions. So I just merely told it to add but it still won't show up.

    Also, I added the components that I'd like to add, though I may come up with more later, but anyway, I put them into a JPanel and showed them with a JFrame. I can't even get the layout of this new JPanel to show three buttons on one level and one checkbox below it. All I get at best is a 2 by 2.

       import java.awt.event.*;
       import java.awt.*;
       import java.util.*;
       import javax.swing.event.*;
       import javax.swing.*;
       import java.io.*;
       import java.text.*;
       import javax.swing.JList;
     
       public class JFontChooser extends JFrame implements ActionListener
       {
     
          private JScrollPane contentScroll;
          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 SamplePanel example;
          private JButton ok;
          private JButton cancel;
          private Choice styles;
          private JTextField fontSearcher;
          private Vector<JLabel> labelVector;
          private JList labelList;
          private JPanel sizePanel;
          private JLabel sizeLabel;
          private FontPanel fontPanel;
          private JLabel fontLabel;
          private JTextField sizeField;
          private String fontName;
          private Integer fontSize;
          private int fontStyle;
          private JTextField styleField;
          private JLabel styleLabel;
          private JPanel stylePanel;
          private JPanel buttonPanel;
          private Font selectedFont;
          private JTextField sampleField;
          private JLabel sampleLabel;
          private JLabel sample;
          private JList ObjectList;
          private Vector<Object> objects;
     
     
          private class SamplePanel extends JPanel implements ActionListener
          {
     
             private Font f;
     
             private JPanel topPanel;
             private JPanel bottomPanel;
     
             public SamplePanel()
             {
                setLayout(new GridLayout(1,3));               
     
                add(sampleLabel);
                add(sampleField);
                sampleField.addActionListener(this);
                setVisible(true);
     
                // setBackground(Color.WHITE);
                setSize(100,100);
     
                sample.setFont(f);
                add(sample);
                sample.setBackground(Color.WHITE);
     
             }
     
             public void actionPerformed(ActionEvent e)
             {
                if (e.getSource().equals(sampleField))
                {
     
     
                   sample.setText(sampleField.getText());
                   if (sample.getText().equals(""))
                      sample.setText("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz");
     
     
     
     
                }
             }
     
     
     
     
     
     
             protected void paintComponent(Graphics g)
             {
                super.paintComponent(g);
                sample.setFont(new Font(getSelectedFontName(), Font.PLAIN, 13));
     
     
             }
     
             public void paintBorder(Graphics g)
             {
                super.paintBorder(g);
                g.setColor(Color.BLACK);
                g.drawRect(0, 0, getWidth()- 1, getHeight() -1 );
     
             }
     
          }
     
          private class FontPanel extends JPanel implements ListSelectionListener
          {
     
             public FontPanel()
             {
                setLayout(new BorderLayout());
                add(fontLabel, BorderLayout.NORTH);
                add(fontSearcher, BorderLayout.CENTER);
                add(fontListScroll, BorderLayout.SOUTH);
     
                fontList.addListSelectionListener(this);
             }
     
             public void valueChanged(ListSelectionEvent e)
             {
                setSelectedFontName(fontList.getSelectedValue().toString());
                fontSearcher.setText(fontList.getSelectedValue().toString());
     
             }
     
          }
     
          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()
          {
             super("Choose a font");
             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(26);
             sizes2.add(28);
             sizes2.add(30);
             sizes2.add(36);
             sizes2.add(48);
             sizes2.add(60);
             sizes2.add(72);
     
             sizes = new JComboBox(sizes2);
             sizes.setBackground(Color.WHITE);
             sizeLabel = new JLabel("Font sizes: ");
             sizePanel = new JPanel();
             sizePanel.setLayout(new BorderLayout());
             sizePanel.add(sizeLabel, BorderLayout.NORTH);
             sizeField = new JTextField(2);
             sizePanel.add(sizeField, BorderLayout.CENTER);
             sizePanel.add(sizes, BorderLayout.SOUTH);
             sizePanel.setBackground(new Color(20, 160, 240));
     
             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);
             fontList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
             fontLabel = new JLabel("Font: ");
             fontSearcher = new JTextField(55);
             fontListScroll = new JScrollPane(fontList, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
             fontPanel = new FontPanel();
             fontPanel.setBackground(new Color(50,150,250));
     
     
     
     
     
             contentPane = new JPanel();
             contentPane.setBackground(new Color(185,185,185));
             contentPane.add(fontPanel);
             contentPane.add(sizePanel);
     
             setVisible(true);
             sampleLabel = new JLabel("Enter text");
             sampleLabel.setBackground(Color.CYAN);
     
             sampleField = new JTextField(100);
             sample = new JLabel("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz");
             example = new SamplePanel();
     
             //add(contentPane, BorderLayout.NORTH);
     
     
             contentPane.add(example);
             //contentPane.setLayout(new FlowLayout());
             buttonPanel = new JPanel();
     
             ok = new JButton("Ok");
             cancel = new JButton("Cancel");
             buttonPanel.setLayout(new GridLayout(1,2));
             buttonPanel.add(ok);
             buttonPanel.add(cancel);
     
             styles = new Choice();
             //styles.setBackground(Color.WHITE);
             styles.add("Regular");
             styles.add("Bold");
             styles.add("Italic");
             styles.add("Bold Italic");
     
             // styles.setFont(new Font("Times New Roman", Font.BOLD, 12));
             styles.select("Bold");
             styles.setLocation(900,900);
             System.out.println(styles.getSelectedObjects()[0].getClass());
             stylePanel = new JPanel();
             stylePanel.setLayout(new BorderLayout());
             styleLabel = new JLabel("Font styles: ");
             styleField = new JTextField(15);
             stylePanel.add(styleLabel, BorderLayout.NORTH);
             stylePanel.add(styleField, BorderLayout.CENTER);
             stylePanel.add(styles, BorderLayout.SOUTH);
             contentPane.add(stylePanel);
             contentPane.add(buttonPanel);
     
             System.out.println(Arrays.toString(styles.getSelectedObjects()));
     
             //contentPane.setLayout(new GridBagLayout());
             //GridBagLayout gbl = new GridBagLayout();
             contentPane.setLayout(new GridLayout(4,5));
             // contentScroll = new JScrollPane(contentPane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
     
     
     
             setContentPane(contentPane);
             //gbl.layoutContainer(contentScroll);
          }
     
          public static void main(String[] args)
          {
             new JFontChooser();
          }
     
          public void setSelectedFont(Font selectedFont)
          {
     
             this.selectedFont = selectedFont;
          }
     
          public Font getSelectedFont()
          {
             return selectedFont;
          }
     
          public String getSelectedFontName()
          {
             return fontName;
     
          }
     
          public Integer getSelectedFontSize()
          {
             return fontSize;
          }
     
          public int getSelectedFontStyle()
          {
             return fontStyle;
          }
     
          public void setSelectedFontName(String fontName)
          {
             this.fontName = fontName;
          }
     
          public void setSelectedFontStyle(int fontStyle)
          {
             this.fontStyle = fontStyle;
          }
     
          public void setSelectedFontSize(Integer fontSize)
          {
             this.fontSize = fontSize;
          }
     
     
          public void actionPerformed(ActionEvent e)
          {
             if (e.getSource().equals(ok))
             {
     
             }
     
     
          }
     
     
       	/**
            * 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;
          }
       }

       import javax.swing.JSpinner;
       import javax.swing.JFrame;
       import javax.swing.JPanel;
       import java.awt.event.ActionListener;
       import javax.swing.event.ChangeListener;
       import javax.swing.event.ChangeEvent;
       import javax.swing.SpinnerNumberModel;
       import java.awt.BorderLayout;
       import java.awt.GridLayout;
       import javax.swing.JLabel;
       import javax.swing.JDialog;
       import javax.swing.JMenu;
       import java.awt.Dimension;
       import java.awt.event.ActionListener;
       import javax.swing.JButton;
       import java.awt.event.ActionEvent;
       import java.awt.Color;
     
     
       public class MarginAdjuster extends JFrame 
       {
     
          private JSpinner rightMargin, leftMargin, topMargin, bottomMargin;
          private MarginAdjusterPanel marginPanel;
          private JPanel contentPane;
          private SpinnerNumberModel snm;
          private JLabel leftMarginLabel, rightMarginLabel, topMarginLabel, bottomMarginLabel;
          private JButton ok, cancel;
     
     
     
          private class MarginAdjusterPanel extends JPanel implements ChangeListener, ActionListener
          {
     
     
     
             public MarginAdjusterPanel()
             {
     
     
                setLayout(new GridLayout(3, 4));
     
                add(leftMarginLabel);
                add(leftMargin);
                leftMargin.addChangeListener(this);
                add(rightMarginLabel);
                add(rightMargin);
     
     
                rightMargin.addChangeListener(this);
     
                add(topMarginLabel);
                add(topMargin);
                topMargin.addChangeListener(this);
                add(bottomMarginLabel);
                add(bottomMargin);
                bottomMargin.addChangeListener(this);
                add(ok);
                ok.addActionListener(this);
                add(cancel);
                cancel.addActionListener(this);
             }
             public void stateChanged(ChangeEvent e)
             {
     
             }
     
             public void actionPerformed(ActionEvent e)
             {
     
             }
     
          }
          public MarginAdjuster()
          {
             setVisible(true);
             snm = new SpinnerNumberModel(0.75,0.00, 8.00, 0.1);
             ok = new JButton("Ok");
             cancel = new JButton("Cancel");
             leftMarginLabel = new JLabel("Left margin: ");
             rightMarginLabel = new JLabel("Right margin: ");
             rightMargin = new JSpinner();
             rightMargin.setModel(snm);
             leftMargin = new JSpinner();
             leftMargin.setModel(snm);
             topMargin = new JSpinner();
             topMargin.setModel(snm);
             topMarginLabel = new JLabel("Top margin: ");
             bottomMargin = new JSpinner();
             bottomMargin.setModel(snm);
             bottomMarginLabel = new JLabel("Bottom margin: ");
     
             marginPanel = new MarginAdjusterPanel();
             setContentPane(marginPanel);
     
     
          }
     
     
          public static void main(String[] args)
          {
             new MarginAdjuster();
     
     
          }
     
     
          private static class SingletonHolder { 
             public static final MarginAdjuster instance = new MarginAdjuster();
          }
     
          public static MarginAdjuster getInstance() {
             return SingletonHolder.instance;
          }
       }

     
       import java.awt.event.ActionListener;
       import javax.swing.JMenuBar;
       import javax.swing.JMenu;
       import javax.swing.JMenuItem;
       import javax.swing.JPanel;
       import javax.swing.JCheckBoxMenuItem;
       import javax.swing.JTextArea;
       import javax.swing.JScrollPane;
       import java.awt.BorderLayout;
       import java.awt.GridLayout;
       import javax.swing.JFrame;
       import java.awt.event.ActionEvent;
       import javax.swing.JOptionPane;
       import java.awt.event.ItemListener;
       import java.awt.event.ItemEvent;
       import java.util.Date;
       import java.util.Calendar;
       import java.sql.Timestamp;
       import java.awt.Color;
       import java.awt.Component;
       import javax.swing.undo.UndoManager;
       import javax.swing.event.UndoableEditListener;
       import javax.swing.event.UndoableEditEvent;
       import javax.swing.plaf.basic.BasicMenuBarUI;
       import javax.swing.plaf.metal.MetalMenuBarUI;
       import javax.swing.plaf.multi.MultiMenuBarUI;
       import java.awt.Insets;
       import java.awt.Graphics;
       import javax.swing.JToolBar;
       import javax.swing.JTextField;
       import javax.swing.JLabel;
       import javax.swing.JButton;
       import javax.swing.GroupLayout;
       import javax.swing.GroupLayout.Alignment;
       import javax.swing.JCheckBox;
       import javax.swing.undo.CannotUndoException;
       import javax.swing.undo.CannotRedoException;
     
     
     
       public class TextBasedDocumentMenu extends JPanel
       {
     
          private JMenuBar menuBar;
          private JMenu file, edit, help, save, find;
          private JMenuItem newDocument, load, save2, saveAs, print, exit;
          private JMenuItem selectAll, copy, paste, cut, find2, findReplace, setTabs, setMargins;
          private JMenuItem help2;
          private JMenu format;
          private JCheckBoxMenuItem wordWrap, lineWrap;
          private JTextArea textArea;
          private JScrollPane scrollPane;
          private JMenuItem timeStamp;
          private UndoManager undoManager;
          private JMenuItem undo, redo;
          private UndoRedoListener udl;
          private JMenuItem font;
          private JFontChooser jfc;
          private TimeStampListener tsl;
          private FontListener fl;
          private MarginAdjuster ma;
          private MarginListener ml;
          private FindPanel fp;
          private JPanel findPanel;
          private JTextField findField;
          private JTextField replaceField;
          private JLabel findLabel;
          private JLabel replaceLabel;
          private JButton findButton;
          private JButton replaceButton;
          private JButton replaceAllButton;
          private JButton cancelButton;
          private GroupLayout groupLayout;
          private JFrame findWindow;
          private GroupLayout.SequentialGroup hGroup;
          private GroupLayout.SequentialGroup h2Group;
          private  GroupLayout.SequentialGroup vGroup;
          private FindListener findListener;
          private JPanel centerPanel;
          private JCheckBox matchCase;
     
          public TextBasedDocumentMenu()
          {
             super(new BorderLayout());
             menuBar = new JMenuBar();
             add(menuBar, BorderLayout.PAGE_START);
             file = new JMenu("File");
             menuBar.add(file);
             newDocument = new JMenuItem("New");
             file.add(newDocument);
             file.addSeparator();
             load = new JMenuItem("Open");
             file.add(load);
             file.addSeparator();
             save = new JMenu("Save");
             file.add(save);
             file.addSeparator();
             save2 = new JMenuItem("Save");
             save.add(save2);
             save.addSeparator();
             saveAs = new JMenuItem("Save As");
             save.add(saveAs);
             print = new JMenuItem("Print");
             file.add(print);
             file.addSeparator();
             exit = new JMenuItem("Ext");
             file.add(exit);
             edit = new JMenu("Edit");
             menuBar.add(edit);
             cut = new JMenuItem("Cut");
             edit.add(cut);
             edit.addSeparator();
             copy = new JMenuItem("Copy");
             edit.add(copy);
             edit.addSeparator();
             paste = new JMenuItem("Paste");
             edit.add(paste);
             edit.addSeparator();
             selectAll = new JMenuItem("Select All");
             edit.add(selectAll);
             edit.addSeparator();
             find = new JMenu("Find");
             edit.add(find);
             edit.addSeparator();
             find2 = new JMenuItem("Find");
             find.add(find2);
             find.addSeparator();
             findReplace = new JMenuItem("Find/Replace");
             find.add(findReplace);
             setTabs = new JMenuItem("Set Tabs");
     
             setMargins = new JMenuItem("Set Margins");
     
             format = new JMenu("Format");
             format.add(setTabs);
             format.addSeparator();
             format.add(setMargins);
             format.addSeparator();
             menuBar.add(format);
     
             wordWrap = new JCheckBoxMenuItem("Word Wrap");
             format.add(wordWrap);
             format.addSeparator();
             lineWrap = new JCheckBoxMenuItem("Line wrap");
             format.add(lineWrap);
             format.addSeparator();
             textArea = new JTextArea(300,300);
             scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
             add(scrollPane, BorderLayout.CENTER);
     
             file.addSeparator();
     
             timeStamp = new JMenuItem("Time Stamp");
             edit.add(timeStamp);
             edit.addSeparator();
     
     
     
             undoManager = new UndoManager();
             textArea.getDocument().addUndoableEditListener(undoManager);
             menuBar.setBorderPainted(true);
     
             undo = new JMenuItem("Undo");
             edit.add(undo);
             udl = new UndoRedoListener();
             undo.addActionListener(udl);
             edit.addSeparator();
             redo = new JMenuItem("Redo");
             redo.addActionListener(udl);
             edit.add(redo);
             edit.addSeparator();
             tsl = new TimeStampListener();
             timeStamp.addActionListener(tsl);
             menuBar.setBackground(new Color(menuBar.getBackground().getRed() - 10, menuBar.getBackground().getGreen() - 5, menuBar.getBackground().getBlue() - 3));
             menuBar.setMargin(new Insets(0,1,0,1));
     
             menuBar.setAlignmentX(BOTTOM_ALIGNMENT);
             font = new JMenuItem("Font");
             format.add(font);
             format.addSeparator();
     
             fl = new FontListener();
             font.addActionListener(fl);
             ml = new MarginListener();
             setMargins.addActionListener(ml);
     
             findField = new JTextField();
             fp= FindPanel.getInstance();
             groupLayout = new GroupLayout(fp);
             fp.setLayout(groupLayout);
             groupLayout.setAutoCreateGaps(true);
             groupLayout.setAutoCreateContainerGaps(true);
             findWindow = new JFrame("Find");
             findLabel = new JLabel("Find:");
             replaceLabel = new JLabel("Replace:");
             hGroup = groupLayout.createSequentialGroup();
             findButton = new JButton("Find");
             replaceButton = new JButton("Replace");
             hGroup.addGroup(groupLayout.createParallelGroup().addComponent(findLabel).addComponent(replaceLabel));
             replaceField = new JTextField();
     
             hGroup.addGroup(groupLayout.createParallelGroup().
                addComponent(findField).addComponent(replaceField));
             groupLayout.setHorizontalGroup(hGroup);
             vGroup = groupLayout.createSequentialGroup();
             vGroup.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).
                addComponent(findLabel).addComponent(findField));
             vGroup.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE).
                addComponent(replaceLabel).addComponent(replaceField));
             groupLayout.setVerticalGroup(vGroup);
             findListener = new FindListener();
             findWindow.setContentPane(fp);
             find2.addActionListener(findListener);
     
             centerPanel = new JPanel();
             cancelButton = new JButton("Cancel");
             centerPanel.setLayout(new GridLayout(3, 1));
             centerPanel.add(findButton);
             centerPanel.add(replaceButton);
             centerPanel.add(cancelButton);
     
     
             fp.add(centerPanel);  // Where's centerPanel?
             centerPanel.setVisible(true);
     
             matchCase = new JCheckBox("Match Case");
             centerPanel.add(matchCase);
     
             JFrame jf2 = new JFrame();
             jf2.setContentPane(centerPanel);
             jf2.setVisible(true);
     
     
     
     
     
     
          }
     
     
          private class ExitListener implements ActionListener
          {
             public void actionPerformed(ActionEvent e)
             {
     
     
     
             }
     
          }
     
          private class FindListener implements ActionListener
          {
             public void actionPerformed(ActionEvent e)
             {
                findWindow.setVisible(true);
     
             }
     
     
          }
     
          private class MarginListener implements ActionListener
          {
             public void actionPerformed(ActionEvent e)
             {
                ma = new MarginAdjuster();
     
     
     
             }
     
     
          }
     
          private static class FindPanel extends JPanel
          {
     
             private FindPanel()
             {}
     
             private static class SingletonHolder { 
                public static final FindPanel instance = new FindPanel();
     
     
             }
     
             public static FindPanel getInstance()
             {
                return SingletonHolder.instance;
             }
     
          }
     
     
     
          private class FontListener implements ActionListener
          {
     
             public void actionPerformed(ActionEvent e)
             {
                jfc = JFontChooser.getInstance();
     
                if (!jfc.isVisible())
                {
     
                   try
                   {
                      textArea.setFont(jfc.getSelectedFont());
                   }
     
                      catch (NullPointerException npe)
                      {
     
     
                      }
     
                }
     
     
     
     
     
             }
     
          }
     
          private class TimeStampListener implements ActionListener
          {
     
             public void actionPerformed(ActionEvent e)
             {
                Date d = new Date();
                textArea.append(d.toString());
     
             }
          }
     
          private class UndoRedoListener implements ActionListener
          {
     
             public void actionPerformed(ActionEvent e)
             {
                if (e.getActionCommand().equals("Undo"))
                {
                   try
                   {
                      undoManager.undo();
     
                   }
     
                      catch(CannotUndoException cue)
                      {
     
     
                      }
     
                }
     
                if (e.getActionCommand().equals("Redo"))
                {
                   try
                   {
                      undoManager.redo();
                      redo.setEnabled(true);
     
                   }
     
                      catch(CannotRedoException cre)
                      {
     
     
                      }
                }
     
             }
     
          }
     
          private class ItemListeners implements ItemListener
          {
     
             public void itemStateChanged(ItemEvent e)
             {
     
     
             }
     
          }
       //It was called getJMenuBar2 to deal with the chance of somebody
       // calling setJMenuBar and then possibly having getJMenuBar point to the wrong
       // value.  This returns the JMenuBar.
          public JMenuBar getJMenuBar2()
          {
             return menuBar;
          }
     
          public JMenu getFileMenu()
          {
             return file;
          }
     
          public JMenu getEditMenu()
          {
             return edit;
          }
     
          public JMenu getHelpMenu()
          {
             return help;
          }
     
          public JMenu getSaveSubMenu()
          {
             return save;
          }
     
          public JMenu getFindSubMenu()
          {
             return find;
          }
     
          public JTextArea getTextArea()
          {
             return textArea;
          }
     
          public JScrollPane getScrollPane()
          {
             return scrollPane;
          }
     
     
     
          public static void main(String[] args)
          {
             TextBasedDocumentMenu tbdm = new TextBasedDocumentMenu();
             tbdm.setVisible(true);
             JFrame jf = new JFrame();
             jf.setContentPane(tbdm);
             jf.setVisible(true);
     
          }
     
     
     
     
       }

    The problem is in the TextBasedDocumentMenu class, I only included the other two to make it compile and fully run without having to delete code.
    Last edited by javapenguin; January 14th, 2012 at 11:12 PM.

Similar Threads

  1. How to Change JTextArea font, font size and color
    By Flash in forum Java Swing Tutorials
    Replies: 7
    Last Post: January 14th, 2012, 10:47 PM
  2. Return Object does not return the expected output
    By Nour Damer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 13th, 2011, 08:24 AM
  3. Ok, Notepad class has returned!
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 1st, 2011, 10:50 PM
  4. notepad as backend database
    By chinni in forum JDBC & Databases
    Replies: 0
    Last Post: October 21st, 2009, 02:48 AM
  5. How to Change JTextArea font, font size and color
    By Flash in forum Java Code Snippets and Tutorials
    Replies: 4
    Last Post: July 8th, 2008, 02:45 PM