Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Sourabh Chourasiya
Java
2 Answer(s)      3 years and 6 months ago
Posted in : JDBC


How to define rich text box in java?


Advertisement
View Answers

April 21, 2011 at 6:02 PM


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.text.*;

public class RichTextBox{
  public RichTextBox(){
    JFrame frame = new JFrame();
    JTextPane textPane = new JTextPane();
    JScrollPane scrollPane = new JScrollPane(textPane);

    JPanel north = new JPanel();
    JMenuBar menu = new JMenuBar();
    JMenu boldMenu = new JMenu();
    boldMenu.setText("B");

    JMenu italicMenu = new JMenu();
    italicMenu.setText("I");

    JMenu lineMenu = new JMenu();
    lineMenu.setText("U");

    JMenu fontMenu = new JMenu();
    fontMenu.setText("F");

    JMenu colorMenu = new JMenu();
    colorMenu.setText("A");

    Action boldAction = new BoldAction();
    boldMenu.add(boldAction);

    Action italicAction = new ItalicAction();
    italicMenu.add(italicAction);

    Action uAction = new UnderLineAction();
    lineMenu.add(uAction);

    Action foregroundAction = new ForegroundAction();
    colorMenu.add(foregroundAction);

    Action formatTextAction = new FontAction();
    fontMenu.add(formatTextAction);

    menu.add(boldMenu);
    menu.add(italicMenu);
    menu.add(lineMenu);
    menu.add(fontMenu);
    menu.add(colorMenu);
    north.add(menu);
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(north, BorderLayout.NORTH);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(400, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
  public static void main(String[] args) {
    new RichTextBox();
  }
}
class BoldAction extends StyledEditorKit.StyledTextAction {
  public BoldAction(){
    super("bold");
  }
    public void actionPerformed(ActionEvent e){
    JEditorPane editor = getEditor(e);
    if(editor != null){
      StyledEditorKit kit = getStyledEditorKit(editor);
      MutableAttributeSet attr = kit.getInputAttributes();
      boolean bold = (StyleConstants.isBold(attr)) ? false : true;
      SimpleAttributeSet sas = new SimpleAttributeSet();
      StyleConstants.setBold(sas, bold);
      setCharacterAttributes(editor, sas, false);
    }
  }
}
class UnderLineAction extends StyledEditorKit.StyledTextAction {
   public UnderLineAction(){
    super("underline");
   }
    public void actionPerformed(ActionEvent e) {
    JEditorPane editor = getEditor(e);
    if(editor != null){
      StyledEditorKit kit = getStyledEditorKit(editor);
      MutableAttributeSet attr = kit.getInputAttributes();
      boolean ul = (StyleConstants.isUnderline(attr)) ? false : true;
      SimpleAttributeSet sas = new SimpleAttributeSet();
      StyleConstants.setUnderline(sas, ul);
      setCharacterAttributes(editor, sas, false);
    }
  }
}
class ItalicAction extends StyledEditorKit.StyledTextAction{
  public ItalicAction() {
    super("font-italic");
  }
   public void actionPerformed(ActionEvent e) {
    JEditorPane editor = getEditor(e);
    if (editor != null){
      StyledEditorKit kit = getStyledEditorKit(editor);
      MutableAttributeSet attr = kit.getInputAttributes();
      boolean italic = (StyleConstants.isItalic(attr)) ? false : true;
      SimpleAttributeSet sas = new SimpleAttributeSet();
      StyleConstants.setItalic(sas, italic);
      setCharacterAttributes(editor, sas, false);
    }
  }
}


April 21, 2011 at 6:03 PM


continue....

class ForegroundAction extends StyledEditorKit.StyledTextAction {
  JColorChooser colorChooser = new JColorChooser();
  JDialog dialog = new JDialog();
  boolean noChange = false;
  boolean cancelled = false;
  public ForegroundAction(){
    super("foreground");
  }
  public void actionPerformed(ActionEvent e){
    JTextPane editor = (JTextPane) getEditor(e);
    int p0 = editor.getSelectionStart();
    StyledDocument doc = getStyledDocument(editor);
    Element paragraph = doc.getCharacterElement(p0);
    AttributeSet as = paragraph.getAttributes();
    fg = StyleConstants.getForeground(as);
    if(fg == null){
    fg = Color.BLACK;
    }
    colorChooser.setColor(fg);
    JButton accept = new JButton("OK");
    accept.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        fg = colorChooser.getColor();
        dialog.dispose();
      }
    });
    JPanel buttons = new JPanel();
    buttons.add(accept);
    dialog.getContentPane().setLayout(new BorderLayout());
    dialog.getContentPane().add(colorChooser, BorderLayout.CENTER);
    dialog.getContentPane().add(buttons, BorderLayout.SOUTH);
    dialog.setModal(true);
    dialog.pack();
    dialog.setVisible(true);
    if(!cancelled){
      MutableAttributeSet attr = null;
      if(editor != null){
        if(fg != null && !noChange){
          attr = new SimpleAttributeSet();
          StyleConstants.setForeground(attr, fg);
          setCharacterAttributes(editor, attr, false);
        }
      }
    }
    noChange = false;
    cancelled = false;
  }
  private Color fg;
}
class FontAction extends StyledEditorKit.StyledTextAction {
  private String family;
  private float fontSize;
  JDialog formatText;
  private boolean accept = false;
  JComboBox fontFamilyChooser;
  JComboBox fontSizeChooser;
  public FontAction(){
    super("Font");
  }
  public void actionPerformed(ActionEvent e){
    JTextPane editor = (JTextPane) getEditor(e);
    int p0 = editor.getSelectionStart();
    StyledDocument doc = getStyledDocument(editor);
    Element paragraph = doc.getCharacterElement(p0);
    AttributeSet as = paragraph.getAttributes();

    family = StyleConstants.getFontFamily(as);
    fontSize = StyleConstants.getFontSize(as);

    formatText = new JDialog(new JFrame(), "Font", true);
    formatText.getContentPane().setLayout(new BorderLayout());

    JPanel choosers = new JPanel();
    choosers.setLayout(new GridLayout(2, 1));

    JPanel fontFamilyPanel = new JPanel();
    fontFamilyPanel.add (new JLabel("Font"));

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    String[] fontNames = ge.getAvailableFontFamilyNames();

    fontFamilyChooser = new JComboBox();
    for (int i = 0; i < fontNames.length; i++) {
      fontFamilyChooser.addItem(fontNames[i]);
    }
    fontFamilyChooser.setSelectedItem(family);
    fontFamilyPanel.add(fontFamilyChooser);
    choosers.add(fontFamilyPanel);
    JButton ok = new JButton("OK");
    ok.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent ae) {
        accept = true;
        formatText.dispose();
        family = (String) fontFamilyChooser.getSelectedItem();
      }
    });
    JPanel buttons = new JPanel();
    buttons.add(ok);
    formatText.getContentPane().add(choosers, BorderLayout.CENTER);
    formatText.getContentPane().add(buttons, BorderLayout.SOUTH);
    formatText.pack();
    formatText.setVisible(true);

    MutableAttributeSet attr = null;
    if(editor != null && accept) {
      attr = new SimpleAttributeSet();
      StyleConstants.setFontFamily(attr, family);
      StyleConstants.setFontSize(attr, (int) fontSize);
      setCharacterAttributes(editor, attr, false);
    }
  }
}


Related Tutorials/Questions & Answers:
java
java  diff bt core java and java
java
java  what is java
Advertisements
Java
Java   Whether Java is pure object oriented Language
java
java  different between java & core java
java
java  explain technologies are used in java now days and structure java
java
java   why iterator in java if we for loop
JAVA
JAVA  how the name came for java language as "JAVA
java
java  what is java reflection
java
java  is java open source
java
what is the size of array in java ?  what is the size of array in java ? what is the mean of finalize in java
java
java   in java does not pointers concept but what is nullpointers in java?   nullpointer is a runtime Exception
java
java  write a java program using filenotfoundexception
Java
Java  how to draw class diagrams in java
java
java   difference between class and interface
java
java  why multiple inheritance is not possible in java
java
java  explain object oriented concept in java
java
java  is java purely object oriented language
Java
Java   Whether Java is Programming Language or it is SOftware
java
java  what are JAVA applications development tools
java
java  how use java method
java
java  send me java interview questions
java
java  write a program in java to acess the email
java
java  Write a java code to print "ABABBABCABABBA
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  give a simple example for inheritance in java
java
java  give a simple example for inheritance in java
Java
Java  how to do java in command prompt
java
java  Does java allows multiline comments
 

 

 

DMCA.com