Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Formatting for JTextPane

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I am adding a file over here. When you run the program you will see a JTextField and a JTextPane. Whatever being directly written into JTextPane can be formatted later but I want to set some default Text Color when I write smething into JTextField and hit enter. B'se it doesn't do that. I would really appriciate if someone can look into this.

thanks in advance
Pratik
// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/Swing/magercises/TextPane/Solution/Formatter.java#2 $
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.io.*;
import java.util.*;
public class Formatter1 extends JFrame {
final static int WIDTH = 400;
final static int HEIGHT = 300;
StyledDocument doc;
JTextPane pane;
JTextField test;
JLabel statusInfo;
public Formatter1(String lab) {
super (lab);
// Get ContentPane
Container c = getContentPane();
// Setup Status Message Area
statusInfo = new JLabel();
c.add (statusInfo, BorderLayout.SOUTH);
// Setup Text Pane
doc = new DefaultStyledDocument();
pane = new JTextPane(doc);
test = new JTextField();
// Place in JScrollPane
JScrollPane sp = new JScrollPane (pane);
c.add(sp, BorderLayout.CENTER);
c.add(test, BorderLayout.NORTH);
test.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e)
{
new StyledEditorKit.ForegroundAction ("set-foreground-red", Color.red);
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setForeground(attr, Color.red);
pane.setCharacterAttributes(attr, true);
new StyledEditorKit.ForegroundAction ("set-foreground-blue", Color.blue);
pane.setSelectionStart(pane.getText().length());
pane.setText(test.getText());
pane.setSelectionEnd(pane.getText().length());
pane.setSelectedTextColor(Color.blue);
new StyledEditorKit.ForegroundAction ("set-foreground-blue", Color.blue);
attr = new SimpleAttributeSet();
StyleConstants.setForeground(attr, Color.red);
pane.setCharacterAttributes(attr, true);
new StyledEditorKit.ForegroundAction ("set-foreground-red", Color.red);
}
});
// Setup Menus
JMenuBar menuBar = new JMenuBar();
setJMenuBar (menuBar);
// Setup File Menu
JMenuItem item;
// Setup Color Menu
JMenu color = new JMenu("Color");
color.add (item = new JMenuItem ("Red"));
item.setIcon (new ColoredBox(Color.red));
item.addActionListener (new
StyledEditorKit.ForegroundAction (
"set-foreground-red", Color.red));
color.add (item = new JMenuItem ("Orange"));
item.setIcon (new ColoredBox(Color.orange));
item.addActionListener (new
StyledEditorKit.ForegroundAction (
"set-foreground-orange", Color.orange));
color.add (item = new JMenuItem ("Yellow"));
item.setIcon (new ColoredBox(Color.yellow));
item.addActionListener (new
StyledEditorKit.ForegroundAction (
"set-foreground-yellow", Color.yellow));
color.add (item = new JMenuItem ("Green"));
item.setIcon (new ColoredBox(Color.green));
item.addActionListener (new
StyledEditorKit.ForegroundAction (
"set-foreground-green", Color.green));
color.add (item = new JMenuItem ("Blue"));
item.setIcon (new ColoredBox(Color.blue));
item.addActionListener (new
StyledEditorKit.ForegroundAction (
"set-foreground-blue", Color.blue));
color.add (item = new JMenuItem ("Magenta"));
item.setIcon (new ColoredBox(Color.magenta));
item.addActionListener (new
StyledEditorKit.ForegroundAction (
"set-foreground-magenta", Color.magenta));
color.add (item = new JMenuItem ("Custom Color"));
item.addActionListener (new ActionListener() {
public void actionPerformed (ActionEvent e) {
doColorCommand();
}
});
menuBar.add (color);

}
public static void main (String args[]) {
Formatter1 frame = new Formatter1("Mini Text Editor");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
frame.setSize(WIDTH, HEIGHT);
frame.setVisible(true);
}
public void doNewCommand() {
pane.setStyledDocument (doc = new DefaultStyledDocument());
}
public void doCloseCommand() {
System.exit (0);
}
public void doOpenCommand() {
try {
FileInputStream fis = new FileInputStream ("doc.ser");
ObjectInputStream ois = new ObjectInputStream (fis);
doc = (StyledDocument)ois.readObject();
ois.close();
pane.setStyledDocument (doc);
validate();
statusInfo.setText ("Reloaded from disk");
} catch (Exception e) {
statusInfo.setText ("Unable to reload");
e.printStackTrace();
}
}
public void doSaveCommand() {
try {
FileOutputStream fos = new FileOutputStream ("doc.ser");
ObjectOutputStream oos = new ObjectOutputStream (fos);
oos.writeObject (doc);
oos.flush();
oos.close();
statusInfo.setText ("Saved to disk");
} catch (IOException e) {
statusInfo.setText ("Unable to save");
e.printStackTrace();
}
}
public void doColorCommand() {
Color color = JColorChooser.showDialog(this, "Color Chooser", Color.cyan);
if (color != null) {
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setForeground(attr, color);
pane.setCharacterAttributes(attr, false);
}
}

class ColoredBox implements Icon {
Color color;
public ColoredBox (Color c) {
color = c;
}
public void paintIcon (Component c, Graphics g, int x, int y) {
g.setColor(color);
g.fillRect (x, y, getIconWidth(), getIconHeight());
}
public int getIconWidth() {
return 10;
}
public int getIconHeight() {
return 10;
}
}
}
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pratik,
I don't really know much about the StyleConstants and StyledEditorKit. My approach would be to use a private Color class variable say: currentColor. Default it to black:
private currentColor = Color.black;
Then I would create a method to create my color menu items:

Then I would clean up the create menu with the following:

Another change I would make would be inside the test actionPerformed method. I would use the currentColor (class variable) and select the new stuff color it and then unselect it:

Then it should work as expected.
Regards,
Manfred.
 
That new kid is a freak. Show him this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic