Highlight a word within a JList Item

I would like to highlight a word red within a JList item. For example, a user has a number of words to select from such as can or will. I would like them to select that word and any entries within the list I would like highlighted. I ONLY want the word within a JList item though and not the whole JList item. I know how to do this with a TextField but am having trouble doing this within a JList. Any help would be greatly appreciated.

Use html syntax to set color/size of a list item. Try this simple example:
import java.awt.*;
import javax.swing.*;
class Testing extends JFrame {
  public Testing() {
    String [] list = {
       "<html><body>Black <font size=15>Black</font> Black",
       "<html><body>Red <font color=red size=10>Red</font> Red ",
       "<html><body>Blue <font color=blue size=5>Blue</font> Blue"
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new BorderLayout());
    JList jlist = new JList(list);
    JScrollPane spane = new JScrollPane(jlist);
    panel.add(spane);
    getContentPane().add(panel);
    setSize(200,200);
  public static void main(String[] args){
    new Testing().setVisible(true);
}

Similar Messages

  • When composing an email, how do I highlight a word in yellow (NOT change the font color, just highlight)?

    On Windows when composing an email,  I would often highlight a word or sentence in yellow. I don't seem to have this option on my Mac Book Air toolbar as I did when I had on my laptop. How can I add this feature to my toolbar? I don't want to change the font color, just want to highlight a word within the email.
    Thank you,

    Browder-41 wrote:
    How to I copy a Word document into an email without losing the design and colors in the document.  When I copy and paste I only get the typed words.  No design or color.
    You may or may not be able to do this based on which e-mail client or webmail platform you're using.  In your email options, you'll want to have the option to paste it as rich-text or HTML.  If you only have the option for plain text then you're out of luck.

  • Highlighting JList Items

    Hello
    Could anybody let me how to change the color of Few Items of JList. I know how to change the foreground color of an item which is selected. But, I need to show few items of JList in different color.
    Thanks in advance

    please can you tell us where is/are ..., could you able to demostrate your issue, now
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class JListDisabledItemDemo implements ItemListener, Runnable {
        private static final String ITEMS[] = {
            "Black", "Blue", "Green", "Orange", "Purple", "Red", "White", "Yellow"};
        private JList jList;
        private JCheckBox[] checkBoxes;
        private boolean[] enabledFlags;
        @Override
        public void run() {
            JPanel pnlEnablers = new JPanel(new GridLayout(0, 1));
            pnlEnablers.setBorder(BorderFactory.createTitledBorder("Enabled Items"));
            checkBoxes = new JCheckBox[ITEMS.length];
            enabledFlags = new boolean[ITEMS.length];
            for (int i = 0; i < ITEMS.length; i++) {
                checkBoxes[i] = new JCheckBox(ITEMS);
    checkBoxes[i].setSelected(true);
    checkBoxes[i].addItemListener(this);
    enabledFlags[i] = true;
    pnlEnablers.add(checkBoxes[i]);
    jList = new JList(ITEMS);
    jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jList.setSelectionModel(new DisabledItemSelectionModel());
    jList.setCellRenderer(new DisabledItemListCellRenderer());
    jList.addListSelectionListener(new ListSelectionListener() {
    @Override
    public void valueChanged(ListSelectionEvent e) {
    if (!e.getValueIsAdjusting()) {
    System.out.println("selection");
    JScrollPane scroll = new JScrollPane(jList);
    scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
    scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    JFrame f = new JFrame("JList and Colors");
    Container contentPane = f.getContentPane();
    contentPane.setLayout(new GridLayout(1, 2));
    contentPane.add(pnlEnablers);
    contentPane.add(scroll);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(240, 280);
    f.setLocationRelativeTo(null);
    UIManager.put("List.background", Color.red);
    UIManager.put("List.selectionBackground", Color.orange);
    UIManager.put("List.selectionForeground", Color.blue);
    UIManager.put("Label.disabledForeground", Color.magenta);
    SwingUtilities.updateComponentTreeUI(f);
    f.setVisible(true);
    @Override
    public void itemStateChanged(ItemEvent event) {
    JCheckBox checkBox = (JCheckBox) event.getSource();
    int index = -1;
    for (int i = 0; i < ITEMS.length; i++) {
    if (ITEMS[i].equals(checkBox.getText())) {
    index = i;
    break;
    if (index != -1) {
    enabledFlags[index] = checkBox.isSelected();
    jList.repaint();
    public static void main(String args[]) {
    SwingUtilities.invokeLater(new JListDisabledItemDemo());
    private class DisabledItemListCellRenderer extends DefaultListCellRenderer {
    private static final long serialVersionUID = 1L;
    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
    if (enabledFlags[index]) {
    return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
    Component comp = super.getListCellRendererComponent(list, value, index, false, false);
    comp.setEnabled(false);
    return comp;
    private class DisabledItemSelectionModel extends DefaultListSelectionModel {
    private static final long serialVersionUID = 1L;
    @Override
    public void setSelectionInterval(int index0, int index1) {
    if (enabledFlags[index0]) {
    super.setSelectionInterval(index0, index0);
    } else {
    if (getAnchorSelectionIndex() < index0) {
    for (int i = index0; i < enabledFlags.length; i++) {
    if (enabledFlags[i]) {
    super.setSelectionInterval(i, i);
    return;
    } else {
    for (int i = index0; i >= 0; i--) {
    if (enabledFlags[i]) {
    super.setSelectionInterval(i, i);
    return;

  • Highlighting different rows within a JTable

    I have a JTable that has 20 rows. I also have a Jlist next to the JTable. When someone selects something within the Jlist I want to highlight certain rows depending on what was selected within that list. For instance if a user selects Item1 within the list I want to highlight the 3rd, 8th and 11th row within a JTable. The problem is the JTable API will let you highlight a number of rows but not allow you to specify single rows to highlight at one time. When I run my code to do this I get the last row highlighted. The reason is because the 3rd and 8th row do get highlighted but when I call the setRowSelectionInterval(i, i); where i is the row to highlight it has already passed and will end up highlighting the 11th row only. Is there any work around for this?

    First, make sure you table supports multi-select. Turn it on in this case, otherwise you can't select multiple items.
    To color different rows, you'll have to set a cell renderer and use it to color those rows. It's possible to collor each and every cell a different color, set fonts, images, etc..you just need a good cell renderer.

  • Double click for JList items selection

    I would like to select JList items only by double clicking on them. I can write a mouse listener for detecting double clicks by my own (and handle it afterwards), by please tell me how to turn the default behavior off (selecting items by clicking one time).
    Thanks in advance
    Marek

    I would like to select JList items only by double clicking on them. Well the standard is to select or highlight on a mouseClick or by using the arrow keys on the keyboard.
    Then once you have a selected item you can perform and Action on the item by double clicking or by using the enter key. Remember you should always be able to use either the keyboard or the mouse to perform any given function.
    This posting shows my solution for the above scenario:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=626866

  • Italicizing words within a paragraph

    I am currently writing a paragraph which has been formatted using a paragraph style from the styles drawer. Within my paragraph I want to italicize just a few words, however it will not let me do this. When I highlight the words and click italicize nothing happens. When I look in the Format > Font menu, italicized has been ticked, however the word does not look italicized and nor is it when I print it. I've tried changing the paragraph style in styles drawer to see if this will make it work but it doesn't seem to make a difference.
    Any advice? Italicizing some words is necessary for APA styled reports so its really important I work out how to do this...
    Thanks for your help

    The use of glyphs grabbed from other fonts is not a new feature.
    It's done to give the user the illusion that he has a complete font.
    The oddities may be seen very easily.
    Sometimes they impact the real size of the chars and lines using only embedded chars have an height different of lines using borrowed ones.
    The problem with super and subscript is the same.
    If we want to have a correct result, it's good practice to use an application able to tell us which glyphs are available in the font which we plan to use.
    I remember that one day I discovered that to get the full set of sub and superscript with a selected font, these ones where in fact grabbed in three fonts which explained the awful result.
    It's always the same problem, so called 'Desktop Publishing' appliation give to Mr everybody the illusion than creating a document is a simple task.
    I faught quite all the afternoon trying to help my ten years old grand son because one of his teacher gave him (in fact it gave it at the whole class) an exercice requiring the building of a page layout using the Fontwork tool in openOffice.
    The content was of no importance. What was important was to use a lot of bells and whistles.
    I was heavily provoked by this foolish requirement.
    In French we call that 'mettre la charrue avant les boeufs'.
    Teach them to use correctly the language. It will be time later to ask them to think to the pages layout.
    Back to faux italics.
    You may be right for the explanations of Apple choices.
    As a guy highly concerned for professional reasons by intellectual property, I like to think that Apple took care of it.
    When I was a children I hate thos breaking my toys. Please, don't break my dreams
    Yvan KOENIG (VALLAURIS, France) dimanche 13 juin 2010 20:27:35

  • Highlight multiple words in column formatting

    Hi,
    it should be possible to highlight multiple words by using a comma delimited list, but only the first item in the list is highlighted. It this a bug?
    Thanks, Maren

    Hello,
    I have the same problem. Did you find any workaround or something?
    Thanks,
    Andrei

  • Searching multiple words within page separately

    1. I am searching within a page using the Find function.
    2. When I type in a search word, Find accurately highlights the word.
    3. I am trying to search multiple words at once, and have Find highlight them separately.
    E.g. Search for "motor OR cables" would search for "motor" and "cables" and not "motor cables" i.e. the search string has to be truncated to allow for each word to be searched separately.

    Hello,
    You can use the following Add-on and let me know if It helped your cause:
    https://addons.mozilla.org/en-US/firefox/addon/searchwp/
    If it did not help you,please get back to me. I will try to help you again.

  • Highlight matching word in AutoSuggest

    I am using AutoSuggestBehavior component to implement type-ahead feature with inputText component. I am wondering if it is possible to highlight the matching letters in the suggested items.
    Here is an example:
    Say I have following LOV items:
    Oracle Application Server
    Oracle JDeveloper
    My Oracle Support
    Now when I type "Oracle", it should highlight (bold) the text "*Oracle*" within the suggested item. So the suggested items would look like this:
    Oracle Application Server
    Oracle JDeveloper
    My Oracle Support
    Is that possible? Is there any custom solution for this?
    Thanks!

    Hi,
    haven't tried, but in a similar use case for tables, I did parse the return values and added HTML markup to it. For example, you can add <b></b> around the string and see if it renders in bold. I think I used a tag that allowed me to use CSS for the color coding. Give it a try, maybe it works for you
    Frank

  • Why can I not highlight single words in a pdf document with Adobe Reader for Android?

    I, like other users I have seen, am unable to highlight single words or phrases in pdf files.  I am using a Galaxy Note 10.1 tablet.

    Hi,
    Welcome to Adobe forums.
    Please see : http://helpx.adobe.com/acrobat/kb/print-comments-acrobat-reader.html
    I hope this will help you.
    Please let me know, if you need any more help on this.
    ~ Aditya

  • How to center JList  items

    Hi,
    I want to center my Jlist item instead of leave them in the left like usual. My List contains pictures and for the beauty of the design it's better to have the image in the center of each cells. How to do it?
    thanks.

    Do you know how to make a JLabel which contains a centred icon? Now make a ListCellRenderer that uses one of those to render your images in the JList.

  • How to save JList items!

    hello,
    i m creating a class that has three JList components.
    My problem is how to save the each JList items into a seperate logfile(ie. text file).

    More info required:
    What are in the JList? Strings? Custom objects?
    Do you know basic file IO?
    http://java.sun.com/docs/books/tutorial/essential/io/index.html
    What you will need to do is get the model, ilterate over each element, then write that element out. If the element is a string, then that is easy, just create a FileWriter, and write the string, followed by a new line.
    If it is a custom object, then the task is harder, and you need to decide how that object should be represented.
    If you are after a computer readable view of the model, rather than a human readable, which can easily be read by Java back into a ListModel, then you want to look at XMLEncoder and XMLDecode, or the Serializing Objects tutorial.
    http://java.sun.com/docs/books/tutorial/uiswing/components/list.html

  • How to highlight specific words in the Reader

    Hi!
    I am new to the Acrobat SDK and could need some guidance on how to solve the following problem:
    I need a plug-in that is able to highlight specific words in a pdf file (e.g. by underlining it or changing the background color) and offers a context menu with futher actions when the user clicks on a highlighted word. The first step would be searching the text to find the word that should be marked. I think that should not be that hard but how can I highlight specific words? I first thought of annotations but I need it in the Reader and I should not change the pdf file itself (just the view of it).
    The idea is similar to the skype plug-in for Firefox or IE. The skype plug-in highlights phone numbers. We need to highlight specific words.
    Thanks for every hint on how to achieve this!
    Fabian

    >The documentation says that AVPageViewHilightText is supposed to use >the current color set with AVPageViewSetColor - so it should work. >What version of Acrobat are you working with?
    Yes that how understood the documentation too. I tried the following:
    //Define red
    red.space = PDDeviceRGB;
    red.value[0] = ASInt32ToFixed(1);
    red.value[1] = 0;
    red.value[2] = 0;
    // highlight
    AVPageViewSetColor(pageView, &red);
    PDTextSelect pdTextSelect = PDDocCreateTextSelect(pdDoc, pageNum, &bbox);
    AVPageViewHighlightText(pageView, pdTextSelect);
    I am using Acrobat 7.0.9.
    >Drawing an image would be done with AVPageViewDrawCosObj
    >- which means you'll need to bring the image in as a PDF
    > object and then render it.
    Oh I should have seen that. Thanks for the hint!!
    >Interesting, there should be a way to get transparency,
    > but I am not seeing it at the moment.
    As far as I understood it, it is possible to set the color to transparent but thats not what I am looking for. I would like to have a way of changing the alpha channel of the color.
    Thanks!
    Fabian

  • I just downloaded Pages. When typing on a blank document, I am unable to highlight certain words.  My daughter usesbitbfor homework and needs to highlight her spelling words.  I've read disucssions and it says gonto the a and enter fill and cg

    I just started using Pages and am trying to figure out how I can highlight certain words in sentences.  I have read some of the discussions and they have said to go to the "a" on the top bar.  When using a blank document, the letter "a" doesn't show up and there isn't an option to fill in or use shadowing.  Help!

    At the top of the screen in Page there is an "I" next to the Media Icon. If you tap that "I" icon - you bring up a menu that has Style as one of the options. Tap Style and you can change the color of the text and use all of the preinstalled Styles available in the app.
    If you swipe all the way to the bottom of the Styles menu- there is a Text Options listed. Tap on that and you can select different sizes, fonts, and text colors.

  • Word wrap in JList

    I was wondering if I can do a word wrap with JLists.
    I have this so far.
    JList pizza;
    DefaultListModel listPizza;
    JScrollPane scrollPizza;
    listPizza = new DefaultListModel();
    pizza = new JList(listPizza);
    scrollPizza = new JScrollPane(pizza);
    This will let me scroll horizontally if the text becomes longer than the screen, but I want it to wrap around the screen. I only want to it to scroll vertically.

    In the future, swing questions should be posted in the swing forum.
    If I recall correctly, you can do this by making a custom CellRenderer for your JList which uses a JTextArea to do the rendering. Search the swing forum or look at the sun java tutorial for custom renderers.

Maybe you are looking for

  • Auto load library

    I installed iTunes 12 for Windows. I was hoping it would allow me to separate my Christmas music from my regular music. I learned that iTunes will allow multiple libraries. I hold the SHIFT key while clicking on iTunes in the Start Menu and upon laun

  • SAP Groupware Connector 5.0 Installation - Connect MS Exchange 2010

    I try to install the SAP Groupware Connector 5.0 to connect a MS Exchange 2010 Server. My host computer has an MS Windows Server 2008 operation system. After starting the setup.exe and fill in the entries Exchange Server, Mailbox, User, Domain, Passw

  • Encore 2.0 doesn't play audio from .mpg file

    A long (1,5 hour) file transcoded from APPro 2.0 with media encoder. Encore 2.0 takes about 20 min to import that file as asset, and then - no audio playing in preview! I thought it was transcoding, or smth. and waited for an hour - still no audio! N

  • Creating recommendations by system in Multi providers

    hi all, While we are creating dimensions on mp? we have the options of creating recommendations by ourself nor to carry out by the system itself. When shall we go for creating recommendations by ourselves and when shall we go for creating recommendat

  • My macbook pro resets when playing games in windows

    I have a macbook pro 2011 and I installed windows with boot camp. when i play games in windows after about 1 hour or so the system resets. why? I dont have any games installed in macintoch so I dont know if it has the same problem in macintoch too.