Get font of selected text in JEditorPane - java

Basically the question says it all;
I have a JEditiorPane with the content type 'text/html'. I have created a font family and font size combo box and enabled them using the StyledEditorKit actions. This works great and I can change the font attributes of selected text (you what it does...)
The only thing is when I select the text it's a bit confusing because the ComboBox's still show the users previous selection. I would love to implement the functionality to update the combo box values according the selected text but cannot figure out how to get the selected texts font! I've tried all sorts of crafty workarounds - non of which have worked
Any Help appriciated
Thanks In Advance
Andy

Add a CaretListener. On each caretUpdate use the code
AttributeSet attrs=((StyleEditorKit)editorPane.getEditorKit()).getInputAttributes()
StyleConstants.getFontFamily(attrs);

Related

How to change the color of a selected text in a JEditorPane?

I am currently developing a program that allows you to change the color of a selected text from a JEditorPane, using setSelectedTextColor (Color.red), but it only temporarily changes the text when I run it with the cursor.
I would like to know how I can set it or change it, as well as the setForeground
Maybe this post can help you: Changing color of selected text in jTextPane
This use jTextPane instead of jEditorPane, which I've searched for and says that supports color changes better than jEditorPane.
If that doesn't works, please search in Google for your question and if you find it, we will appreciate you post it here. Thanks.

Jcombobox with Jcheckboxes

how can I create (with JAVA) Combobox that contains Checkboxes for multiple selection and display the selected items in the Combobox like this picture:
click to see the pic
and thnx for advance.
EDIT:
I found this API (JAPURA API) and it's great but when I select multiple things I want to display the selected items instead of "* multiple items *".
Here's the link to achieve your goal:
JComboBox Providing a Custom Renderer
I would suggest you create a JButton and style it in the form of a Combobox if you want that.
Then, at the onclick function (actionperformed), you create a JPanel and make it visible under the button. In the panel, you can put whatever you want, so you just put checkboxes there.
I know this is kind of a workaround of your problem, but it should be easy for you to do so, and the actual user does not see a difference at all.
Hope I could help you.
Cheers,
Lucky

Changing the font of a text in a JTextPane dynamically

I want to change the certain texts written on a JTextPane dynamically. I have a String array containing the words which should be changed
String ListMethod [] = {"forward", "backward", "left", "right"};
I've gone through some posts and many suggest to use JTextPane or JEditorPane to edit text but most of the answers given work on static text. I want to do it in a way such that as I type "forward" or "backward", etc... in the textpane, it detects this words and changes the color. How can I go about it?
Thanks for your help.
See here how to implement a DocumentListener Value Change Listener to JTextField. The have a look at javax.swing.text.Highlighter and javax.swing.text.HighlightPainter.
You have to capture the appropriate event and perform actions. For example in your case you can create an ActionListener that changes color and use registerKeyBoardAction to attach it on your JTextPane.
Oracle has a good tutorial: http://docs.oracle.com/javase/tutorial/uiswing/events/index.html on event listeners. I suggest you start getting yourself familiar there

how to set text to a jcombobox, but the text set should not be an element of jcombobox

is their any way to set text for a combo box, which is not an element of the combobox.
cboSubjects=new JComboBox();
cbo.addItem("Maths"); // and few more subjects are added
cbo.setSelectedItem("subjects"); // this does not set the default text of combobox
Is their any way to solve this problem ? I need something which works like combobox.text
property of combobox in visual basic
I am working on school management system. I need help.
I'd like to show you a possible alternative :)
Note, you need to call label.setDisplayedMnemonic('s'); and label.setLabelFor(combo) to complete the effect!
You can make jComboBox editable, cbo.setEditable(true);, and after that set the text you want: cbo.setSelectedItem("subjects");

A part of Text should be bold in jList

can i set a part of the text in a jList bold?
in some component i can set the text with html-marks to bold but not here.. is there any other way to do this?..
The default font is bold for a JList (in the Metal LAF). So you would first need to change the default font and then add your HTML string to the ListModel to only bold the text that you want displayed bold. Something like:
String[] items = { "one", "<html>normal <b>bold</b> normal</html>" };
JList list = new JList( items );
list.setFont( list.getFont().deriveFont(Font.PLAIN) );
If you have problems then post your SSCCE demonstrating the problem.
You should be able to tie into the ListCellRenderer. Since the DefaultListCellRenderer extends JLabel, I'd expect there to be some way to wedge in HTML that's getting passed over in the default usage.
Have you tried creating a custom list cell renderer yet? If not, you may wish to give this a try. The tutorials will show you how. Please have a look here:
http://download.oracle.com/javase/tutorial/uiswing/components/list.html
http://download.oracle.com/javase/tutorial/uiswing/components/list.html#renderer
http://download.oracle.com/javase/tutorial/uiswing/components/combobox.html#renderer

Categories