The Bold Attribute won't stay bold ?

Hi,
I've for a JTextPane in which I can type text, I press enter is takes a new line etc.
I also have a JButton which I've called "SetBold", and when I press this button this code line is implemented
            new StyledEditorKit.BoldAction().actionPerformed(null);Seems to work ok, when I highlight text and press the button if changes it to bold, and when no text is highlighted and I press the button, the next text I type is bold and I can press it again to do the opposite. As the java site says, its a "toggle for the bold attribute".
Problem is, if I press it, and type stuff and it appears bold. If I press backspace until all the text is deleted and start typing again, it has "toggled itself off" for some reason.
I don't want it to do that.
I want to know when the attribute is set so I can change the color of the button to notify the user that bold is set. But I can't even do this, as by monitoring the attribute (through System.out.println) it is always false on the largest/fartherst caret position (as if it is saying, there is nothing in the next space as its blank, therefore attribute is false for this space), until you type a charactor, then it changes true ! So I therefore cannot tell, whether or not the next letter I type will be bold or not.
Can anyone suggest a method of knowing when, the next letter I type, whether it will be bold or not ? This will allow me to know what color to set the JButton.
As I say, the isBold() method doesn't seem helpful for this objective.
Thanks for your help/

Hopefully I'm doing something wrong, but it doesn't work for me. Unfortunately, I couldn't get it to work either. The method doesn't work the way I would expect it to. I was able to get it working a little better than you but it still doesn't work completely. I ended up borrowing code from the JTextPane.getCharacterAttributes() method:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class TextPaneCaret extends JFrame implements CaretListener
     JTextPane textPane;
     public TextPaneCaret()
          textPane = new JTextPane();
          textPane.addCaretListener( this );
          JScrollPane scrollPane = new JScrollPane( textPane );
          scrollPane.setPreferredSize( new Dimension( 200, 200 ) );
          getContentPane().add( scrollPane );
          JButton bold = new JButton( new StyledEditorKit.BoldAction() );
          getContentPane().add(bold, BorderLayout.SOUTH);
     public void caretUpdate(CaretEvent e)
          StyledDocument doc = textPane.getStyledDocument();
          int offset = textPane.getCaretPosition();
          if (offset > 0)
               offset--;
          Element run = doc.getCharacterElement(offset);
          AttributeSet attributes = run.getAttributes();
//          AttributeSet attributes = textPane.getInputAttributes();
          System.out.println( attributes.containsAttribute(StyleConstants.Bold, Boolean.TRUE) );
     public static void main(String[] args)
          TextPaneCaret frame = new TextPaneCaret();
          frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
          frame.pack();
          frame.setVisible(true);
}The above code works after text has been typed, but it doesn't register the bold attribute until at least one character has been typed with the attribute. Example:
a) enter "abc"
b) click bold
c) enter "123"
Move caret around it will correctly display the attribute of the next character to be typed.
d) move to end of text
e) click bold
This is where it gets tricky.
If you just move the caret between the Bold characters and then move to the end of the text a "normal" character will be entered.
However, if you move the caret between the bold and normal characters and then move to the end of the text a "bold" character will be entered. Its like it lost the information that you have a "pending" normal character to enter.
Totally confusing me and I have no idea how to fix it. It looks the the only way to be 100% sure what the attribute will be is to enter the character and remove it as you have proposed.
As you can see I also did some testing with getInputAttributes. This is because I traced the DefaultEditorKit DefaultKeyTypedAction back to the replaceSelection() method of JTextPane. This method just uses a insertString(...., , getInputAttributes()). Again these attributes are correct immediately after you click on the bold button, but if you move the caret before entering any text then its possible that these attributes will change to match the attributes at the new location of the cursor. Hope that makes some sense. Anyway, I've given up.

