Resizing JButtons

Here's the dilema- I'm trying to make an applet with a lot of buttons in each row, but I don't want the applet to stretch the width of the screen. Some of the buttons don't have anything on them but a "-" or a single digit number. How can I properly size these buttons down so that they don't take up as much space as the larger buttons?
Here's what I'm dealing with:
public class inputscreen extends JApplet {
  upstat x;
  String fname;
  String lname;
  String ppg, xft, xfta, home, pos, cl, ht, wt;
  Connection con = null;
  Statement st = null;
  ResultSet rs = null;
  int vfg, vfga, vt3, vt3a, vft, vfta, vrb, vas, vf, vtot;
  String updateString;
  Container cp = getContentPane();
  JTextField ply = new JTextField(3);
  JButton upd = new JButton("UD");
  String sfg;
  JButton fg = new JButton(sfg);
  JButton fgm = new JButton("-");
  String sfga;
  JButton fga = new JButton(sfga);
  JButton fgam = new JButton("-");
  String st3;
  JButton t3 = new JButton(st3);
  JButton t3m = new JButton("-");
  String st3a;
  JButton t3a = new JButton(st3a);
  JButton t3am = new JButton("-");
  String sft;
  JButton ft = new JButton(sft);
  JButton ftm = new JButton("-");
  String sfta;
  JButton fta = new JButton(sfta);
  JButton ftam = new JButton("-");
  String srb;
  JButton rb = new JButton(srb);
  JButton rbm = new JButton("-");
  String sas;
  JButton as = new JButton(sas);
  JButton asm = new JButton("-");
  String sf;
  JButton f = new JButton(sf);
  JButton fm = new JButton("-");
  String stot;
  JTextField tot = new JTextField(4);
  JButton player = new JButton("Player");
  JButton season = new JButton("Season");
  JButton tonight = new JButton("Tonight");
  public void init() {
    JPanel line = new JPanel();
    line.add(ply);
    line.add(upd);
    line.add(fg);
    line.add(fgm);
    line.add(fga);
    line.add(fgam);
    line.add(t3);
    line.add(t3m);
    line.add(t3a);
    line.add(t3am);
    line.add(ft);
    line.add(ftm);
    line.add(fta);
    line.add(ftam);
    line.add(rb);
    line.add(rbm);
    line.add(as);
    line.add(asm);
    line.add(f);
    line.add(fm);
    line.add(tot);
    JPanel lower = new JPanel();
    lower.add(player);
    lower.add(season);
    lower.add(tonight);
    line.setLayout(new GridLayout(2, 21));
    lower.setLayout(new FlowLayout());
    cp.setLayout(new BorderLayout());
    cp.add(line, BorderLayout.NORTH);
    cp.add(lower, BorderLayout.CENTER);
    upd.addActionListener(new ButtonListener());
    fg.addActionListener(new ButtonListener());
    fgm.addActionListener(new ButtonListener());
    fga.addActionListener(new ButtonListener());
    fgam.addActionListener(new ButtonListener());
    t3.addActionListener(new ButtonListener());
    t3m.addActionListener(new ButtonListener());
    t3a.addActionListener(new ButtonListener());
    t3am.addActionListener(new ButtonListener());
    ft.addActionListener(new ButtonListener());
    ftm.addActionListener(new ButtonListener());
    fta.addActionListener(new ButtonListener());
    ftam.addActionListener(new ButtonListener());
    rb.addActionListener(new ButtonListener());
    rbm.addActionListener(new ButtonListener());
    as.addActionListener(new ButtonListener());
    asm.addActionListener(new ButtonListener());
    f.addActionListener(new ButtonListener());
    fm.addActionListener(new ButtonListener());
    player.addActionListener(new ButtonListener());
    season.addActionListener(new ButtonListener());
    tonight.addActionListener(new ButtonListener());
  }I'm trying to figure out JButton.setPreferredSize(new Dimension(x,y)); with no success. If someone could give me an example of how to implement that or another technique of resizing individual buttons, I'd really appreciate it.

The post by camickr is correct: Java components are sized by the layout manager managing the container in which they reside. Simply, GridLayout attempts to size every component the same -- your precise problem. If you use a GridBagLayout instead, the buttons will all respect their preferred size. See the given link.
- Steev.

Similar Messages

