please can anyone help me doing this example.
Add to the text editor's GUI a menu that lists these font sizes: 12, 14, and 16. When the user selects one of the sizes from the menu, the text displayed in the text area changes to the selected size.
SNIP

Recommended Answers

All 4 Replies

Hi

You have not posted any code so this help will be very universal.
For a classic menu you need a JMenuBar that you add to your container or JFrame whit the method setJMenuBar();
To the JMenuBar you then add a JMenu and to the JMenu you add three JMenuItem that changes the font size when you chooses one of them.
To make the event happen you must add ActionListeners to the JMenuItems and build a class that catches the events like in this example:

private class MenuItemListener implements ActionListener
{
	public void actionPerformed(ActionEvent e)
	{
		String text = e.getActionCommand();

		if(text.equals("Font size 16"))
		{
			//change the font size to 16
		}

In java.awt you will find Class Font and constructors that takes name, style and size of a font, so by creating new Font(); you can decide/change the size of the font.

Hope this will give you some help in the right direction.
Good luck :)

Hi
Was looking for something else today and stumbled upon this:

public static class StyledEditorKit.FontSizeAction
extends StyledEditorKit.StyledTextAction
An action to set the font size in the associated JEditorPane. This will use the size specified as the command string on the ActionEvent if there is one, otherwise the size that was initialized with will be used.

http://java.sun.com/javase/6/docs/api/
I have not tried it but maybe this is something for you to look at.
:)

I have a lot of classes so if can help me anyone
here are my classes

view for text editor=======================================


import java.awt.*;
import javax.swing.*;
/** EditFrame displays a text editor with two menus and a text area.  */
public class EditFrame extends JFrame
{ // the EditModel, a subclass of JTextArea, is the ``model'':
private EditModel buffer = new EditModel("", 15, 50);


/** Constructor  EditFrame  builds the editor interface */
public EditFrame()
{ // Create the ReplaceFrame, which appears when the user selects ``Replace'':
ReplaceFrame second_frame = new ReplaceFrame(buffer);
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
JMenuBar mbar = new JMenuBar();
JMenu file = new JMenu("File");     // defines the  "File"  menu
file.add(new ClearMenuItem("New", buffer));
file.add(new QuitMenuItem("Exit"));
mbar.add(file);    // attach menu to menu bar
JMenu edit = new JMenu("Edit");     // defines the  "Edit"  menu
edit.add(new CutMenuItem("Cut", buffer));
edit.add(new CopyMenuItem("Copy", buffer));
edit.add(new PasteMenuItem("Paste", buffer));
edit.addSeparator();
JMenu search = new JMenu("Search");  // defines the "Search" submenu
search.add(new FindMenuItem("Find", buffer));
search.add(new ReplaceMenuItem("Replace", second_frame));
edit.add(search);
mbar.add(edit);
setJMenuBar(mbar);  // attach menu bar to frame
JScrollPane sp = new JScrollPane(buffer);  // embed into a scroll pane
cp.add(sp, BorderLayout.CENTER);
setTitle("EditFrame");
pack();
setVisible(true);
}
}


text area for editor=======================================


import java.awt.*;  import javax.swing.*;
/** EditModel models a text area  */
public class EditModel extends JTextArea
{ /** EditModel builds the text area
* @param initial_text - the starting text for the text area
* @param rows - the number of rows
* @param cols - the number of columns  */
public EditModel(String initial_text, int rows, int cols)
{ super(initial_text, rows, cols);   // create the underlying JTextArea
setLineWrap(true);
setFont(new Font("Courier", Font.PLAIN, 14));
}


/** clear resets the text area to be empty */
public void clear()
{ setText(""); }


/** find locates string  s  in the text area, starting from  position */
private int find(String s, int position)
{ String text = getText();
int index = text.indexOf(s, position);       // see Table 9, Chapter 3
if ( index != -1 )                           // did we find string  s?
{ setCaretPosition(index + s.length()); // resets the caret
moveCaretPosition(index);             // selects the string
}
return index;
}


/** findFromStart locates a string starting from the front of the text area
* @param s - the string to be found
* @return the position where s is first found; -1, if s not found  */
public int findFromStart(String s)
{ return find(s, 0); }


/** findFromCaret locates a string starting from the caret position
* @param s - the string to be found
* @return the position where s is first found; -1, if s not found  */
public int findFromCaret(String s)
{ return find(s, getCaretPosition()); }
}


menu item-controllers for text editor========================


import javax.swing.*;  import java.awt.event.*;
/** QuitMenuItem terminates the text editor.  */
public class QuitMenuItem extends JMenuItem implements ActionListener
{ public QuitMenuItem(String label)
{ super(label);
addActionListener(this);
}


public void actionPerformed(ActionEvent e)
{ System.exit(0); }
}


import javax.swing.*;  import java.awt.event.*;
/** EditorMenuItem defines a generic menu item for the text editor  */
public abstract class EditorMenuItem extends JMenuItem implements ActionListener
{ private EditModel buffer; // address of the model manipulated by the menu item


public EditorMenuItem(String label, EditModel model)
{ super(label);
buffer = model;
addActionListener(this);
}


/** myModel returns the address of the model this menu item manipulates */
public EditModel myModel()
{ return buffer; }


public abstract void actionPerformed(ActionEvent e);
}


import java.awt.event.*;
/** ClearMenuItem clears a text area */
public class ClearMenuItem extends EditorMenuItem
{ public ClearMenuItem(String label, EditModel model)
{ super(label, model); }


public void actionPerformed(ActionEvent e)
{ myModel().clear(); }
}



menu item-controllers for text editor (cont.)======


