JButtons and Icons

Hi,
I'd like to know how you can display a background image on a JButton, so the button is completely filled by the imaged. I've tried icons but they will only display next to the text...I've also tried extending JButton but can't get it to work...I have to use a JButton or other lightweight component. Anyone know how this is done?
Regards,
Falk

Here's a simple example of extending JButton and rendering an Image into it.
-Brian
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JButton;
* A JButton that contains an image.
public class ImageButton extends JButton {
        private Image theImage;
         * Constructor.
         * @param anImage An instance of Image to be drawn in the button
        public ImageButton(Image anImage) {
                theImage = anImage;
         * Render the image on the button.
        public void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (theImage != null)
                      g.drawImage(theImage, 0, 0, null);
        public void setImage(Image anImage) {
                theImage = anImage;
        public Image getImage() {
                return theImage;
}

Similar Messages

  • JToolBar/JButton text/icon on and off

    Is there an easy way to turn on/off all text/icon display in all the buttons of a JToolBar in one shot? You know, I want to let the user choose to view the toolbar in 3 ways:
    1. icon only
    2. text only
    3. icon plus text
    I know I could iterate over each JButton and do
    jbutton.setText(null) or jbutton.setIcon(null), but this way I will have to store the original text/icon somewhere so I can easily restore them later on. This is cumbersome.

    Hi,
    try something like this :
    regards
    [email protected]
    //demonstrates use of the Action object
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    //swing classes
    import javax.swing.text.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.border.*;
    public class xtendMenu extends JxFrame {
    JPanel jp;
    JMenuItem menuitem;
    ActionToolBar toolbar;
    public xtendMenu() {
    super("Extended Menu");
    JMenuBar mbar = new JMenuBar(); //set up menu bar
    setJMenuBar(mbar);
    //Add File menu
    JMenu mFile = new JMenu("File");
    mbar.add(mFile);
    //create two Action Objects
    Action Open = new FileButton("Open", new ImageIcon("open.gif"), this);
    menuitem = mFile.add(Open);
    menuitem.setIcon(null);
    Action Exit = new ExitButton("Exit", new ImageIcon("exit.gif"));
    mFile.addSeparator();
    menuitem = mFile.add(Exit);
    menuitem.setIcon(null);
    //Exit.setEnabled(false); --used to test disable
    //now create toolbar that fixes up the buttons as you add them
    toolbar = new ActionToolBar();
    getContentPane().add(jp = new JPanel());
    jp.setLayout(new BorderLayout());
    jp.add("North", toolbar);
    //add the two action objects
    toolbar.add(Open, "Open a file");
    toolbar.addSeparator();
    toolbar.addSeparator();
    toolbar.addSeparator();
    toolbar.add(Exit, "Exit from program");
    setSize(300, 200);
    setVisible(true);
    static public void main(String argv[]) {
    new xtendMenu();
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    //swing classes
    import javax.swing.text.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.border.*;
    public abstract class ActionButton extends AbstractAction {
    Hashtable properties;
    public ActionButton(String caption, Icon img) {
    properties = new Hashtable();
    properties.put(DEFAULT, caption);
    properties.put(NAME, caption);
    properties.put(SHORT_DESCRIPTION, caption);
    properties.put(SMALL_ICON, img);
    public void putValue(String key, Object value) {
    properties.put(key, value);
    public Object getValue(String key) {
    return properties.get(key);
    public abstract void actionPerformed(ActionEvent e);
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    //swing classes
    import javax.swing.text.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.border.*;
    public class ExitButton extends ActionButton {
    JFrame fr;
    public ExitButton(String caption, Icon img) {
    super(caption, img);
    public void actionPerformed(ActionEvent e) {
    System.exit(0);
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    //swing classes
    import javax.swing.text.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.border.*;
    public class ActionToolBar extends JToolBar
    public ActionToolBar()
    setFloatable(false);
    public void add(Action act, String tip)
    JButton button = super.add(act);
    button.setText("");
    button.setToolTipText(tip);
    button.setMargin(new Insets(0,0,0,0));
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    //swing classes
    import javax.swing.text.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.border.*;
    public class FileButton extends ActionButton
    JFrame fr;
    public FileButton(String caption, Icon img, JFrame frm)
    super(caption, img);
    fr = frm;
    public void actionPerformed(ActionEvent e)
    FileDialog fdlg = new FileDialog(fr, "Open File", FileDialog.LOAD);
    //JFileChooser fdlg = new JFileChooser();
    //fdlg.showOpenDialog(fr);

  • JButton help, icon +text position

    Hi, is it possible to have JButton with icon on far left and text between the icon and far right hand side?

    Yo can do it by using the setHorizontalTextPosition() method.
    here is one example
    import java.awt.*;//Frame
    import javax.swing.*;
    public class Test extends JFrame
         public Test()//Constructer. Creates the frame
              JButton bt = new JButton("Done", new ImageIcon("image.gif"));
              bt.setHorizontalTextPosition(AbstractButton.LEFT);
              setBounds(300,300,300,300);
              getContentPane().setLayout(new FlowLayout());
              getContentPane().add(bt);
              setVisible(true);//shows on the screen
         public static void main(String[] args)
              new Test();//Creates a new Instance of our class
    }

  • JButton with Icon question

    In the Toolbar I have a JButtons. I created the JButtons with Icons.
    When I click on a button it is putting a dotted line right around the Icon. How can I get rid of that? Thanks.

    You're talking about the focus rectangle. If memory serves in the SwingSet demo there's an entire panel devoted to buttons and such. You can find something there to help you. The toggle in the example is labeled "Paint Focus", and it does what you want.

  • Can i paint a JButton with icon?

    i have jbutton with icon and when i push on him i want him to look like when he doesnt Enabled with gray color but i want him to be enable...
    How i do this?

    i dont want to add icon i want to paint the button in gray color
    like you take a jbutton with icon and you do setEnabled(False)
    Thanks

  • Enlarging Print and Icons

    I just loaded CS6 and the program came up with very small print for both words and icons.  How do I enlarge everything to normal size??

    You may want to try reducing the resolution of your monitor if you are having difficulty with the UI.  According to http://www.adobe.com/products/photoshop/tech-specs.html we require a minimum resolution of 1024 x 768.  Perhaps try adjusting your resolution settings to this setting and see if it is a bit easier for you to utilize.

  • Photoshop CC 2014 UI Menu Text And Icon Size?

    so i really like that adobe has added the "experimental" option to increase the ui menu text and icon size in photoshop cc 2014, it was almost unreadable on my 2560 x 1440 27" monitor with photoshop cs4 and illustrator cs4, but the lone 200% setting is too big... seems that 125%, 150%, or maybe even 175% would be much better solutions. in fact, for reasons i can't quite understand, the ui text and icon size in illustrator cc 2014 is spot on, absolutely perfect on my monitor, even tho there is no option for increased ui size as far as i can tell, it was spot on the moment i installed and launched it.
    in short, the photoshop cs4 and illustrator cs4 user interfaces were identical in size as far as i could tell, and far too small on my 2560 x 1440 27" monitor. in terms of cc 2014, the illustrator ui is perfect out of the box on my monitor, considerably larger than illustrator cs4, while the photoshop cc 2014 ui is identical to cs4, tiny, but can be enlarged 200% which makes the ui too big. why couldn't/didn't adobe just make the photoshop cc 2014 base ui the exact same size as the illustrator cc 2014 ui? it would have been perfect (for my monitor size).

    thanks... just wish illustrator cc 2014 wasn't so perfect in terms of ui size, i mean it's just great and, consequently, makes it seem as if the ball was dropped with photoshop cc 2014... i mean how can one be spot on while the other be so far off?

  • Since i have updated my iphone everytime i open an app it comes up with a message 'connect to itunes to use push notifications, may include alerts, sounds and icon badges, i have backed up my phone on itunes and restored phone but its still doing it, :(

    since i have updated my iphone everytime i open an app it comes up with a message 'connect to itunes to use push notifications, may include alerts, sounds and icon badges, i have backed up my phone on itunes and restored phone but its still doing it  can anyone help?
    Em

    Return the iPhone and get your money back. It has been
    hacked/modified/jailbroken and likely cannot be made
    operative.

  • Push Notifications. The Message "Connect to iTunes to Use Push Notifications "BBC History Magazine" notifications may include alerts, sounds and icon badges" keeps coming up in Newstand, also happens with National Geographic Magazine. HELP!

    The Message "Connect to iTunes to Use Push Notifications "BBC History Magazine" notifications may include alerts, sounds and icon badges" keeps coming up in Newstand, also happens with National Geographic Magazine. HELP!
    I have now followed multiple instructions from this and other forums. have turned push notifications off. Turned them on. Have updated to IOS 6. Have signed out and signed back in, have uninstalled all magazines and reinstalled. Have synced, and updated everything in iTunes.
    Nothing works,. Also cannot use youtube.

    Basic troubleshooting from the User's Guide is reset, restart, restore (first from backup then as new).  Try each of these in order until the issue is resolved.
    It might also be a good idea to contact Square for assistance.

  • Photoshop CS6 Menus and Icons too small on Windows 8.1 with 4K UHD display

    Hello,
    So I have seen that I am not the first one to ask this question, but I have yet to find any true resolutions.  I recently bought anew laptop, a Lenovo with a 4K UHD display which I already love for so many things.  But, like a few other programs, the high resolution means the icons and menu text of PS CS6 are incredibly small, which may be alright on a TV, but on a laptop screen, it make it incredibly difficult to work.  I have tried all sorts of different approaches in Windows, such as changing the OS font sizes and icon sizes, which do indeed change everything else on the computer, but PS is still the same tiny size.  I have also increased the text size under the interface preferences to large, but with little real change.  I understand why this is happening, but I am hoping that there is some sort of workaround, beyond just bumping down my screen resolution, which defeats the whole purpose of using UHD to edit photos.  I have heard rumors of being able to code in some UI changes manually, but have found no concrete examples of how to actually do that. 
    I am sure that it would not take Adobe too much time and effort to create an update to help with scaling, but they are very set on pushing forward with Creative Clound, leaving the rest of us loyal customers to the dust until we pay up.  If anyone has found any resolutions or workarounds, I'd really appreciate it.  Thank you. 

    Well other than that, I can't offer anything else as a forum volunteer. I have no idea if anything, what Adobe plans to do for CS6.
    I do know that Photoshop CC 2014 has a 200% ui feature for Windows and you might like the Photography Plan which for $10 monthly includes Photoshop CC 2014 and Lightroom for $10/month.
    You can trial CC 2014 to see if it does solve your situation
    Gene

  • I have a mac pro g4 when i load a cd or dvd there is no start up noise from drive and icon will not show on desktop or in itunes. How should i troubleshoot? pioneer 105 mirror door

    I have a mac pro g4 when i load a cd or dvd there is no start up noise from drive and icon will not show on desktop or in itunes. How should i troubleshoot? pioneer 105 mirror door

    PIONEER DVD-RW  DVR-105:
      Firmware Revision:          A506
      Interconnect:          ATAPI
      Burn Support:          Yes (Apple Shipping Drive)
      Cache:          2000 KB
      Reads DVD:          Yes
      CD-Write:          -R, -RW
      DVD-Write:          -R, -RW
      Write Strategies:          CD-TAO, CD-SAO, CD-Raw, DVD-DAO
      Media:          Insert media and refresh to show available burn speeds
    yes its a power mac thanks its been a long month and Merry Christmas thanks for checking my question im hoping its a driver problem but not liking some other stuff looking like replace drive

  • Windows updates gave me version of Firefox that does not allow me to have Text and Icon in Menu Bar, lost my autofill; how do I get old version back?

    Windows updates this morning gave me a different firefox: the Customize box includes fewer choices. No Text and Icon, which I want. Also, this new version dropped my Autofill.

    It wasn't Windows update that caused this, but Firefox automatic updates. It's the new Firefox 29 Australis, their attempt to look more like Chrome. They dramatically changed the UI, removing several features in the process. If the way it looks bothers you as much as it does others you can install Firefox 28 again by going here;
    https://ftp.mozilla.org/pub/mozilla.org/firefox/releases/28.0/win32/en-US/
    That of course does come with the risk of security exploits they fixed with 29, so beware of that. I also don't know if it will repair your auto fill or anything.
    Otherwise if you don't mind the new look, keep using it.

  • Windows and icons are black, menu items are not showing correctly

    Hello!
    Well just doing my normal routine today browsing the Internet, watching videos on vlc, my computer suddenly started to glitch. Icons were disappearing, Safari turned black.
    So i did a restart but the problem is still there. I cant even choose the user properly because the avatars are not there it's just all dark gray. Remembering where my user avatar is, i do a click and type in my password and I do and able to get in to my system but all windows menu items and icons are black.
    Basically what I'm saying is the system seems to be working fine except for that I can't see the icons and windows properly displayed.
    I've already tried to reinstall of Mavericks hoping that it's a software thing. You guys think it's a problem with the graphic processor?
    Any help or suggestions will be appreciated thank you.

    Please read this whole message before doing anything.
    This procedure is a diagnostic test. It changes nothing, and therefore will not, in itself, solve your problem.
    Third-party system modifications are a common cause of usability problems. By a “system modification,” I mean software that affects the operation of other software — potentially for the worse. The procedure will help identify which such modifications you've installed, as well as certain other aspects of the configuration that may have a bearing on the problem. Don’t be alarmed by the apparent complexity of these instructions — they’re easy to carry out and won’t change anything on your Mac. 
    These steps are to be taken while booted in “normal” mode, not in safe mode, if possible. If you’re now running in safe mode, reboot as usual before continuing. If you can only boot in safe mode, you can still use this procedure, but not all of it will work. Be sure to mention that in your reply, if you haven't already done so. 
    Below are instructions to enter UNIX shell commands. The commands are safe and do nothing but produce human-readable text output, but they must be entered exactly as given in order to work. If you have doubts about the safety of the procedure suggested here, search this site for other discussions in which it’s been followed without any report of ill effects. I am not asking you to trust me. If you can't satisfy yourself that these instructions are safe, don't follow them.
    The commands will line-wrap or scroll in your browser, but each one is really just a single long line, all of which must be selected. You can accomplish this easily by triple-clicking anywhere in the line. The whole line will highlight, and you can then copy it.
    Note: If you have more than one user account, Step 2 must be taken as an administrator. Ordinarily that would be the user created automatically when you booted the system for the first time. Step 1 should be taken as the user who has the problem, if different. Most personal Macs have only one user, and in that case this paragraph doesn’t apply.
    Launch the Terminal application in any of the following ways: 
    ☞ Enter the first few letters of its name into a Spotlight search. Select it in the results (it should be at the top.) 
    ☞ In the Finder, select Go ▹ Utilities from the menu bar, or press the key combination shift-command-U. The application is in the folder that opens. 
    ☞ Open LaunchPad. Click Utilities, then Terminal in the icon grid. 
    When you launch Terminal, a text window will open with a line already in it, ending either in a dollar sign (“$”) or a percent sign (“%”). If you get the percent sign, enter “sh” and press return. You should then get a new line ending in a dollar sign. 
    Step 1 
    Triple-click anywhere in the line of text below on this page to select it:
    { echo "Loaded kernel extensions:"; kextstat -kl | awk '!/com\.apple/{printf "%s %s\n", $6, $7}'; echo $'\n'"Loaded user agents:"; launchctl list | sed 1d | awk '!/0x|com\.apple|org\.(x|openbsd)|\.[0-9]+$/{print $3}'; echo $'\n'"Inserted libraries:"; launchctl getenv DYLD_INSERT_LIBRARIES; echo $'\n'"User cron tasks:"; crontab -l; echo $'\n'"System launchd configuration:"; cat /e*/lau*; echo $'\n'"User launchd configuration:"; cat .lau*; echo $'\n'"Login items:"; osascript -e 'tell application "System Events" to get name of login items' | sed $'s/, /\\\n/g'; echo $'\n'"Safari extensions:"; /usr/libexec/PlistBuddy -c Print L*/Saf*/*/E*.plist | awk -F'= ' '/Bundl/{print $2}' | sed 's/\..*$//;s/-[1-9]$//'; printf "\nRestricted user files: %s\n" $(find ~ $TMPDIR.. \( -flags +sappnd,schg,uappnd,uchg -o ! -user $UID -o ! -perm -600 \) | wc -l); echo $'\n'"Extrinsic loadable bundles:"; cd; find -L /S*/L*/E* {,/}L*/{Ad,Compon,Ex,In,Keyb,Mail/Bu,P*P,Qu,Scripti,Servi,Spo}* -type d -name Contents -prune | while read d; do /usr/libexec/PlistBuddy -c 'Print :CFBundleIdentifier' "$d/Info.plist" | egrep -qv "^com\.apple\.[^x]|Accusys|ArcMSR|ATTO|HDPro|HighPoint|driver\.stex|hp-fax|JMicron|print|SoftRAID" && echo ${d%/Contents}; done; echo $'\n'"Unsigned shared libraries:"; find /u*/{,*/}lib -type f -exec sh -c 'file -b $1 | grep -qw shared && ! codesign -v $1' {} {} \; -print; echo; ls -A {,/}L*/{La,Priv,Sta}* L*/Fonts; } 2> /dev/null | open -ef
    Copy the selected text to the Clipboard by pressing the key combination command-C. Then click anywhere in the Terminal window and paste (command-V). I've tested these instructions only with the Safari web browser. If you use another browser, you may have to press the return key after pasting.
    The command may take up to a few minutes to run, depending on how many files you have and the speed of the computer. A TextEdit window will open with the output. Post the contents of the TextEdit window (not the Terminal window) — the text, please, not a screenshot. You can then close the TextEdit window. The title of the window doesn't matter, and you don't need to post that. No typing is involved in this step.
    Step 2 
    Remember that you must be logged in as an administrator for this step. Do as in Step 1 with this line:
    { echo "Loaded system agents:"; sudo launchctl list | sed 1d | awk '!/0x|com\.(apple|openssh|vix\.cron)|org\.(amav|apac|cups|isc|ntp|postf|x)/{print $3}'; echo $'\n'"Login hook:"; sudo defaults read com.apple.loginwindow LoginHook; echo $'\n'"Root cron tasks:"; sudo crontab -l; echo $'\n'"Log check:"; syslog -k Sender kernel -k Message CReq 'GPU |hfs: Ru|I/O e|find tok|n Cause: -|NVDA\(|pagin|timed? ?o' | tail | awk '/:/{$4=""; print}'; } 2> /dev/null | open -ef
    This time you'll be prompted for your login password, which you do have to type. Nothing will be displayed when you type it. Type it carefully and then press return. You may get a one-time warning to be careful. Heed that warning, but don't post it. If you see a message that your username "is not in the sudoers file," then you're not logged in as an administrator.
    You can then quit Terminal.
    To prevent confusion, I'll repeat: When you type your password in the Terminal window, you won't see what you're typing.
    Note: If you don’t have a login password, set one before taking Step 2. If that’s not possible, skip the step.
    Important: If any personal information, such as your name or email address, appears in the output of these commands, anonymize it before posting. Usually that won't be necessary.
    Remember, Steps 1 and 2 are all copy-and-paste — no typing, except your password. Also remember to post the output as text, not as a screenshot.
    Please post the contents of the TextEdit window, not the Terminal window.

  • How do you adjust the font size for the tabs and icons around the screen?

    How do  you adjust the font size of tabs and and icons to ake them larger?

    sfcalling
    Click on the text in the menu so that you see the bounding box around it. Look to the right of the Movie Menu customization workspace, Adjustments Palette, Text and edit your text font style and size.
    In this screenshot, "Glass Flowers" text in the main menu is selected and its Adjustments Palette Text editing opportunities are shown. To do comparable editing in the scene menu, click on the scene menu in the bin to bring its image up to the screen.
    Please let us know if you are OK with the details.
    Please also review the following Adobe document on disc menus which applies to both version 11 and 12.
    Adobe Premiere Elements 11 * Creating disc menus
    By the way, what computer operating system is your Premiere Elements 12 running on?
    Thanks.
    ATR

  • Problem with my iphone 4s. screen and icons shaking. ios 5.1. please help thanks apple.

    good day apple. can you help me with my iphone 4s 5.1. SCREEN AND ICONS ARE SHAKING. what am going to do. thanks

    okay. I tried the airplane mode and do not disturb on and off thing, resetting the network settings and all settings, restoring the iPhone using iTunes, draining phone using flashlight, reheating the phone using hair dryer and freezing the phone.. the freezing makes my wifi work again but it only last a couple of hours and I didn't want to try it anymore because I'm afraid it may cause another problem to my phone if I keep doing that.

Maybe you are looking for

  • Spring and TopLink/JPA on OC4J 10.1.3.4.0

    Our configuration worked fine on OC4J 10.1.3.1 (linux) and 10.1.3.3 (windows). We have a problem with JPA (toplink essentials--not eclipselink) on OC4J 10.1.3.4.0 using Spring (2.0.7 and 2.5.6). We found that it was trying to connect to the default O

  • New MBP won't print with Wifi HP All-in-one that it cam with from Apple

    Hey, all just got a new MBP and the free HP all in one wireless printer. After installing all the software and drivers I managed to bonjour it through our router or direct through USB. Same on my iMAc However, Wifi will hang for a few minutes , then

  • In-process inspection in repetitive mfg-  Inspection type - 13

    Hi there,              Can any one please tell me what is the differrence between the inspection type -03 & 13. As per my understanding the only diff is that 03 is used in discrete mfg & 13 in repetitive mfg... what is the other differrence ........?

  • How to get the real name of a sender ?

    When I receive a message under JavaMail, I want to get the real name of the sender but when I do a getFrom() I received a string like : =?iso-8859-1?q?Julien=20Dupont?= <[email protected]> But I only want the real name... How can I do to recover only

  • Hyper V 2012 Backup - best practice

    I'm setting up a new Hyper-V server 2012 and I'm trying to understand how to best configure it for backups and restores using Windows Server Backup. I´ve 5 VMs running, everyone has their one iScsi drive. I would like to backup the VMs on a external