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 762193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:55:42+00:00 2026-05-14T15:55:42+00:00

I’ve written an application in Swing that listens for UDP packets from a smart

  • 0

I’ve written an application in Swing that listens for UDP packets from a smart battery and displays them in JTextFields inside a JPanel inside a JFrame.

For some reason, minimizing the application and then restoring it smooshes all the text inside the center of the main frame and prevents updates to the JTextFields to be drawn to the screen.

I’m not sure why this is happening (Swing newb) or exactly how to fix it. Below are code snippets with the relevant code.

public class Initializer {

public void initialize() {

            //The mediator performs all updates of the BatteryPanel
            mediator = Mediator.getInstance();

            //BatteryService listens for UDP packets and uses mediator to update panel
            bService = new BatteryService();

    createGUI();

    bService.start();   
}

public void createGUI() {

    bPanel = new BatteryPanel();
    frame = new JFrame();

            //For spacing between the BatteryPanel and the edge of the window
    frame.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.insets = new Insets(6,8,8,6);

    frame.getContentPane().add(bPanel, gbc);
    frame.setResizable(false);

    mediator.setBatteryPanel(bPanel);
    frame.pack();
    frame.setVisible(true);

}
} 

public class BatteryService {
   private Mediator mediator;
   ...
         //This is inside a SwingWorker - we have data now update the panel
         protected void process(List<BatteryUpdateBean> bBeans) {
           ...
           mediator.setBatteryStatus(status);
           mediator.setTemperature(temperature);
           mediator.setLastConnected(lastConnected);
         }
       }
   }
}

public class BatteryPanel extends JPanel {

private static final int AFTER_LABEL_SPACE = 8;
private static final int AFTER_TITLE_SPACE = 8;
private static final int BETWEEN_ROWS_SPACE = 3;

private JTextField statusField;
private JTextField temperatureField;
private JTextField lastConnectedField;

public BatteryPanel() {     
    initComponents();
}

    //get textfield methods snipped
    ...

private void initComponents() {
    JLabel titleLabel = new JLabel("Battery");
    titleLabel.setFont(new Font("Tahoma", Font.BOLD, 14));

    JLabel lastConnectedLabel = new JLabel("Last connected:");
    JLabel statusLabel = new JLabel("Status:");
    JLabel temperatureLabel = new JLabel("Temperature:");

    temperatureField= new JTextField("NO CONNECTION                ");
    temperatureField.setOpaque(false);
    temperatureField.setEditable(false);
    temperatureField.setBorder(BorderFactory.createEmptyBorder());

    statusField= new JTextField("                                 ");
    statusField.setOpaque(false);
    statusField.setEditable(false);
    statusField.setBorder(BorderFactory.createEmptyBorder());

    powerField = new JTextField("                             ");
    powerField.setOpaque(false);
    powerField.setEditable(false);
    powerField.setBorder(BorderFactory.createEmptyBorder());


    setLayout(new GridBagLayout());

    GridBagConstraints titleC = new GridBagConstraints();
    GridBagConstraints lastConnectedLabelC = new GridBagConstraints();
    GridBagConstraints statusLabelC = new GridBagConstraints();
    GridBagConstraints temperatureLabelC = new GridBagConstraints();
    GridBagConstraints statusFieldC = new GridBagConstraints();
    GridBagConstraints temperatureFieldC = new GridBagConstraints();
    GridBagConstraints lastConnectedFieldC = new GridBagConstraints();

    titleC.gridx = 0; titleC.gridy = 0; titleC.gridwidth = 2;
    titleC.anchor = GridBagConstraints.FIRST_LINE_START;
    titleC.insets = new Insets(0, 0, AFTER_TITLE_SPACE, 0);

    lastConnectedLabelC.gridx = 0;  lastConnectedLabelC.gridy = 1;
    lastConnectedLabelC.anchor = GridBagConstraints.LINE_START;
    lastConnectedLabelC.insets = new Insets(0,0,BETWEEN_ROWS_SPACE,AFTER_LABEL_SPACE);

    lastConnectedFieldC.gridx = 1; lastConnectedFieldC.gridy = 1;
    lastConnectedFieldC.anchor = GridBagConstraints.LINE_START;
    lastConnectedFieldC.insets = new Insets(0,0,BETWEEN_ROWS_SPACE,0);

    statusLabelC.gridx = 0; statusLabelC.gridy = 2;
    statusLabelC.anchor = GridBagConstraints.LINE_START;
    statusLabelC.insets = new Insets(0,0,BETWEEN_ROWS_SPACE,AFTER_LABEL_SPACE);

    statusFieldC.gridx = 1; statusFieldC.gridy = 2;
    statusFieldC.anchor = GridBagConstraints.LINE_START;
    statusFieldC.insets = new Insets(0,0,BETWEEN_ROWS_SPACE,0);
    statusFieldC.fill = GridBagConstraints.HORIZONTAL;

    temperatureLabelC.gridx = 0; temperatureLabelC.gridy = 3;
    temperatureLabelC.anchor = GridBagConstraints.LINE_START;
    temperatureLabelC.insets = new Insets(0,0,BETWEEN_ROWS_SPACE,AFTER_LABEL_SPACE);


    temperatureFieldC.gridx = 1; temperatureFieldC.gridy = 3;
    temperatureFieldC.anchor = GridBagConstraints.LINE_START;
    temperatureFieldC.insets = new Insets(0,0,BETWEEN_ROWS_SPACE,0);

    ...
            //add (item, constraints) snipped       
}

I’d greatly appreciate anyone’s help with this.

  • 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-05-14T15:55:42+00:00Added an answer on May 14, 2026 at 3:55 pm

    On your GridBagConstraints at the top level, have you considered setting the other properties of the object? I’d look into:

    gbc.fill = GridBagConstraints.BOTH;
    gbc.weightx = 1;
    gbc.weighty = 1;
    

    Also, in your BatteryPanel, you can re-use the same GridBagConstraints object and just change the values. Check out the GridBagLayout tutorial for more information.

    Once you fix your layout, I think you’ll find the behavior to be as expected.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from

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.