import java.awt.event.*;
/** CutMenuItem  cuts the selected text from the text area. */
public class CutMenuItem extends EditorMenuItem
{ public CutMenuItem(String label, EditModel model)
{ super(label, model); }


public void actionPerformed(ActionEvent e)
{ myModel().cut(); }
}


import java.awt.event.*;
/** CopyMenuItem  copies selected text into the clipboard */
public class CopyMenuItem extends EditorMenuItem
{ public CopyMenuItem(String label, EditModel model)
{ super(label, model); }


public void actionPerformed(ActionEvent e)
{ myModel().copy(); }
}


import java.awt.event.*;
/** PasteMenuItem moves contents of the clipboard into the text area */
public class PasteMenuItem extends EditorMenuItem
{ public PasteMenuItem(String label, EditModel model)
{ super(label, model); }


public void actionPerformed(ActionEvent e)
{ myModel().paste(); }
}



menu item-controllers for text editor (concl.)=====


import javax.swing.*;  import java.awt.event.*;
/** FindMenuItem generates a dialog to find a string in the text area */
public class FindMenuItem extends EditorMenuItem
{ public FindMenuItem(String label, EditModel model)
{ super(label, model); }


public void actionPerformed(ActionEvent e)
{ String s = JOptionPane.showInputDialog(this, "Type string to be found:");
if ( s != null )
{ int index =  myModel().findFromCaret(s);
if ( index == -1 )
{ int response = JOptionPane.showConfirmDialog(this,
"String " + s + " not found.  Restart search from beginning of buffer?");
if ( response == JOptionPane.YES_OPTION )
{ index = myModel().findFromStart(s);
if ( index == -1 )
{ JOptionPane.showMessageDialog(this,
"String " + s + " not found");
}
}
}
}
}
}


import javax.swing.*;  import java.awt.event.*;
/** ReplaceMenuItem shows the frame that helps the user replace strings */
public class ReplaceMenuItem extends JMenuItem implements ActionListener
{ private ReplaceFrame my_view;


public ReplaceMenuItem(String label, ReplaceFrame view)
{ super(label);
my_view = view;
addActionListener(this);
}


public void actionPerformed(ActionEvent e)
{ my_view.setVisible(true); }
}



frame that replaces strings==================================


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/** ReplaceFrame shows a frame that helps a user find and replace a string */
public class ReplaceFrame extends JFrame implements ActionListener
{ private EditModel model;
private JButton replace = new JButton("Replace");
private JButton clear = new JButton("Clear");
private JButton close = new JButton("Close");
private JTextField find_text = new JTextField("", 20);
private JTextField replace_text = new JTextField("", 20);


public ReplaceFrame(EditModel my_model)
{ model = my_model;
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
JPanel p1 = new JPanel(new GridLayout(2, 1));
JPanel p11 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
p11.add(new JLabel("From caret, replace "));
p11.add(find_text);
p1.add(p11);
JPanel p12 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
p12.add(new JLabel("by "));
p12.add(replace_text);
p1.add(p12);
cp.add(p1, BorderLayout.CENTER);
JPanel p2 = new JPanel(new FlowLayout());
p2.add(replace);
p2.add(clear);
p2.add(close);
cp.add(p2, BorderLayout.SOUTH);
replace.addActionListener(this);
clear.addActionListener(this);
close.addActionListener(this);
setTitle("ReplaceFrame");
pack();
setVisible(false);
}



frame that replaces strings (concl.)=====================


/** actionPerformed handles all button pushes on this frame
* @param e - contains the identity of the button that is pushed */
public void actionPerformed(ActionEvent e)
{ if ( e.getSource() == close )         // was it the Close button?
{ setVisible(false); }
else if ( e.getSource() == clear )    // the Clear button?
{ find_text.setText("");
replace_text.setText("");
}
else if ( e.getSource() == replace )  // the Replace button?
{ String find = find_text.getText();
int location = model.findFromCaret(find);
if ( location == -1 )            // string not found?
{ JOptionPane.showMessageDialog(this,
"String " + find + " not found");
}
else { model.replaceRange(replace_text.getText(),
location, location + find.length());
}
}
}
}

now I want to add this
Add to the text editor's GUI a menu that lists these font sizes: 12, 14, and 16. When the user selects one of the sizes from the menu, the text displayed in the text area changes to the selected size.
You can add buttons to a menu--try this: Modify class AbsTempFrame in Figure 28 so that it uses a menu to hold the Go, Reset, and Bye buttons.
Create an ``appointments'' GUI that displays a text area, a Save button, and a five-item menu consisting of Monday, Tuesday, Wednesday, Thursday, and Friday. When the GUI's user selects one of the menu items, the text area displays a message saved for the item. (Initially, all the messages for all items are empty.) The user can edit the message, and when she pushes Save, the message is saved with the selected item.
Create a GUI that displays two text areas. Write controllers that let a user cut, copy, and paste text from one text area to the other.

If JTextArea is not necessary as EditModel:
Change to class EditModel extends JTextPane{ Now, you can simply write

JMenu fontSize = new JMenu("fontSize");
        fontSize.add(new StyledEditorKit.FontSizeAction("16", 16));
        mbar.add(fontSize);

or define Size16MenuItem class

class Size16MenuItem extends EditorMenuItem {

    private static final Action act = new StyledEditorKit.FontSizeAction("16", 16);

    public Size16MenuItem(String label, EditModel model) {
        super(act, model);
    }

    public void actionPerformed(ActionEvent e) {
        act.actionPerformed(e);
    }
}

in this case you need second constructor in EditorMenuItem class

public EditorMenuItem(Action act, EditModel model) {
        super(act);
        buffer = model;
        addActionListener(this);
    }

---- change replace.method too

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.