dcsimg
i think i hv to put this question at least ten time.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   maneesh_sata
Posted On:   Monday, October 28, 2002 04:23 PM

hi Yasmeen! sorry u hv misinterpreted my question.i now give example t suppose there is a story all written in text pane and now i hv selected(text selection as in MSworld) one of heading or say three or more consective character , i want that from list i select color green than only the selected heading in the text pane should hv green color not all story should hv green color. In my original question i hv mentioned by using world only selected text of text pane should change color.(may seem some what confusing my statement also) thanks realy there was no compilation error. thanx and tell me now whether u can do this for me or not if u can not do than i hv to post this    More>>

hi Yasmeen!
sorry u hv misinterpreted my question.i now give example t suppose there is a story all written in text pane and now i hv selected(text selection as in MSworld) one of heading or say three or more consective character , i want that from list i select color green than
only the selected heading in the text pane should hv green color not all story should hv green color.


In my original question i hv mentioned by using world only
selected text of text pane should change color.(may seem
some what confusing my statement also)


thanks realy there was no compilation error.


thanx and tell me now whether u can do this for me or not
if u can not do than i hv to post this question 8th time(alas!).

   <<Less

Re: i think i hv to put this question at least ten time.

Posted By:   Pavan_Gattu  
Posted On:   Wednesday, October 30, 2002 09:50 AM

Hope this helps. Copy paste this code in TextPaneColor.java file compile and run. Select a part of the text and choose a color from the menu.


import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;
import javax.swing.text.*;

public class TextPaneColor extends JFrame
{
JTextPane textPane;
Document theDoc;
Hashtable actionHashtable;

public TextPaneColor ()
{
super("TextComponentDemo");
Container contentPane = this.getContentPane();
this.addWindowListener(new MyWindowListener());

//Create the text pane and configure it
textPane = new JTextPane();
theDoc = textPane.getDocument();
try
{
theDoc.insertString(theDoc.getLength(), "This is story header

", textPane.getLogicalStyle());
theDoc.insertString(theDoc.getLength(), "This is the actual story. This is first sentence.", textPane.getLogicalStyle());
}
catch (Exception e)
{
}

JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.setPreferredSize(new Dimension(400, 400));

//Add to the frame
contentPane.add(scrollPane, BorderLayout.CENTER);

//Set up the menu bar
JMenu colorsMenu = createColorsMenu();
JMenuBar mb = new JMenuBar();
mb.add(colorsMenu);
setJMenuBar(mb);
}


protected JMenu createColorsMenu()
{
JMenu menu = new JMenu("Colors");

menu.add(new StyledEditorKit.ForegroundAction("Red", Color.red));
menu.add(new StyledEditorKit.ForegroundAction("Green", Color.green));
menu.add(new StyledEditorKit.ForegroundAction("Blue", Color.blue));
menu.add(new StyledEditorKit.ForegroundAction("Black", Color.black));
menu.add(new StyledEditorKit.ForegroundAction("Yellow", Color.yellow));

return menu;
}



class MyWindowListener extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}

/**
* Where it all begins
*/
public static void main(String[] args)
{
TextPaneColor tpc = new TextPaneColor();
tpc.pack();
tpc.setVisible(true);
}
}
About | Sitemap | Contact