Similar Messages

  • I have bought the wireless keyboard for my iPad.  I was working for a day but now the power light won't stay on and as such won't connect to the iPad.  I have changed the batteries but still no difference.

    I have bought the wireless keyboard for my iPad.  I was working for a day but now the power light won't stay on and as such won't connect to the iPad.  I have changed the batteries but still no difference.

    Not knowing which wireless keyboard you bought, it is difficult to be specific.  but ...
    it is quite normal for the keyboard's power light not to stay on - it is a way of conserving battery power.
    Try this:
    turn you keyboard off
    on your iPad go to Settings > Bluetooth
    if your keyboard is listed under Devices, then touch the little blue i to its right and choose Forget this Device
    now reset your iPad:  hold both the Home and Power buttons down for about 10 seconds until the Apple logo appears, let go promptly when the Apple logo appears and wait another 30 seconds for your iPad to turn itself on
    After your iPad is back on again go back to Settings > Bluetooth
    now turn your keyboard back on and see if it appeard under Now Discoverable
         if it doesn't read your keyboard guide to figure out how to put it into Discovery mode
    touch the listing for your keyboard under Now Discoverable, it should now appear under Devices
    touch the listing for you keyboard under Devices, and choose to pair with it.

  • When I have my Firefox browser running, if I bring up a document from my computer, it appears briefly, then disappears behind the browser. It shows in the taskbar, but won't stay on top until I minimize all other windows.

    When I have my Firefox browser running, if I bring up a document from my computer, it appears briefly, then disappears behind the browser. It shows in the taskbar, but won't stay on top until I minimize all other windows.

    Hi NicoMan,
    According to your description ,we seem to suffer a performance issue here.
    Have you tried to open the task manager to check the system performance  ? Is there any specific process occupied many system resources ?Will the issue occur only with the specific applications ?
    We can try to restart the Windows Explorer process to have a check.
    If it only occurred with the spefcific two applications ,we can try to end the other application processes manually from the task manager to have a check .
    If this is caused by the specific application ,we may need to look for help from the third party support considering they are all the third party applications .
    Best regards
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact [email protected]

  • When I expand a photo it will slide off screen to the right and won't stay centered

    When I expand a photo it will slide of the screen to the right and won't stay centered. It also very hard to get to the close button when this happens which is bottom right of picture.

    Hello MCoyne50,
    Thank you for using Apple Support Communities!
    It sounds like when you are viewing a photo in the Safari app and you zoom in on it, the photo moves to the right on its own and it cannot be moved back.
    I would try a couple of things here.
    First quit all the apps that art running and then restart the phone to test the issue again.
    From the Home screen, click the Home button twice.
    Tap and hold on the app.
    When it starts to jiggle, tap the - to close it.
    Double-click the Home button and try opening the app again.
    From: iOS: Force an app to close
              http://support.apple.com/kb/ht5137
    Then
    Restarting your device
    Press and hold the Sleep/Wake button for a few seconds until the red "slide to power off" slider appears, and then slide the slider.
    Press and hold the Sleep/Wake button until the Apple logo appears.
    From: iPhone, iPad, iPod touch: Turning off and on (restarting) and resetting
    http://support.apple.com/kb/ht1430        
    Take care,
    Sterling

  • Show itunes in the cloud purchases won't stay checked

    For months now, when I open iTunes the option "show itunes in the cloud purchases" won't stay checked.  I have to restart, sign out and sign back in and then the option only shows up half the time.  This is VERY frustrating.  What gives?!?

    I think I restored a very old system and must install a newer itunes. Not sure how to erase this.

  • My brushes won't work - they stay attached to the cursor and won't stay on the photo.  I have Photoshop Elements 13 and have had varous problems since installation.  Anyone have any ideas re my 'brushes'?

    my brushes won't work - they stay attached to the cursor and won't stay on the photo.  I have Photoshop Elements 13 and have had varous problems since installation.  Anyone have any ideas re my 'brushes'?

    <moved from Downloading, Installing, Setting Up to Photoshop Elements>

  • HT1572 the charging light won't stay on - I have to close and re-open the I Tunes app

    I have a new HP laptop. I don't know if it's a coincidence, but my charging light won't stay on. I have to keep closing and re-opening the I Tunes program. Is the battery shot or what?
    Thanks.

    Depending on how often you use your shuffle (how many charging cycles it's been through), a 2nd gen shuffle from 2008 could have a worn out battery.
    Battery charging is not dependent on iTunes running.  If you connect it to the laptop without iTunes running (or even installed), it should charge.  If you connected the USB cable to one of those USB power adapters, it should charge.  It just needs a USB 2.0 port with power.
    When you connect it and run iTunes, does the shuffle still appear in the iTunes sidebar?  Can you still sync it from empty to full?

  • The usb cable won't stay in my m515

    It won't stay in tight enough to charge the device.  Is there some way I can make it tighter or do I have to get a new cable?  Also how do I access the user manual.  It says it's in the software, but I can't find it.
    Post relates to: Palm m515

    If the cable won't stay connected, you will probably need to replace the cable.
    If you have the CD that came with the device, browse the CD and look in the English folder for the user guide.
    Another option is to go here for the user guide.
    http://kb.palm.com/wps/portal/kb/common/article/47831_en.html#m505
    The m505 handbook states it is for the m500 series. It should have the info. about your handheld.

  • The view options won't stay?

    I've used Mac OS for a very long time now and am at the least an intermediate user both in hardware and software but this is perplexing. For whatever reason, whenever a Finder window is opened (disk image just mounted, clicked the Finder icon, opened my documents) it always opens in Column View, even if I already set the folder to icon view. It doesn't seem to be saving the .DS_store file correctly or something, because if I close the window and open it again, it resets to column view. Every once in a while it will stay in icon view but it's rare.

    DBAqua----
    Whoa!
    http://hints.macworld.com/article.php?story=20050217012732914
    "....[robg adds: I would also recommend opening NetInfo Manager again and using Security: Disable Root User (after authenticating again) to disable root.]...."
    Since I got in trouble jumping in before I really knew what I was doing, tell me the difference between "Root User" and the one that locks the files so you
    can't access them from Disk Utility.
    I'm putting this in my "ToDo" file for a future time when I'm tuning/cleaning my
    files. Right now I have a work in progress.
    Thanks again DB, my indebtedness is compounding daily.
    ...........goldie

  • UGH! Help!New Mac owner and I'm about to throw it out the window! Won't stay connected wirelessly to the internet.

    I purchased a MacBook Pro 13":
    Processor  2.8 GHz Intel Core i7
    Memory  4 GB 1333 MHz DDR3
    Graphics  Intel HD Graphics 3000 384 MB
    Software  Mac OS X Lion 10.7.3 (11D50b)
    I have AT&T DSL with the 2wire wireless connected directly to the dsl line.  There is no base computer.  I owned a netbook prior to this and had several people use there pc laptops wirelessly before.  Now I can't seem to get my Mac to stay connected wirelessly.  My iPhone 4, touch and satalite still maintain perfect wireless connection as they did before. I called apple , they said it was my internet, I called AT&T they said it was the Mac.  I have a similar post on AT&T, hopefully someone can help!  This is NOT how I wanted my first Mac experience to go!!!  It is vital I stay connected because I remote in to work constantly and can not be interrupted.  Someone PLEASE help!!

    Recommended settings for Wi-Fi routers and access points
    Troubleshooting Wi-Fi issues
    Potential sources of wireless interference

  • The Status function won't stay on for my Universal Wireless Keyboard 3169WWZ

    I have worked on this for many hours with no success.  I think the problem is the Keyboard Status.  I have downloaded the update for this keyboard and it seems to be functioning except...They keyboard status will not stay in the On position.  I have to re-install the updated driver to the turn it on.  It will stay on for awhile but then turns on.  At other times when it is in the On position the keyboard still does not work but keyboard will at other times .  If I turn the Status to Off, I can not turn it back on again until I reset my Zire 31.  In general it behaves very irratically.  Any suggestions?  All of this is true whether the IR is selected, or not, in the Connections application.  I have soft reset the Zire on more than one occassion.  The Zire and keyboard are several years old and I haven't used them in two years.  The batteries in the keyboard are new.  The Zire is full charged and is fully functioning to include HotSync.  Thank you for our help.
    Post relates to: Zire 31

    Do you remember this issue before you let the device sit for two years? It could be that the keyboard is not working correctly due to the not being used for a long time. Before we get to replacement options, make sure that both IR ports are clean. I would use a damp not wet cloth on both ports to see if that helps clear up the issue.
    If it does not you can try a hard reset on the Zire but you may need to replace the keyboard and/or the device
    Post back if you have any more questions.

  • The system drive won't stay unlocked

    When attempting to use the OSX Snow Leopard install disk for the repair tools I press and hold the C key and unfortunately the disk ejects.????!!!!  I looked at the "Get Info" data and noticed that all of the drives including the system drive are locked.  So I unlocked the system drive and rebooted with the Snow Leopard Install Disk, held down the C key and again the disc ejected.  I looked at the "Get Info" data and the drive had relocked itself again.  What do I need to do to keep the drive unlocked.  I won't be able to access the Install Disc and repair the drive with the advanced tools unless I can get and keep the drive unlock for read and write purposes.  I might be wrong about the access problem and maybe the Install Disc is ejecting for some other reason but I still need to be able to unlock the drives for read and write purposes and after I "restarted" with the Install Disc the drive had relocked itself.  What gives...????

    Check this site for battery part numbers and sources.
    Mac PRAM, NVRAM, CUDA/PMU & Battery Tutorial
    http://www.geocities.com/texas_macman/pram.html
    Cheers, Tom

  • After updating to Firefox 5 on a Windows 7 PC the bookmarks icon won't stay on the toolbar and I can't click it to get a bookmarks column on the left side of the screen, how do I fix it?

    There was no bookmarks icon on the toolbar. I used View > Toolbars> customise to put various icons on the toolbar all of which were visible using "customise". On going back to Firefox all the other icons remained visible but the Bookmarks Icon disappeared leaving no means of getting a bookmarks column on the left of the screen when using Firefox.

    Check that you still have the "Bookmarks Toolbar items" placed on the Bookmarks Toolbar<br />
    If the menu bar is hidden then press F10 or hold down the Alt key, that should make the "Menu Bar" appear
    Make sure that toolbars like the "Navigation Toolbar" and the "Bookmarks Toolbar" are visible: "View > Toolbars"
    * Open the Customize window via "View > Toolbars > Customize" or via "Firefox > Options > Toolbar Layout" and check that the "Bookmarks Toolbar items" is on the Bookmarks Toolbar
    * If the "Bookmarks Toolbar items" is not on the Bookmarks Toolbar then drag it back from the toolbar palette in the customize window to the Bookmarks Toolbar
    * If you do not see the "Bookmarks Toolbar items" or other items that are missing in the toolbar palette or on a toolbar then click the "Restore Default Set" button
    Open the Customize window via "View > Toolbars > Customize" or via "Firefox > Options > Toolbar Layout"
    *http://kb.mozillazine.org/Toolbar_customization
    *https://support.mozilla.com/kb/Back+and+forward+or+other+toolbar+items+are+missing

  • HT1688 My phone is glitching all the time and won't stay on for more than 30 sec...

    Until a few days ago my phone was fine but then the screen turned kind of denim looking almost and slowly faded black and I would restart it but it continued to go through this proccess

    Could be faulty battery. Apple Store can perform free quick (minutes) battery diagnostic testing. Make Genius reservation to avoid long wait.

  • 13 Macbook Pro the ethernet cord won't stay plugged in.

    My ethernet plug will not clip in. I plugs in and it works, but it will just slide out on its own. It doesn't click in. I have tried different cables and it doesn't make any differences. I have look at the female part of the plug and it doesn't appear that their is anything wrong with it. Anyone have any suggestions?

    I have the same problem. I can verify that there is nothing wrong with the ethernet cable, as it "clips in" fine in 3 other computers that I own. Even the slightest movement dislodges the cable. I have tried several different cables, even buying an expensive cable with a premium jack, all to the same result. I believe this is a product defect, but as it's the only Macbook Pro that I own, I don't know if it's something with product design. This is a brand new 13" mid 2012 Macbook Pro.

Maybe you are looking for