Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8540415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:35:27+00:00 2026-06-11T11:35:27+00:00

I want to increase and decrease the size of JButton on its focus gain

  • 0

I want to increase and decrease the size of JButton on its focus gain and lost event. Simultaneously I want to change the JLabel with the text of focussed JButton.

If I’m not changing the JLabel text, I’m able to change the size of buttons, but when I’m changing the label simultaneously the size of JButton does not change.

Here is the code:

public class Main extends JFrame implements FocusListener {

    JButton b1, b2;
    JLabel lbl;
    private static final long serialVersionUID = 1L;

    public Main() {

        setSize(600, 600);//Size of JFrame
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);//Sets if its visible.

        JPanel panel = new JPanel();

        b1 = new JButton("Start");//The JButton name.
        b1.setRequestFocusEnabled(false);
        b1.addFocusListener(this);
        panel.add(b1);

        b2 = new JButton("End");//The JButton name.
        b2.setRequestFocusEnabled(false);
        b2.addFocusListener(this);

        panel.add(b2);

        add(panel, BorderLayout.CENTER);
        lbl = new JLabel("       ");
        add(lbl, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        new Main();//Reads method main()
    }

    /*
    * What the button does.
    */
    @Override
    public void focusLost(FocusEvent ae) {

        if (ae.getSource() == b2) {
            b2.setSize(55, 26);
        } else if (ae.getSource() == b1) {
            b1.setSize(55, 26);
        }
    }

    @Override
    public void focusGained(FocusEvent ae) {

        if (ae.getSource() == b2) {
            lbl.setText("End");
            b2.setSize(55, 40);
        } else if (ae.getSource() == b1) {
            lbl.setText("Start");
            b1.setSize(55, 40);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:35:28+00:00Added an answer on June 11, 2026 at 11:35 am

    The problem is that you are mixing absolute-layout (or null-layout) with an implicit LayoutManager (JPanel comes with a FlowLayout by default).

    When you call setText on a JLabel (and that text changes), it automatically calls revalidate which will eventually trigger the LayoutManager to lay out the components.

    Either use a LayoutManager, optionally set some constraints and pref/min/max size (but the latter is not recommended) (and you never call setLocation/setSize/setBounds), or use null-layout and set yourself the location and size of the components (and in this case you must call yourself setSize/setLocation/setBounds).

    Seriously consider reading the LayoutManager tutorial. There is a dedicated chapter on “Doing Without a Layout Manager (Absolute Positioning)“.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to draw a rectangle using canvas which change its size with different
I want to change text size of EditText as text entered. It means initially
For each click I want to increase the font size. $('#fontplus').each(function('click') { var fs
I have written this code where I want to increase / decrease number of
I want to increase or decrease or even mute the volume of movie player
I have sketchpad as InkCanvas ; I want to change size of eraser so
How do you increase or decrease the size of a UIImageView in Obj-C? For
I want to set the font size of button on componentResized event. I get
I have input text and I want to increase the number that is inside
I want to increase/decrease the font of and element, then the next element of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.