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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:52:54+00:00 2026-05-30T20:52:54+00:00

I am trying to get plotted graphics to refresh/repopulate when the user chooses each

  • 0

I am trying to get plotted graphics to refresh/repopulate when the user chooses each of various options in a JComboBox. The examples I have found on the web all use JLabels, which may be fine for image files, but which do not work for paintComponent-generated custom graphics.

I tried to roll my own solution in the ~60 lines of code below. I am using a trivial example of a rectangle to be re-sized. If you compile and run the code below, you will see that it does not repaint when the user selects different options from the JComboBox. Also, I have deliberately not yet done anything with displayConstraints because I do not want to impose a solution if someone has a better approach. My goal is for the JComboBox to be displayed in its own row at the top, and for the plotted graphing to be done in a much larger second row underneath the first row. The second row will absorb all resizing changes, while the first row will remain more or less the same size when the JFrame is resized. By choosing different options from the JComboBox, the user will be able to make the plotted rectangle get smaller or larger relative to the current size of the JFrame.

Can anyone show me how to fix the code below so that it accomplishes my above-stated goals?

import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;

public class ComboBox extends JFrame implements ItemListener {
final String[] sizes = { "10%", "20%", "33%" };
JComboBox combobox = new JComboBox(sizes);
int selectedIndex;

public ComboBox() {
    setLayout(new GridBagLayout());
    combobox.setSelectedIndex(-1);
    combobox.addItemListener(this);
    GridBagConstraints comboBoxConstraints = new GridBagConstraints();
    comboBoxConstraints.gridx = 0;
    comboBoxConstraints.gridy = 0;
    comboBoxConstraints.gridwidth = 1;
    comboBoxConstraints.gridheight = 1;
    comboBoxConstraints.fill = GridBagConstraints.NONE;
    add(combobox,comboBoxConstraints);//This should be placed at top, in middle.

    GridBagConstraints displayConstraints = new GridBagConstraints();
    displayConstraints.gridx = 0;
    displayConstraints.gridy = 1;
    displayConstraints.gridwidth = 1;
    displayConstraints.gridheight = 1;
    displayConstraints.fill = GridBagConstraints.BOTH;
    //I am aware that nothing is done with displayConstraints.
    //I just want to indicate that the rectangle should go below the combobox,
    //and that the rectangle should resize while the combobox should not.
    //Other suggested approaches are welcome.

    setSize(300, 300);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setLocationRelativeTo(null);
    setVisible(true);
}

public static void main(String[] args) {new ComboBox();}

public void itemStateChanged(ItemEvent e) {
    if (e.getStateChange() == ItemEvent.SELECTED) {
        JComboBox combo = (JComboBox) e.getSource();
        selectedIndex = combo.getSelectedIndex();
        System.out.println("selectedIndex is: "+selectedIndex);
        repaint();
    }
}
protected void paintComponent(Graphics g){
    int scaleFactor = 1;
    if(selectedIndex==0){scaleFactor = 10;}
    if(selectedIndex==1){scaleFactor = 5;}
    if(selectedIndex==2){scaleFactor = 3;}
    if(selectedIndex!=-1){
        int xStart = (getWidth()/2)-(getWidth()/scaleFactor);
        int yStart = (getHeight()/2)-(getHeight()/scaleFactor);
        g.drawRect(xStart, yStart, (getWidth()/scaleFactor), (getHeight()/scaleFactor));
    }
}
}
  • 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-30T20:52:55+00:00Added an answer on May 30, 2026 at 8:52 pm

    You shouldn’t draw directly in a JFrame and even if you did, JFrame has no paintComponent method. Use an @Override annotation to see for yourself. Instead, draw in a JPanel or JComponent, do the drawing in paintComponent, and make sure that you’re correctly overriding the method with the override annotation.

    For example:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class ComboBoxTest extends JPanel implements ItemListener {
       private static final int PREF_W = 300;
       private static final int PREF_H = PREF_W;
       final String[] sizes = { "10%", "20%", "33%" };
       JComboBox combobox = new JComboBox(sizes);
       int selectedIndex;
       private double scaleFactor = 1;
    
       public ComboBoxTest() {
          setLayout(new GridBagLayout());
          combobox.setSelectedIndex(-1);
          combobox.addItemListener(this);
          GridBagConstraints comboBoxConstraints = new GridBagConstraints();
          comboBoxConstraints.gridx = 0;
          comboBoxConstraints.gridy = 0;
          comboBoxConstraints.gridwidth = 1;
          comboBoxConstraints.gridheight = 1;
          comboBoxConstraints.fill = GridBagConstraints.NONE;
          add(combobox, comboBoxConstraints);// This should be placed at top, in
                                             // middle.
    
          GridBagConstraints displayConstraints = new GridBagConstraints();
          displayConstraints.gridx = 0;
          displayConstraints.gridy = 1;
          displayConstraints.gridwidth = 1;
          displayConstraints.gridheight = 1;
          displayConstraints.fill = GridBagConstraints.BOTH;
       }
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       public void itemStateChanged(ItemEvent e) {
          if (e.getStateChange() == ItemEvent.SELECTED) {
             JComboBox combo = (JComboBox) e.getSource();
             selectedIndex = combo.getSelectedIndex();
             System.out.println("selectedIndex is: " + selectedIndex);
             if (selectedIndex == -1) {
                return;
             }
             String selectedItem = combo.getSelectedItem().toString().trim();
             selectedItem = selectedItem.replace("%", "");
             scaleFactor = Double.parseDouble(selectedItem) / 100.0;
             repaint();
          }
       }
    
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
             int xStart = (getWidth() / 2) - (int)(getWidth() * scaleFactor);
             int yStart = (getHeight() / 2) - (int)(getHeight() * scaleFactor);
             g.drawRect(xStart, yStart, (int)(getWidth() * scaleFactor),
                   (int)(getHeight() * scaleFactor));
       }
    
       private static void createAndShowGui() {
          JFrame frame = new JFrame("ComboBoxTest");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(new ComboBoxTest());
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to get comfortable with jQuery and I have encountered some sample code that
Trying to get my mind around google protobuf. I found some implementation of protobuf
I am trying get the html within .event_recur. $(.entry).each(function(){ alert($(this).find(.event_recur).html()); }); <div class=entry> <p
I am trying get my first simple project in rails to run. I have
I'm trying get to know decorators, and I have a problem with sharing value,
I am trying get a div that I have hidden to show in the
I'm trying get a subset of keys for each hash in an array. The
I'm trying get my <a> tag triggered when the user press on the enter
I'm trying get values from a GridView using the following code: foreach (GridViewRow row
Trying to get my css / C# functions to look like this: body {

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.