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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:44:22+00:00 2026-06-17T22:44:22+00:00

I am writing a Java program that displays a combo box that get’s it’s

  • 0

I am writing a Java program that displays a combo box that get’s it’s information from a properties file. There is a settings class that will allow the user to update the names of the fields that are in the combo box. My problem is when the settings class is called and modified, I cannot figure out how to update the combo box with the new settings. I have a method that will repaint the entire panel and re-load the combo box. But I do not know how to activate that method when the “apply” button is pressed in the setting class.

Here is a rough and tough example of what I am trying to accomplish.

Main Class:

package testing;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Properties;

import javax.swing.*;

public class testConfigLoad extends JFrame
{
    JButton apply = new JButton("Apply");
JButton set   = new JButton("Settings");

    Properties          config      = new Properties();
    FileInputStream     fis         = null;
    FileOutputStream    fos         = null;
    final String        configFile  = "config.properties";

    OptPanel opt;

    public testConfigLoad() throws IOException
    {
        super("Test Program");
        setSize(200, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        config();
        opt = new OptPanel(config);
        buildFrame();
    }

    public void buildFrame()
    {
        set.addActionListener(new setListener());

        add(opt);
        add(apply);
        add(set);

        setVisible(true);
    }

    public void config() throws IOException
    {
        try
        {
            fis = new FileInputStream(configFile);
            config.load(fis);
        }
        catch (FileNotFoundException e)
        {
            System.out.println("File not found");
        }
        finally
        {
            if (fis != null)
            {
                fis.close();
            }
        }       
    }

    private class setListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            settings set = new settings(config);
        }
    }

    public static void main(String[] args) throws IOException
    {
        new testConfigLoad();
    }
} 

Panel that needs to be Refreshed:

package testing;

import java.util.Properties;
import javax.swing.*;

public class OptPanel extends JPanel
{
    String[] opts;
    JLabel optLabel = new JLabel("Available Options");
    Properties config;

    public OptPanel(Properties p)
    {
        config = p;
        opts = new String[3];
        buildPanel();
    }

    public void buildPanel()
    {
        for (int i = 0; i < opts.length; i++)
        {
            opts[i] = config.getProperty("option." + i + ".name");
        }

        JComboBox optBox = new JComboBox(opts);

        add(optLabel);
        add(optBox);
    }

    public void refPanel()
    {
        removeAll();
        this.buildPanel();
        ((JPanel) this).revalidate();
        repaint();
    }
}

And the settings class:

package testing;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.swing.*;

public class settings 
{
    Properties config;
    final String        configFile  = "config.properties";

    JFrame setFrame = new JFrame("Settings");
    JLabel opt1 = new JLabel("Option 1");
    JLabel opt2 = new JLabel("Option 2");
    JLabel opt3 = new JLabel("Option 3");
    JTextField text1 = new JTextField(15);
    JTextField text2 = new JTextField(15);
    JTextField text3 = new JTextField(15);
    JButton apply = new JButton("Apply");

    public settings(Properties p)
    {
        config = p;
        setFrame.setSize(275, 200);
        setFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        setFrame.setLayout(new FlowLayout());

        buildSetFrame();
    }

    public void buildSetFrame()
    {
        text1.setText(config.getProperty("option.0.name"));
        text2.setText(config.getProperty("option.1.name"));
        text3.setText(config.getProperty("option.2.name"));

        apply.addActionListener(new applyListener());

        setFrame.add(opt1);
        setFrame.add(text1);
        setFrame.add(opt2);
        setFrame.add(text2);
        setFrame.add(opt3);
        setFrame.add(text3);
        setFrame.add(apply);

        setFrame.setVisible(true);
    }

    private class applyListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e) 
        {
            config.setProperty("option.0.name", text1.getText());
            config.setProperty("option.1.name", text2.getText());
            config.setProperty("option.2.name", text3.getText());

            FileOutputStream fos = null;
            try
            {
                fos = new FileOutputStream(configFile);
                config.store(fos, null);
            }
            catch (IOException f)
            {
                System.out.println("Error");
            }
            finally
            {
                if (fos != null)
                {
                    try
                    {
                        fos.close();
                    }
                    catch (IOException g)
                    {
                        System.out.println("Problem");
                    }
                }
            }
            setFrame.setVisible(false);

            //  This is where I need to pass something back to the 
                    //  testConfigLoad class to tell it to 
            //  run the refPanel method in the OptPanel class.
        }
    }
}   

The config file is named config.properties and looks like this:

option.2.name=two
option.1.name=one
option.0.name=zero
  • 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-17T22:44:23+00:00Added an answer on June 17, 2026 at 10:44 pm

    One approach would be to use a callback between the OptPanel and the settings class. You could extract out the functionality to add the elements to the properties JComboBox into its own method:

    public void updateProperties(Properties p) {
        model.removeAllElements();
        for (String s: p.stringPropertyNames()) {
            model.addElement(p.getProperty(s));
        }
    }
    

    where model is a DefaultComboBoxModel. Then you could simply call

    optPanel.updateProperties(config);
    

    after successfully storing the properties.


    Some Points:

    • The preferred approach is not to use multiple JFrames. One option is to use a single JFrame with a modal dialog. See this discussion.
    • Don’t extend JFrame, rather use an instance directly.
    • Java Naming conventions indicate that class names start with an uppercase letter, so settings would become Settings
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a program that's parsing an XML file to java objects using smooks.
I'm writing a Java program that needs to extract a 32 character key from
I have a Java program that reads some text from a file, line by
I'm writing a java program that will take int input as a option from
I am writing a java program that needs a file open dialog. The file
I am writing a Java program that inputs a test file, performs some modifications
I am currently writing a chatroom program in Java. There is a server that
I'm writing a java program that goes out and searches twitter, finds certain instagram
I'm currently writing a java program that requires some Data to run. The data
I am writing a program that needs to run a java.jar server. I need

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.