Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2006

Tip: Looking for answers? Try searching our database.

Setting an icon on a JButton

Thread view: 
dushkin - 25 Dec 2006 16:29 GMT
Hello there,

Please review the following code segment....

/*************************************************************/
public class IMIEasternButtonPanel extends JPanel {

    private BoxLayout bl = new BoxLayout(this, BoxLayout.LINE_AXIS);
    private JButton m_btnUp, m_btnDown;

    public IMIEasternButtonPanel(){
        setLayout(bl);
        setPreferredSize(new Dimension(500,50));
        addChilds();
    }

    public void addChilds(){

        boolean b = FilesUtils.isFileExists("up.gif");  //b = true !!!
        Icon upIcon = null;
        upIcon = new ImageIcon("up.gif");
        m_btnUp = new JButton();
        m_btnUp.setIcon(upIcon);
        m_btnUp.setSize(50, 50);
        add(m_btnUp);
/*******************************************************/

Why can't I see the icon on the up button??? Moreover, the button does
not seems to get the correct size as I had set. It is more like a
oblong than a 50X50 square...

Many thanks!
hiwa - 26 Dec 2006 04:59 GMT
> Hello there,
>
[quoted text clipped - 28 lines]
>
> Many thanks!
You could use a simple JPanel instead of extendint it.
What's the point in extendint it?

Try this:
-------------------------------------------------------------------------------------
import javax.swing.*;
import java.awt.*;

public class IMIEasternButtonPanel extends JPanel {
 private JButton m_btnUp, m_btnDown;

 public IMIEasternButtonPanel(){
   setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
   setPreferredSize(new Dimension(500,50));
   addChilds();
 }

 public void addChilds(){
   m_btnUp = new JButton(new ImageIcon("up.gif"));
   m_btnUp.setMinimumSize(new Dimension(50, 50));
   m_btnUp.setMaximumSize(new Dimension(50, 50));
   m_btnUp.setPreferredSize(new Dimension(50, 50));
   add(m_btnUp);
 }

 public static void main(String[] args){
   JFrame frame = new JFrame();
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   IMIEasternButtonPanel iebp = new IMIEasternButtonPanel();
   frame.getContentPane().add(iebp, BorderLayout.EAST);

   frame.setSize(500, 500);
   frame.setVisible(true);
 }
}
dushkin - 27 Dec 2006 11:01 GMT
Hiwa, thank you for your kind help. It worked.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.