Can you prevent a Highlight from growing?

I want the highlight to remain a fixed size but it will still grow when you add a character after the highlight.
a) click the "Show Highlights" button to see the current highlights
b) position the caret after the "8" and enter a character
c) click the "Show Highlights" button again and the last position value will have changed.
Is there a way to prevent this?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class HighlightQuestion extends JFrame
     private JTextComponent component;
     private Highlighter highlighter;
     private Highlighter.HighlightPainter painter;
     public HighlightQuestion()
          component = new JTextField(20);
          component.setText("01234567890");
          getContentPane().add(component, BorderLayout.NORTH);
          JButton show = new JButton("Show Highlights");
          show.addActionListener( new ActionListener()
               public void actionPerformed(ActionEvent e)
                    Highlighter.Highlight[] highlights = component.getHighlighter().getHighlights();
                    System.out.println("Show:");
                    for (int i = 0; i < highlights.length; i++)
                         Highlighter.Highlight h = highlights;
                         System.out.println("\t" + h.getStartOffset() + " : " + h.getEndOffset());
          getContentPane().add(show, BorderLayout.SOUTH);
          highlighter = component.getHighlighter();
          painter = new DefaultHighlighter.DefaultHighlightPainter( Color.YELLOW );
          try
               highlighter.addHighlight(2, 4, painter);
               highlighter.addHighlight(7, 9, painter);
          catch(BadLocationException ble)
               System.out.println(ble);
     public static void main(String args[]) throws Exception
          JFrame frame = new HighlightQuestion();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo( null );
          frame.setVisible(true);

Hi
I found a solution, but I am not sure if it is the best...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.*;
public class HighlightQuestion extends JFrame
    private JTextComponent component;
    private Highlighter highlighter;
    private Highlighter.HighlightPainter painter;
    public HighlightQuestion()
        component = new JTextField(20);       
        component.setText("01234567890");
        component.addCaretListener(new CaretListener()
            public void caretUpdate(CaretEvent e)
                removeHighlights(component);
                setHighLights();
        getContentPane().add(component, BorderLayout.NORTH);
        JButton show = new JButton("Show Highlights");
        show.addActionListener(new ActionListener()
            public void actionPerformed(ActionEvent e)
                Highlighter.Highlight[] highlights = component.getHighlighter().getHighlights();
                System.out.println("Show:");
                for (int i = 0; i < highlights.length; i++)
                    Highlighter.Highlight h = highlights;
System.out.println("\t" + h.getStartOffset() + " : " + h.getEndOffset());
getContentPane().add(show, BorderLayout.SOUTH);
this.setHighLights();
public void removeHighlights(JTextComponent textComp)
Highlighter hilite = textComp.getHighlighter();
Highlighter.Highlight[] hilites = hilite.getHighlights();
for (int i=0; i<hilites.length; i++)
hilite.removeHighlight(hilites[i]);
public void setHighLights()
highlighter = component.getHighlighter();
painter = new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
try
highlighter.addHighlight(2, 4, painter);
highlighter.addHighlight(7, 9, painter);
catch (BadLocationException ble)
System.out.println(ble);
public static void main(String args[]) throws Exception
JFrame frame = new HighlightQuestion();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

Similar Messages

  • Can you prevent Procurement Wizard from popping up when Procurement Documen

    Can you prevent Procurement Wizard from popping up when Procurement Document is checked on the Logistics tab?
    We would like to handle all Procurement Documents in a process separate from entry of the Sales Order. Is there a way to not have the Procurement Wizard pop up automatically when a Sales Order has Procurement Document checked and is then added?
    Alan

    Hi Alan,
    Please check Note No. :1524113. There is a change in working of Procurement Wizard as follows which is mentioned in the Note.
    In the "Customer" window of the procurement confirmation wizard, the "Include All Open Sales Orders" checkbox was added. If you select this checkbox, all open sales orders with unfulfilled purchase quantities for the selected business partners are displayed.
    If you do not select this checkbox, only those sales orders are displayed for which the "Procurement Document" option was checked or that use a drop-ship warehouse and have unfulfilled purchase quantities.
    Check in Demo if you do not tick the checkbox 'Procurement Document' and in the Procurement Confirmation wizard, use the 'Include All Open Sales Orders, whether the same helps.
    Kind Regards,
    Jitin
    SAP Business One Forum Team

  • Can you prevent a pdf from opening if a user has javascript disabled?

    I have code that is important to always be running in a pdf file for corporate security reasons.  I understand some users may choose not to allow scripting which is fine but in those cases I do not want the user to be allowed to open the file.  Is there a way to prevent the file from opening if javascript is disabled?
    Thanks in advance.

    No. However, you can have a layer covering the content of the file which can be made hidden using JS code when the file is opened.
    But the user will still be able to hide the layer manually.

  • Can you prevent Apple TV from connecting to the internet?

    Is there a way to prevent Apple TV from updating and connecting to the internet? My internet is metered and slow, and I just use the AtV to mirror my itunes and stream content from my computer. I do not need the AtV connecting to the internet to load previews, pictures of newest tv shows or even genius recommendations.

    The main problem is that for protected content in iTunes AppleTV sometimes needs to get a playback authorisation across the internet.
    I've never quite figured what triggers this but it's often when content is new and hasn't been played on the computer before.
    I took AppleTV, iPad and MacMini on holiday where I had no internet access and created a LAN without internet.
    It was impossible to Airplay protected content from the iPad but 90% of the protected content in itunes on teh Mac Mini would stream and play - some new kids shows I purchased and had not played before would not stream.
    Don't suppose you can connect the computer direct to AppleTV via ethernet and see if that lets you stream and play without internet connectivity?
    Alternatively can you seletively turn off your router at times to avoid AppleTv connecting - that usually makes it only display Computers (iTunes streaming locally) and Settings as teh other icons are generated dynamically depending on availability tested via Apple's servers.
    AC

  • How can you prevent the mac book pro from unexpectedly shutting off and beeping three times repeatedly?

    How can you prevent the mac book pro from unexpectedly shutting off and beeping three times repeatedly?

    The three beeps are an indication of a hardware problem. Power On Self-Test Beep Definition - Part 2 - Apple Support

  • How can you prevent encrypted folders that you created in disc utility from being deleted?

    How can you prevent encrypted folders that you created in disc utility from being deleted?

    bibst wrote:
    How can you prevent encrypted folders that you created in disc utility
    You cannot create encrypted folders in, or encrypt folders with, Disk Utility.
    You can create with Disk Utility encrypted disk images from folders. I assume that's what you mean.
    The proper way to do it is by setting the immutable bit. Read here
    <http://www.thexlab.com/faqs/immutableflags.html>
    about the immutable bits (and the trouble they sometimes create).
    The user immutable bit (uchg) is the same as the Finder lock, as described above by X423424X. It's a weak lock, with only a warning.
    The system immutable bit (schg) is more powerful. Once is set, the item cannot be deleted in Finder (even though Finder will ask for your password and may give you the impression that it can trash it; it will end with an error).
    I set it in Terminal, thus
    $ sudo chflags schg my_encrypted_disk_image
    and unset it also in Terminal
    $ sudo chflags noschg my_encrypted_disk_image
    (Note that, when you unset the system immutable bit, the change won't be reflected immediately in Finder, but the item can be trashed nevertheless.)

  • Can't prevent unwanted toolbar from loading in Acrobat Pro 9

    I downloaded an evaluation copy of EverMap AutoBookmark plug-in.  I prefer not to automatically open toolbars that I don't use regularly when first opening Acrobat, but I can't keep the AutoBookmark toolbars from loading, even if I uncheck those particular toolbars.  EverMap support claims that it is a problem in Acrobat.  I have also had the same issue with a free plug-in from the Acrobat for Legal Professionals site. Plug-ins work fine, but I can't prevent the toolbars from loading on startup.
    Anyone else experience this issue?

    Sometimes it is simplest to simply enter the Acrobat folder and move the plugins that are a problem. Just copy then to a backup so you can put them back when you need them. I think that the presence or absence of the plugins is all that Acrobat needs.

  • How can we prevent a user from entering a value in Parameter Field?

    how can we prevent a user from entering a value in Parameter Field and Select-options Field?

    make it invisible, make it inactive, make it display only.
    if the field is not there or not to be seen the user WILL have problems inputting data.
    BUT once the field is there you can NOT prevent that the user fills it with data.
    all you can do is on PAI check if the data the user inputted was good or bad, and clear his inputs, but you can NOT prevent him inputting something.

  • How can I prevent my iPhone4 from starting after connecting to Power?

    How can I prevent my iPhone4 from starting (booting) after connecting to Power?

    You can't so far as I know. You can however, shut it down after it's connected to power and it should not power back on as long as it's connected. If the power is disconnected/reconnected, it will power back on.
    If you just want to avoid calls, etc. while it's plugged in, you could just put it in airplane mode.

  • How can i prevent my pro from over heating?

    How can i prevent my pro from over heating?

    Don't use the computer in your lap, or on a blanket or pillow. The case bottom is part of the heat dissipation system so it needs air underneath. If you must use the computer in your lap, get a lapboard. There are cheap, even more so if you make one yourself.
    If the computer is a older MBP with silver keys, avoid "clamshell" mode when using an external monitor. Those models let a lot of heat escape through the keyboard. The black-key Unibody versions don't seem to rely so much on the keyboard as a vent.
    Like eww said, what feels hot to you may not threaten the computer's well-being. Get a temperature monitoring utility. I like this one:
    http://bresink.com/osx/TemperatureMonitor.html
    It runs politely in the background and puts the readout in the menubar:
    so you don't have to invoke Dashboard like some other temp monitors require. As Dashboard loads all its widgets as soon as it's invoked, the extra widget processes can run up the temps and you don't get a true average use temp.
    If you use Dashboard a lot, those processes stay active after the first launch and can eat processor cycles as well as increase temps. I use DashQuit, a widget that shuts down Dashboard when I'm done without requiring a computer restart. It's here:
    http://www.apple.com/downloads/dashboard/status/dashquit_berenguierduncan.html

  • Can we prevent the user from saving the document?

    Hi All,
    I want to know that can we prevent the user from saving  an  illustrator doc till a condition match.
    if we can, then how?
    Please comment.
    Thanks

    You can't prevent it, no. You could continually set the document modified flag to false, which would prevent regular 'Save', but they could always do 'Save As' -- nothing can stop that.

  • I have checked around the internet, but still can't find any answers. How can I prevent incoming mail from interrupting the music I am listening to on my iPhone? That 1-second muting function drives me insane, especially when a lot of mail is coming in.

    I have checked around the internet, but still can't find any answers. How can I prevent incoming mail from interrupting the music I am listening to on my iPhone? That 1-second muting function drives me insane, especially when a lot of mail is coming in.

    If you want to hear your music on iPhone without interruption, switch Settings > Airplane Mode: ON.

  • On a MacBook, in Messages, how do you prevent your IMs from being sent as texts?

    In Messages, how do you prevent your messages from being sent as texts?

    Shootist007 wrote:
    Not sure how and why this type of thing started but it seems every program you install these days likes to start part of itself when the system starts.
    That started with Lion and progresses with the hillside feline. Ever since that pair got a personality disorder and can't make up their mind whether they're computers or phones, the world went to the dogs.  "Always on" paradigm my azz!

  • I'm using "window.open()" to show one Calendar in a popup window. I can see that the popup is re-sizable. How can I prevent the user from re-sizing the popup?

    I'm using "window.open()" to show one Calendar in a popup window. I can see that the popup is re-sizable. How can I prevent the user from re-sizing the popup? I have tried "resizable=yes|no|1|0" and that seems to be not working.

    You can't prevent users from resizing a pop-up.
    *https://developer.mozilla.org/en-US/docs/Web/API/window.open

  • How do you prevent the organizer from mixing the order of pictures when down loading off the memory card?

    @How do you prevent the organizer from mixing the order of pictures when down loading off the memory card?

    This sounds like you are using Photoshop Elements, and the Photoshop Elements forum would be best at answering that.
    Photoshop Elements
    Gene

Maybe you are looking for

  • TAS and process flow

    Dear All, We are trying to use TAS for existing imeplentation. We are not able to assign load id to Sales Order manually even config settings are done for manual one. Pls. guide whether the process flow is correct for this. Create Load ID --> Assign

  • Way to find transaction code - if not listed in system-- status

    Hi friends way to find transaction code - if not listed in system--> status ex: spro- sd - sales -sales documents- sales document item -define item categories I didn't find T-code for this path from <b>system -->status</b> Please let me know the ways

  • Unable to connect to database which uses custom NLS charset

    Hello, I have custom character set installed on my database. I'm trying to connect to DB using OCI driver. On the client machine (Linux), I have Oracle 10g Release 2 installed. Custom character set is installed on the client side too and SQL*Plus con

  • Oracle JMS Advanced Queuing

    Problems with Oracle JMS Advanced Queuing Hello, We have build an application using Advanced Queuing (database version 10.2.0.1.0). Everything works fine using PL/SQL. But when we try to send in messages through the Oracle JMS interface messages with

  • ACTIONS for OM.

    Dear Gurus, How can i configure different action actions for OM so that i can regulate the flow of infotypes while creating the OBJECTS. How to configure the actions for OM What are the steps involved in the same???? Need your assistance on this.