  • How to Resize JButton,JTextArea and JTextFields on Mouse Over....

    Hi All...
    Hi friends.. I want to be implement my java swing application Resize the Width and Height wise of JButton,JLabel,JTextFields on mouse over etc.
    ============================
    Arjun Palanichamy.
    Senior Programmer.
    Chennai-India.

    Hi All...
    I am herewith enclosed my sample code for resize the JButton(),But This one resize the when will be extend the Button Caption letters,But i want mouse click resize the without button caption or with caption.
    sample code :
    package Resize;
    import java.awt.Container;
    import java.awt.FlowLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import java.awt.Dimension;
    import java.awt.ComponentOrientation;
    public class ReButton{
    public static boolean RIGHT_TO_LEFT = false;
    public static void addComponents(Container contentPane) {
    if (RIGHT_TO_LEFT) {
    contentPane.setComponentOrientation(
    ComponentOrientation.RIGHT_TO_LEFT);
    contentPane.setLayout(new FlowLayout());
    contentPane.add(new JButton("1"));
    contentPane.add(new JButton("100"));
    contentPane.add(new JButton("100 100"));
    contentPane.add(new JButton("100 100 100"));
    contentPane.add(new JButton("100 100 100 100"));
    contentPane.add(new JButton("5"));
    private static void createAndShowGUI() {
    JFrame frame = new JFrame("FlowLayoutDemo") {
    public Dimension getMinimumSize() {
    Dimension prefSize = getPreferredSize();
    return new Dimension(100, prefSize.height);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //Set up the content pane.
    addComponents(frame.getContentPane());
    //Display the window.
    frame.pack();
    frame.setVisible(true);
    public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
    public void run() {
    createAndShowGUI();
    for example can you Click this.....
    http://www.eclipse.org/vep/WebContent/docs/testcases/null-layout/null-layout.htm
    Thanks for your time.
    Arjun Palanichamy.
    Chennai-India.

  • Problems resizing JButtons

    Hi. I have a GUI with a JPanel to which I have added 43 JButtons. I use a for loop to add the buttons but I cannot set the size of individual buttons. I want two of the buttons to be bigger than the rest. Can anyone please help me

    Hi. I have a GUI with a JPanel to which I have added
    43 JButtons. I use a for loop to add the buttons but I
    cannot set the size of individual buttons. I want two
    of the buttons to be bigger than the rest. Can anyone
    please help meAs some else has mentioned, much depends on your
    layout manager. You be able to do the job with
    GridBagLayout, but if that's too much like hard
    work, you can always turn the panel's layout manager off
    and manually place & size each of your buttons.
    setLayout( null );
    and for each button:
    button.setBounds( x, y, width, height );
    If you do this, the buttons won't move or expand
    when the panel is resized. I reckon that's not
    a bad thing for buttons anyway.

  • Resize JButtons Using FlowLayout ?

    I'm using some images for my Jbuttons and when I use FlowLayout, it sets the buttons dimensions bigger than the images and I can't figure out how to set the dimensions while having FlowLayout set too. They look fine when I use absolute positioning, but I need the flexibility of FlowLayout for different resolutions, etc. Anyonw know how to set the dimensions of a JButton while having FlowLayout set too? Thanks so much!

    As far as I know, FlowLayout doesn't adjust the size of
    components at all. Can you post a short, self-contained,
    compileable example that shows the offending behavour?
    You should be able to do new JButton(myIcon); or
    myButton.setIcon(myIcon); and have it Just Work. The button
    should resize itself to contain the icon.

  • Resizing JButtons in a BorderLayout

    Hi, I've been working with a BorderLayout and whenever I try and set a JButton's size or preferredSize, it always ignores what I set it at and puts it's size as filling the entire BorderLayout.CENTER. Any ideas?

    {color:#507C9A}1. In future, post Swing questions in the Swing forum: http://forum.java.sun.com/forum.jspa?forumID=57
    2. The behaviour of a Borderlayout is to ignore a component's preferredSize and stretch it to fill the centre.
    Solution: nest.
    JPanel withBorderLayout = ...
    JButton btn = ...
    //withBorderLayout .add(btn, BorderLayout.CENTER);
    JPanel centrePanel = new JPanel(); //FlowLayout by default, you may want to change this
    centrePanel .add(btn);
    withBorderLayout .add(centrePanel , BorderLayout.CENTER);{color}

  • Resizing JButton on JPanel?

    Hi all,
    I just started to learn to use swing and I ran into problem when I try to change the size of a JButton on JPanel. The following code make sense to me, but it just doesn't work. Am I missing something? Can anyone give me some directions?
    import javax.swing.*;
    public class TestFrame extends JFrame
         public static void main(String[] args)
              TestFrame myFrame = new TestFrame();
              myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              myFrame.setVisible(true);
         private JPanel myPanel = new JPanel();
         private JButton myBtn = new JButton("Test");
         public TestFrame()
              setSize(300, 300);
              myBtn.setSize(200, 200); // the button size still does not change
              myPanel.add(myBtn);     
              getContentPane().add(myPanel);          
    }

    use setPreferredSize instead of setSize
    import javax.swing.*;
    import java.awt.Dimension;
    public class TestFrame extends JFrame
         public static void main(String[] args)
              TestFrame myFrame = new TestFrame();
              myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              myFrame.setVisible(true);
         private JPanel myPanel = new JPanel();
         private JButton myBtn = new JButton("Test");
         public TestFrame()
              setSize(300, 300);
              //myBtn.setSize(200, 200); // the button size still does not change
              myBtn.setPreferredSize(new Dimension(200, 200));
              myPanel.add(myBtn);     
              getContentPane().add(myPanel);          
    }

  • How to resize data in JTable?

    Hi all,
    I wish to provide a resize button on an application which will resize the data in a table (in most cases just text) as well as the table containing it. Is it possible to directly resize the table or must I change the renderer to draw all of the data bigger? I'm also not sure how to increase the width of the table. Any help would be much appreciated.
    package gui;
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.table.DefaultTableModel;
    public class Resizer extends JFrame
         * @see java.io.Serializable
        public static final long serialVersionUID = 0;
         * Table containing data
        private JTable table = null;
         * Scroll pane containing table
        private JScrollPane scroller = null;
         * Name of button to increase text size
        private static final String INCREASE = "Increase";
         * Name of button to decrease text size
        private static final String DECREASE = "Decrease";
         * An increment to use when resizing
        private static final int INCREMENT = 5;
        public Resizer()
            setTitle("Resizer");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(300,200);
            getContentPane().setLayout(new BorderLayout());
            // Add the table and its contents
            table = createTable();
            scroller = new JScrollPane(table);
            getContentPane().add(scroller, BorderLayout.CENTER);
            // Add buttons to resize
            JButton increase = new JButton(INCREASE);
            increase.addActionListener(new ButtonListener());
            JButton decrease = new JButton(DECREASE);
            decrease.addActionListener(new ButtonListener());
            JPanel panel = new JPanel();
            panel.add(increase);
            panel.add(decrease);
            getContentPane().add(panel, BorderLayout.SOUTH);
            // Show this window
            setLocationRelativeTo(null);
            setVisible(true);
        private JTable createTable()
            JTable t = new JTable();
            t.setModel(new MyModel());
            // Row 1
            t.getModel().setValueAt("John", 0, 0);
            t.getModel().setValueAt(new Integer(1), 0, 1);
            // Row 2
            t.getModel().setValueAt("Chris", 1, 0);
            t.getModel().setValueAt(new Integer(2), 1, 1);
            // Row 3
            t.getModel().setValueAt("Susan", 2, 0);
            t.getModel().setValueAt(new Integer(3), 2, 1);
            return t;
        private class MyModel extends DefaultTableModel
            private static final long serialVersionUID = 2;
            public MyModel()
                this.setColumnCount(2);
                this.setRowCount(3);
            public String getColumnName(int col)
                switch(col)
                case 0:
                    return "Name";
                case 1:
                    return "Number";
                default:
                    return "Oops";
        private class ButtonListener implements ActionListener
            public void actionPerformed(ActionEvent ae)
                    if (((JButton)ae.getSource()).getText().equals(INCREASE))
                        increaseTable();  
                    else if (((JButton)ae.getSource()).getText().equals(DECREASE))
                        decreaseTable();
        private void increaseTable()
            this.setSize(this.getWidth()+INCREMENT, this.getHeight()+INCREMENT);
            table.setRowHeight(table.getRowHeight()+INCREMENT);
            table.setSize(table.getWidth()+INCREMENT, table.getHeight()+INCREMENT);
        private void decreaseTable()
            this.setSize(this.getWidth()-INCREMENT, this.getHeight()-INCREMENT);
            table.setRowHeight(table.getRowHeight()-INCREMENT);
            table.setSize(table.getWidth()-INCREMENT, table.getHeight()-INCREMENT);
        public static void main(String[] args)
            SwingUtilities.invokeLater(new Runnable()
                public void run()
                    new Resizer();
    }Using JDK 1.4.2

    Thanks all.
    The solution was to use setFont() on the table and its headers. Here's what I put in to increase and descrease the table size (for future reference).
         * Increases the size of everything in the table
        private void increaseTable()
            table.setRowHeight(table.getRowHeight() + INCREMENT);
            table.setSize(table.getWidth() + INCREMENT, table.getHeight() + INCREMENT);
            for (int i = 0; i < table.getColumnCount(); i++)
                TableColumn col = table.getColumnModel().getColumn(i);
                col.setWidth(col.getWidth() + INCREMENT / table.getColumnCount());
            Font headerFont = table.getTableHeader().getFont();
            table.getTableHeader().setFont(headerFont.deriveFont(headerFont.getSize2D()+INCREMENT));
            Font dataFont = table.getFont();
            table.setFont(dataFont.deriveFont(dataFont.getSize2D() + INCREMENT));
         * Decreases the size of everything in the table
        private void decreaseTable()
            if (table.getRowHeight() > INCREMENT)
                table.setRowHeight(table.getRowHeight() - INCREMENT);
                table.setSize(table.getWidth() - INCREMENT, table.getHeight() - INCREMENT);
                for (int i = 0; i < table.getColumnCount(); i++)
                    TableColumn col = table.getColumnModel().getColumn(i);
                    col.setWidth(col.getWidth() - INCREMENT / table.getColumnCount());
                Font headerFont = table.getTableHeader().getFont();
                table.getTableHeader().setFont(headerFont.deriveFont(headerFont.getSize2D()-INCREMENT));
                Font dataFont = table.getFont();
                table.setFont(dataFont.deriveFont(dataFont.getSize2D() - INCREMENT));
        }

  • Resized animated gif ImageIcon not working properly with JButton etc.

    The problem is that when I resize an ImageIcon representing an animated gif and place it on a Jbutton, JToggelButton or JLabel, in some cases the image does not show or does not animate. More precicely, depending on the specific image file, the image shows always, most of the time or sometimes. Images which are susceptible to not showing often do not animate when showing. Moving over or clicking with the mouse on the AbstractButton instance while the frame is supposed to updated causes the image to disappear (even when viewing the non-animating image that sometimes appears). No errors are thrown.
    Here some example code: (compiled with Java 6.0 compliance)
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Test
         public static void main(String[] args)
              new Test();
         static final int  IMAGES        = 3;
         JButton[]           buttons       = new JButton[IMAGES];
         JButton             toggleButton  = new JButton("Toggle scaling");
         boolean            doScale       = true;
         public Test()
              JFrame f = new JFrame();
              f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              JPanel p = new JPanel(new GridLayout(1, IMAGES));
              for (int i = 0; i < IMAGES; i++)
                   p.add(this.buttons[i] = new JButton());
              f.add(p, BorderLayout.CENTER);
              f.add(this.toggleButton, BorderLayout.SOUTH);
              this.toggleButton.addActionListener(new ActionListener() {
                   @Override
                   public void actionPerformed(ActionEvent e)
                        Test.this.refresh();
              f.setSize(600, 300);
              f.setVisible(true);
              this.refresh();
         public void refresh()
              this.doScale = !this.doScale;
              for (int i = 0; i < IMAGES; i++)
                   ImageIcon image = new ImageIcon(i + ".gif");
                   if (this.doScale)
                        image = new ImageIcon(image.getImage().getScaledInstance(180, 180, Image.SCALE_AREA_AVERAGING));
                   image.setImageObserver(this.buttons);
                   this.buttons[i].setIcon(image);
                   this.buttons[i].setSelectedIcon(image);
                   this.buttons[i].setDisabledIcon(image);
                   this.buttons[i].setDisabledSelectedIcon(image);
                   this.buttons[i].setRolloverIcon(image);
                   this.buttons[i].setRolloverSelectedIcon(image);
                   this.buttons[i].setPressedIcon(image);
    Download the gif images here:
    http://djmadz.com/zombie/0.gif
    http://djmadz.com/zombie/1.gif
    http://djmadz.com/zombie/2.gif
    When you press the "Toggle scaling"button it switches between unresized (properly working) and resized instances of three of my gif images. Notice that the left image almost never appears, the middle image always, and the right image most of the time. The right image seems to (almost) never animate. When you click on the left image (when visble) it disappears only when the backend is updating the animation (between those two frames with a long delay)
    Why are the original ImageIcon and the resized ImageIcon behaving differently? Are they differently organized internally?
    Is there any chance my way of resizing might be wrong?

    It does work, however I refuse to use SCALE_REPLICATE for my application because resizing images is butt ugly, whether scaling up or down. Could there be a clue in the rescaling implementations, which I can override?
    Maybe is there a way that I can check if an image is a multi-frame animation and set the scaling algorithm accordingly?
    Message was edited by:
    Zom-B

  • Dynamic resize of JButton and JFrame in response to Font

    Im supposed to increase the font size of the text with JButton by 1 within each click.
    Eventually the text becomes shorter and less visible like WORD becomes WOR... then WO... etc
    How to make the button always resize with text so that the text is fully visible and JFrame always resize with button so that the button doesn't change its position within the Frame ?
    edit:
    I managed to make buttons resize with increasing font
    by making
    JButton b = new JButton("button");
    b.setHorizontalTextPosition(CENTER);
    b.setVerticalTextPosition(CENTER);However the window size doesnt increase with components ;[
    any help would be appretiated.
    Edited by: pimpcane on Dec 11, 2007 12:16 PM

    Ok I managed to get it working moreless by
    adding pack() to the actionPerformed(...) function
    public void actionPerformed(ActionEvent e)
                 int index = Integer.parseInt(e.getActionCommand());
              int size = buttons[index].getFont().getSize();
              size++;
              buttons[index].setFont(new Font(name, style, size));
              pack();
    }The problem is in the task im given it is forbidden to use pack();
    Is there any other method to obtain the same result of JFrame resizing dynamically in response to components resize ?
    Edited by: pimpcane on Dec 12, 2007 12:13 PM
    Edited by: pimpcane on Dec 12, 2007 12:15 PM

  • Why can't JButton resize use setSize but have to use setPreferredSize

    Dear all,
    i have one question here. Why is it that i have to use setPreferredSize() instead of setSize() to resize a JButton? As from the API, setSize() is inherited from java.awt.Component and setPreferredSize() inherited from javax.swing..JComponent and I should be able to use any of the 2 methods. Am i correct to say that? Please help to solve my doubt. Thanks.

    I think i am still a bit confused. If i am not using any layout manager and i wish to resize the JButton, i still have to stick to setPreferredSize? Example of question:
           JFrame frame = new JFrame("Set Size");
              JPanel panel = new JPanel();
              JButton b1 = new JButton("Size");
              b1.setPreferredSize(new Dimension(60,20));
         //     b1.setSize(new Dimension(60,20));
              panel.add(b1);
              frame.setContentPane(panel);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setSize(200,100);
              frame.setVisible(true);In this case, i did not use any layout manager but setSize does not seem to works. Sorry about all this confusion as i wish to know the where is my problem and understand fully.

  • Resizing of Jbutton when we drag the button

    Hi ,
    I want to resize the button dynamically when we drag the edge of the button.Is it possible in swing.Can you
    please send me the sample code.
    Thanking you,
    with regards,
    Revathi

    Hi Revathi,
    Here is a sample test code you can execute directly :
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.plaf.basic.*;
    import javax.swing.plaf.*;
    import javax.swing.border.*;
    public class MyFrame extends JFrame {
         public static String windowsUI = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
         private static final Cursor horizontalCursor = new Cursor(Cursor.W_RESIZE_CURSOR);
         private static final Cursor verticalCursor = new Cursor(Cursor.S_RESIZE_CURSOR);
         private static final Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);
         public MyFrame() {
              super("My Frame");
              setWindowsLF();
              addWindowListener(new WindowAdapter() {
                   public void windowClosing(WindowEvent e) {
                        System.exit(0);
              setBounds(312,250,412,200);
              Container c = getContentPane();
              c.setLayout(null);     // very important
              JButton b1 = new JButton("button", new ImageIcon("save.gif"));
              c.add(b1);
              b1.setBounds(10,10,100,50);
              new ButtonResizeListener(b1);
              JToggleButton b2 = new JToggleButton("toggle", new ImageIcon("save.gif"));
              c.add(b2);
              b2.setBounds(120,10,100,50);
              new ButtonResizeListener(b2);
              setVisible(true);
         final class ButtonResizeListener extends MouseInputAdapter {
              private int selectedEdge = 0;
              private JComponent component;
              private Rectangle bounds;
              public ButtonResizeListener(JComponent component) {//can't be shared my several components
                   this.component = component;
                   component.addMouseListener(this);
                   component.addMouseMotionListener(this);
              private final void getCursor(Point p) {
                   double x = p.getX();
                   double y = p.getY();
                   bounds = component.getBounds();
                   if (x<3) {
                        component.setCursor(horizontalCursor);
                   else
                   if (x>bounds.getWidth()-4) {
                        component.setCursor(horizontalCursor);
                   else
                   if (y<3) {
                        component.setCursor(verticalCursor);
                   else
                   if (y>bounds.getHeight()-4) {
                        component.setCursor(verticalCursor);
                   else {
                        component.setCursor(defaultCursor);
              private int getEdge(Point p) {
                   int edge = 0;
                   double x = p.getX();
                   double y = p.getY();
                   bounds = component.getBounds();
                   if (x<3) {
                        edge = 1;
                        component.setCursor(horizontalCursor);
                   else
                   if (x>bounds.getWidth()-4) {
                        edge = 3;
                        component.setCursor(horizontalCursor);
                   else
                   if (y<3) {
                        edge = 2;
                        component.setCursor(verticalCursor);
                   else
                   if (y>bounds.getHeight()-4) {
                        edge = 4;
                        component.setCursor(verticalCursor);
                   else {
                        component.setCursor(defaultCursor);
                   return(edge);
              private void moveTo(int edge, Point dir) {
                   Point dir2 = SwingUtilities.convertPoint(component, dir, component.getParent());
                   bounds = component.getBounds();
                   if (selectedEdge==1) {
                        component.setBounds((int)(dir2.getX()), (int)bounds.getY(), (int)(bounds.getWidth()-dir2.getX()+bounds.getX()), (int)bounds.getHeight());
                   else
                   if (selectedEdge==2) {
                        component.setBounds((int)bounds.getX(), (int)dir2.getY(), (int)bounds.getWidth(), (int)(bounds.getHeight()-dir2.getY()+1+bounds.getY()));
                   else
                   if (selectedEdge==3) {
                        component.setBounds((int)bounds.getX(), (int)bounds.getY(), (int)(dir.getX()+1), (int)bounds.getHeight());
                   else
                   if (selectedEdge==4) {
                        component.setBounds((int)bounds.getX(), (int)bounds.getY(), (int)bounds.getWidth(), (int)(dir.getY()+1));
              public void mouseExit(MouseEvent e) {
                   component = null;
              public void mouseMoved(MouseEvent e) {
                   getCursor(e.getPoint());
              public void mousePressed(MouseEvent e) {
                   selectedEdge = getEdge(e.getPoint());
              public void mouseDragged(MouseEvent e) {
                   if (selectedEdge == 0) {
                        return;
                   moveTo(selectedEdge, e.getPoint());
              public void mouseReleased(MouseEvent e) {
                   bounds = component.getBounds();
         public static final void setWindowsLF() {
              try {
                   UIManager.setLookAndFeel(windowsUI);
              catch (Exception exc) {
                   System.err.println("Error loading L&F: " + exc);
         public static void main(String[] args) {
              new MyFrame();
    }Denis

  • JButton with auto resizing icon

    Hi,
    I want to extend the JButon in order to make it auto resize the icon image to the button size itself.
    I tried somethink like the following code and it work but there is some recursive side effect I cannot understand becouse I get a 100% CPU usage and from the print() I see the paint() method is called continuosly.
    import java.awt.Graphics;
    import java.awt.Image;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    public class IconButton extends JButton {
    private static final long serialVersionUID = 4049919355141829686L;
    int cntr;
    public IconButton(ImageIcon icon) {
    super(icon);
    public void paintComponent(Graphics g) {
    System.out.println(cntr++);
    Icon icn = getIcon();
    if (icn instanceof ImageIcon) {
    ImageIcon ii = (ImageIcon)icn;
    Image img = ii.getImage();
    img = img.getScaledInstance(getWidth(), getHeight(), Image.SCALE_AREA_AVERAGING);
    icn = new ImageIcon(img);
    super.setIcon(icn);
    super.paintComponent(g);
    Maybe there's a better way... for example by changing the icon only when the button size change: but how?
    Thank in advance,
    Carlo.

    ok,
    the question is:
    how to have a Button with the icon automatically resized to the button size? Is it right to extends JButton or is better extends AbstractButton?
    thanks.

  • Resize the size of JButton

    Just wondering is there a way to resize the size of a Jbutton? Thanks!

    That depends mainly on the container where you placed the button. Post a minimal program demsontrating your problem.

  • JButton wont resize - Question not Problem

    Hi,
    I'm wondering if you can give me the reason behind this behaviour.
    If you add a button to a panel, then call setSize(), the button doesn't resize itself on the panel. I've tried setting the setPreferredSize method as well, but the button always stays as the size it was created.
    I know there are work arounds, but how come you cant just set the size of a button on an empty panel?
    Cheers
    Dave

    setSize(...) won't work, because the LayoutManager will override whatever you set. (Although setSize(...) is require when using a null LayoutManager).
    setPreferredSize(...) should work. If it doesn't then you have something wrong with your program.

  • Resizing JFrame with a Jbutton click

    I've witten a simple GUI program that if you click on one button it increases the size of the JFrame and the other button will decrease it. The problem is I tried using ComponentResize() putting a listener on the buttons,but it wouldn't work. Anybody with an idea of how I could do this?

    try this code
    public void actionListener (ActionEvent e)
           int increment = 20;
           frame.setBounds (0,0,frame.getWidth()+increment,frame.getWidth()+increment);
    }

Maybe you are looking for

  • Running 2 additional monitors?

    Hi there, running a new-ish 21.5" iMac. Already have one external monitor hooked up and running smoothly with the iMac display, wondering if there's any way I can hook a second one up at the same time. Maybe a pigtail splitter running from the displa

  • Can we use SmartForms in WM Module?

    Hello,          I would like to know if we can use SmartForms for WM Module, lets say, TO (Transfer Order) Output. Please provide your Suggestions. Thanks and Regards, Venkata Phani Prasad K

  • ORA-01031: insufficient privileges - after upgrade from 2.1 to 3.0.1

    Hi I installed 10g with APEX 2.1 and started working on an app. I had the privs needed to do all DDL. I updated to 3.0.1 (not 3.1 because my work won't go to 3.1 yet) I started working on the app not problem with DML but I tried to create a function

  • Download difficulties

    The problems I have met are :- Photoshop CS5 will not open without Java SE 6 runtime which i am having **** of a job locating and downloading, Parallels 7 for Mac will not work and opening the internet is very slow. I have a 2 year old iMac running S

  • Outlook synchronisation: what smart-alec developer decided USB cables were old hat?

    I've been trawling the internet/this forum to see if BlackBerry have condescended to come up with a way of allowing those of us who want to synchronise Outlook Contacts/Calendars with a Z10 to do so (as I've been doing with my boss's BlackBerries ove