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

  • Home
  • SEARCH
  • 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 7659761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:22:32+00:00 2026-05-31T13:22:32+00:00

My use case is that a List<String> is passed to a Jpanel and for

  • 0

My use case is that a List<String> is passed to a Jpanel and for each String in the List, the JPanel renders a UI component. This UI component consists of 3 buttons and my current code for my given use case is as follows. —
The code for the ‘UI component’ follows —

public class MacroEditorEntity implements ActionListener {
    private String macro;
    private JButton upButton;
    private JButton downButton;
    private JButton MacroDetailsButton;

    public MacroEditorEntity(String macro) {
    this.macro = macro;
    upButton =  new JButton("Up");
    downButton = new JButton("Down");
    MacroDetailsButton =  new JButton(macro);

    upButton.addActionListener(this);
    downButton.addActionListener(this);
    MacroDetailsButton.addActionListener(this);
}

    @Override
    public void actionPerformed(ActionEvent evt) {

        if(evt.getSource().equals(MacroDetailsButton))
        {
            System.out.println(macro);
        }
    }

    public JButton GetUpButton()
    {
        return upButton;
    }
    public JButton GetDownButton()
    {
        return downButton;
    }
    public JButton getMacroDetailsButton()
    {
        return MacroDetailsButton;
    }
}

The code for my Panel is as follows —

public class MacroEditor extends JPanel implements PropertyChangeListener {

    private static final long serialVersionUID = 1L;
    private List<String> stringlist;

    public MacroEditor(List<String> list) {

        this.stringlist = list;
        setupComponents();
        validate();
        setVisible(true);
    }

    public void setupComponents()
    {
        Box allButtons =  Box.createVerticalBox();
        for(String string : stringlist)
        {
            MacroEditorEntity entry =  new MacroEditorEntity(string);
            Box entryBox =  Box.createHorizontalBox();
            entryBox.add(entry.GetUpButton());
            entryBox.add(Box.createHorizontalStrut(15));
            entryBox.add(entry.getMacroDetailsButton());
            entryBox.add(Box.createHorizontalStrut(15));
            entryBox.add(entry.GetDownButton());

            allButtons.add(entryBox);
        }

        add(allButtons);
    }

    @Override
    public void propertyChange(PropertyChangeEvent arg0) {
        revalidate();
        repaint();
    }

}

The code works fine for all Strings in the passed List. I want my Panel to pick up any change that may happen to the List like additions or deletions and add/remove relevant corresponding UI components accordingly. I think this can be done by using PropertyChangeListener but have not been able to account for that in my code.
Any ideas or suggestions on how i can make my Panel render/rerender stuff as soon as there are changes to the List would be of help.

  • 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-31T13:22:33+00:00Added an answer on May 31, 2026 at 1:22 pm

    What you need here is an observable collection. This should do it: http://commons.apache.org/dormant/events/apidocs/org/apache/commons/events/observable/ObservableCollection.html

    Edit:

    Here’s the code snippet you requested:

    public class ObservableListExample implements StandardPostModificationListener,
        StandardPreModificationListener {
    
        public static void main(String[] args) {
            new ObservableListExample();
        }
    
        public ObservableListExample() {
    
            ObservableList list = ObservableList.decorate(new ArrayList<>(),
                    new StandardModificationHandler());
    
            list.getHandler().addPostModificationListener(this);
            list.getHandler().addPreModificationListener(this);
            //....
    
        }
    
        @Override
        public void modificationOccurring(StandardPreModificationEvent event) {
            // before modification
            Collection changeCollection = event.getChangeCollection();
            if (event.isTypeAdd()) {
                // changeCollection contains added elements
            } else if (event.isTypeReduce()) {
                // changeCollection contains removed elements
            }
        }
    
        @Override
        public void modificationOccurred(StandardPostModificationEvent event) {
            // after modification
            Collection changeCollection = event.getChangeCollection();
            if (event.isTypeAdd()) {
                // changeCollection contains added elements
            } else if (event.isTypeReduce()) {
                // changeCollection contains removed elements
            }
        }
    }
    

    By the way: Another concept that helps to bind buisness objects to your GUI and react to modifications (bidirectionally) is Data Binding. Have a look at this, a Data Binding Library commonly used with Swing.

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

Sidebar

Related Questions

My use case is this... I have a project that has two production branches.
I've read that it's unwise to use ToUpper and ToLower to perform case-insensitive string
This is my use case: Input is a string representing an Oracle PL/SQL statement
This one should be pretty simple. The use case is that I have a
The use case is that I use one development machine for different source trees.
What is CLR hosting? What is the use case for that?
On a Use Case diagram can you show things that an actor cannot do,
Here's the use case: A user clicks on a link that opens a window
I have the following use case: There's a class called Template and with that
That is, I'd like to have a tuple of values. The use case on

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.