Closed Thread Icon

Topic awaiting preservation: Help Setting Font Type in Java JEditorPane (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12575" title="Pages that link to Topic awaiting preservation: Help Setting Font Type in Java JEditorPane (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Help Setting Font Type in Java JEditorPane <span class="small">(Page 1 of 1)</span>\

 
WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 01-14-2003 05:10

I am attempting to write a simple little Java Program that would be able to change text in a JEditorPane. I want to be able to perform multiple HTML type functions on text that is written into the the Text Area to have it function as a mini-html editor.

The problem I am running into is that I am having a hard time figuring out how I would work changing the Font settings to different elements. Such as Bold, Italic and Underline. I want this to function via a selection method. I am really unsure of how to get started on this.

Anyone out there have any ideas?

code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Content{

public static void main(String[] args){
ContentFrame frame = new ContentFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}

class ContentFrame extends JFrame implements ActionListener{

public static final int WIDTH= 640;
public static final int HEIGHT= 480;

public ContentFrame() {
setTitle("Student Affairs Content Management");
setSize(WIDTH,HEIGHT);

JPanel stylePanel = new JPanel();
stylePanel.setLayout(new GridLayout(3,1));
stylePanel.add(new JButton("Bold"));
stylePanel.add(new JButton("Italic"));
stylePanel.add(new JButton("Underline"));

JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);

JMenu menu = new JMenu("File");
menuBar.add(menu);

JMenuItem menuItem = new JMenuItem("Open");
menuItem.addActionListener(this);
menu.add(menuItem);

menuItem = new JMenuItem("Close");
menuItem.addActionListener(this);
menu.add(menuItem);

menuItem = new JMenuItem("Save");
menuItem.addActionListener(this);
menu.add(menuItem);

menuItem = new JMenuItem("Quit");
menuItem.addActionListener(this);
menu.add(menuItem);

menu = new JMenu("Style");
menuBar.add(menu);

menuItem = new JMenuItem("Bold");
menuItem.addActionListener(this);
menu.add(menuItem);

menuItem = new JMenuItem("Italic");
menuItem.addActionListener(this);
menu.add(menuItem);

menuItem = new JMenuItem("Underline");
menuItem.addActionListener(this);
menu.add(menuItem);



Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(stylePanel,BorderLayout.EAST);

JEditorPane editorPane = new JEditorPane();
editorPane.setEditable(true);
JScrollPane scrollPane = new JScrollPane(editorPane);

contentPane.add(scrollPane, BorderLayout.CENTER);
}

public void actionPerformed(ActionEvent e) {

}
}



InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 01-14-2003 08:42

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 01-14-2003 17:58

Ini - thanks a lot for those resources.

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 01-15-2003 05:12

Since I finally found a solutions I thought you might want me to share. Hope you enjoy.

code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.util.Hashtable;

public class Content{

public static void main(String[] args){
ContentFrame frame = new ContentFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.show();
}
}

class ContentFrame extends JFrame implements ActionListener{

private static final int WIDTH= 640;
private static final int HEIGHT= 480;

private Action boldAction;
private Action italicAction;
private Action underlineAction;
private Action linkAction;

private JButton testOutput;

private JTextPane editorPane;
private JScrollPane scrollPane;

Hashtable actions;

public ContentFrame() {
setTitle("Student Affairs Content Management");
setSize(WIDTH,HEIGHT);

boldAction = new StyledEditorKit.BoldAction();
boldAction.putValue(Action.NAME, "Bold");

italicAction = new StyledEditorKit.ItalicAction();
italicAction.putValue(Action.NAME, "Italic");

underlineAction = new StyledEditorKit.UnderlineAction();
underlineAction.putValue(Action.NAME, "Underline");

testOutput = new JButton("Test Output");
testOutput.addActionListener(this);

JPanel stylePanel = new JPanel();
stylePanel.setLayout(new GridLayout(4,1));
stylePanel.add(new JButton(boldAction));
stylePanel.add(new JButton(italicAction));
stylePanel.add(new JButton(underlineAction));
stylePanel.add(testOutput);

setJMenuBar(createMenuBar());

Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(stylePanel,BorderLayout.EAST);

editorPane = new JTextPane();
editorPane.setCaretPosition(0);
editorPane.setMargin(new Insets(5,5,5,5));
editorPane.setContentType("text/html");

editorPane.setEditable(true);
scrollPane = new JScrollPane(editorPane);
scrollPane.setPreferredSize(new Dimension(400,300));

contentPane.add(scrollPane, BorderLayout.CENTER);

createActionTable(editorPane);
}

private void createActionTable(JTextComponent textComponent){
actions = new Hashtable();
Action[] actionsArray = textComponent.getActions();
for (int i = 0; i < actionsArray.length; i++){
Action a = actionsArray[i];
actions.put(a.getValue(Action.NAME), a);
}
}

private Action getActionByName(String name){
return (Action)(actions.get(name));
}



public void actionPerformed(ActionEvent e) {
System.out.println(editorPane.getText());
}

public JMenuBar createMenuBar(){
JMenuBar menuBar = new JMenuBar();

JMenu menu = new JMenu("File");
menuBar.add(menu);

JMenuItem menuItem = new JMenuItem("Open");
menuItem.addActionListener(this);
menu.add(menuItem);

menuItem = new JMenuItem("Close");
menuItem.addActionListener(this);
menu.add(menuItem);

menuItem = new JMenuItem("Save");
menuItem.addActionListener(this);
menu.add(menuItem);

menuItem = new JMenuItem("Quit");
menuItem.addActionListener(this);
menu.add(menuItem);

menu = new JMenu("Style");
menuBar.add(menu);

menuItem = new JMenuItem(boldAction);
menu.add(menuItem);

menuItem = new JMenuItem("Italic");
menuItem.addActionListener(this);
menu.add(menuItem);

menuItem = new JMenuItem("Underline");
menuItem.addActionListener(this);
menu.add(menuItem);

return menuBar;
}
}



InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 01-15-2003 14:00

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

WarMage
Maniac (V) Mad Scientist

From: Rochester, New York, USA
Insane since: May 2000

posted posted 01-15-2003 16:09

Glad, someone other than me could gain some enjoyment out of the solution.

Thanks for the help with the links.

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 01-15-2003 23:04

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

« BackwardsOnwards »

Show Forum Drop Down Menu