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("Exít");
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.