There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
setting the ImageIcon size


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
Caliraj's Avatar
Senior Member with 145 posts.
 
Join Date: Mar 2006
Experience: Intermediate
08-Mar-2007, 04:13 AM #1
setting the ImageIcon size
Hi,
Im getting stuck in the way Im trying to set the size of the ImageIcon. Im trying to set a jpeg image as the button. The program is below:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*<applet code=jbt.class width=500 height=500>
</applet>*/
public class jbt extends JApplet implements ActionListener
{
Container c=getContentPane();
JLabel l=new JLabel("type");
JTextField t=new JTextField(10);
ImageIcon im1=new ImageIcon("yoga.jpeg");
JButton mb=new JButton(im1);
public int getIconWidth()
{
return 200;
}
public int getIconHeight()
{
return 300;
}
public void init()
{
c.setLayout(new FlowLayout());
c.add(l);
c.add(t);
mb.setIcon(im1);
mb.setSize(im1.getIconWidth(),im1.getIconHeight());

mb.setRolloverIcon(im1);
mb.setRolloverEnabled(true);
c.add(mb);
mb.addActionListener(this);
}
public void actionPerformed(ActionEvent a)
{
l.setText(t.getText());
}
}
Im not quite sure if this is the right way to set the size of the ImageIcon, because even if I give values as high as 1000, I dont get to see the image on the button.
Thanks,
Cali
Chicon's Avatar
Computer Specs
Distinguished Member with 6,553 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
10-Mar-2007, 12:39 PM #2
Hi caliraj,

First of all, resizing a component inside a FlowLayout is pratically impossible as the layout manager will put components sequentially in a row according their preferred size, either from left to right (default) or from top to bottom. In a FlowLayout, components are in a way interconnected.
Also, layout managers have their own set of rules that prevail on whatever method applied on a component. That means certain actions on the components can simply be ignored because they are violating one of the rules of the layout manager.

In your example, instead of implementing directly a variable size button, you may use a JPanel with a a size sufficiently large to contain a button. With the JPanel, you will have an independant container (nested container) in which you'll be able to resize your button according to the size of the image it will contains.

You should check the methods of the JButton : http://java.sun.com/javase/6/docs/ap...g/JButton.html.
__________________
for ( ; ; ) ;

Examinations time is coming, take a Java beta exam and get a belt !
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 01:58 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.