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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:19:44+00:00 2026-06-10T16:19:44+00:00

I am trying to create gui, which changes part of its elements after reloading

  • 0

I am trying to create gui, which changes part of its elements after reloading XML file (application creates few GUI elements at start according to the content of XML file).

Everything works fine after starting the application (creating 20 buttons [10 word-translation pairs] if test.xml is loaded, and 4 buttons [2 word-translation pairs], when testTwo.xml is loaded), but I dont know how to reload or shuffle the GUI content after that (after clicking on the button).

I’ve tried putting revalidate(); in ActionListener, but it doesnt work in my application.

I will be glad if you can point me in right direction on how to do this.

GUI: XmlGui.java

package xmltest;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import net.miginfocom.swing.MigLayout;
import org.xml.sax.SAXException;

public class XmlGui extends JFrame {

    protected JPanel panel;
    protected JFrame frame;
    protected File file = new File("src/xmltest/test.xml");
    protected JButton setOne, setTwo, shuffle;
    protected JTextArea text;
    protected XmlTest engine;

    public XmlGui() throws ParserConfigurationException, SAXException, IOException {

        XmlEvent xmlEvent = new XmlEvent(this);

        SAXParserFactory spfac = SAXParserFactory.newInstance();
        SAXParser sp = spfac.newSAXParser();
        XmlTest engine = new XmlTest();
        sp.parse(file, engine);
        engine.readList();
        engine.shuffleList(1);

        frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());

        panel = new JPanel(new MigLayout("Insets 0"));

        setOne = new JButton("Default (test.xml)");
        setOne.addActionListener(xmlEvent);
        panel.add(setOne, "split 3");

        setTwo = new JButton("Next set (testTwo.xml)");
        setTwo.addActionListener(xmlEvent);
        panel.add(setTwo);

        shuffle = new JButton("Shuffle");
        shuffle.addActionListener(xmlEvent);
        panel.add(shuffle, "wrap");

        for (int i = 0; i < engine.cardList.size(); i++) {

            JPanel xpanel = new JPanel(new MigLayout("Insets 0"));
            final String test = engine.cardList.get(i).getTextOne();
            JButton word = new JButton(engine.cardList.get(i).getWord()+" ("+i+")");
            word.setPreferredSize(new Dimension(200, 40));
            word.addActionListener(new ActionListener(){
                public void ActionListener(ActionEvent event) {

                }

                @Override
                public void actionPerformed(ActionEvent e) {
                    text.setText(test);
                }
            });

            JButton translation = new JButton(engine.cardList.get(i).getTranslation()+" ("+i+") ");
            translation.setName("translation"+i);
            translation.setPreferredSize(new Dimension(200, 40));

            xpanel.add(word);
            xpanel.add(translation);
            panel.add(xpanel, "wrap");

        }

        text = new JTextArea();
        text.setLineWrap(true);
        text.setPreferredSize(new Dimension(400, 45));
        panel.add(text);

        frame.add(panel);
        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String[] arguments) throws ParserConfigurationException, SAXException, IOException {

        XmlGui gui = new XmlGui();

    }

}

XML methods: XmlTest.java

package xmltest;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XmlTest extends DefaultHandler {

       Card card;
       private String temp;
       ArrayList<Card> cardList = new ArrayList<Card>();

       public void characters(char[] buffer, int start, int length) {
              temp = new String(buffer, start, length);
       }


       public void startElement(String uri, String localName,
                     String qName, Attributes attributes) throws SAXException {
              temp = "";
              if (qName.equalsIgnoreCase("Card")) {
                     card = new Card();

              }
       }

       public void endElement(String uri, String localName, String qName)
                     throws SAXException {

              if (qName.equalsIgnoreCase("Card")) {
                     cardList.add(card);

              } else if (qName.equalsIgnoreCase("Word")) {
                     card.setWord(temp);
              } else if (qName.equalsIgnoreCase("Translation")) {
                     card.setTranslation(temp);
              } else if (qName.equalsIgnoreCase("TextOne")) {
                     card.setTextOne(temp);
              } else if (qName.equalsIgnoreCase("TextTwo")) {
                     card.setTextTwo(temp);
              }

       }

       public void readList() {
              System.out.println("Number of cards in the collection: " + cardList.size()  + ".\n");
              Iterator<Card> it = cardList.iterator();
              while (it.hasNext()) {
                     System.out.println(it.next().toString());
              }
       }

       public void shuffleList(int x) {
              System.out.println("Shuffled cards order ("+x+"): ");
              Collections.shuffle(cardList);
              Iterator<Card> it = cardList.iterator();
              while (it.hasNext()) {
                     System.out.print(it.next().shuffledList());
              }
              System.out.println("\n");
       }

}

Events class: XmlEvent.java

package xmltest;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class XmlEvent implements ActionListener {

        XmlGui xmlGui;

        XmlEvent (XmlGui in) {

        xmlGui = in;
    }


    @Override
    public void actionPerformed(ActionEvent e) {

        Object source = e.getSource();

        if (source.equals(xmlGui.setOne)) {

            xmlGui.file = new File("src/xmltest/test.xml");
            xmlGui.text.setText("test.xml");

        }

        if (source.equals(xmlGui.setTwo)) {

            xmlGui.file = new File("src/xmltest/testTwo.xml");
            xmlGui.text.setText("testTwo.xml");

        }

        if (source.equals(xmlGui.shuffle)) {

            xmlGui.text.setText("Shuffling list!");

        }

    }

}

Representation of XML data: Card.java

package xmltest;

public class Card {

       private String word;
       private String translation;
       private String textOne;
       private String textTwo;

       public Card() {
       }

       public Card(String word, String translation, String textOne, String textTwo) {
              this.word = word;
              this.translation = translation;
              this.textOne = textOne;
              this.textTwo = textTwo;
       }

       public String getWord() {
              return word;
       }

       public void setWord(String word) {
              this.word = word;
       }

       public String getTranslation() {
              return translation;
       }

       public void setTranslation(String translation) {
              this.translation = translation;
       }

       public String getTextOne() {
              return textOne;
       }

       public void setTextOne(String textOne) {
              this.textOne = textOne;
       }

       public String getTextTwo() {
              return textTwo;
       }

       public void setTextTwo(String textTwo) {
              this.textTwo = textTwo;
       }

       public String toString() {
              StringBuffer sb = new StringBuffer();
              sb.append("Card details:");
              sb.append("\nWord: " + getWord());
              sb.append("\nTranslation: " + getTranslation());
              sb.append("\nTextOne: " + getTextOne());
              if(getTextTwo().equals("blank")) {
                  sb.append("\n\n");
              } else {
              sb.append("\nTextTwo: " + getTextTwo()+"\n\n");
              }

              return sb.toString();
       }

       public String shuffledList() {
           return getWord()+" ";
       }
}

XML1: test.xml

<?xml version="1.0" encoding="UTF-8"?>
<cards>
    <card>
        <word>cloud</word>
        <translation>chmura</translation>
        <textOne>D: a visible collection of particles of water or ice suspended in the air.</textOne>
        <textTwo>blank</textTwo>
    </card>
    <card>
        <word>rain</word>
        <translation>deszcz</translation>
        <textOne>D: water that is condensed from the aqueous vapor in the atmosphere and falls to earth in drops.</textOne>
        <textTwo>blank</textTwo>
    </card>
    <card>
        <word>wind</word>
        <translation>wiatr</translation>
        <textOne>D: air in natural motion, as that moving horizontally at any velocity along the earth's surface.</textOne>
        <textTwo>blank</textTwo>
    </card>
    <card>
        <word>storm</word>
        <translation>burza</translation>
        <textOne>D: a disturbance of the normal condition of the atmosphere, manifesting itself by winds of unusual force or direction, often accompanied by rain, snow, hail, thunder, and lightning, or flying sand or dust.</textOne>
        <textTwo>blank</textTwo>
    </card>
    <card>
        <word>blizzard</word>
        <translation>zamieć</translation>
        <textOne>D: a storm with dry, driving snow, strong winds, and intense cold.</textOne>
        <textTwo>blank</textTwo>
    </card>
    <card>
        <word>fog</word>
        <translation>mgła</translation>
        <textOne>D: a cloudlike mass or layer of minute water droplets or ice crystals near the surface of the earth, appreciably reducing visibility.</textOne>
        <textTwo>blank</textTwo>
    </card>
    <card>
        <word>sunny</word>
        <translation>słoneczny</translation>
        <textOne>D: abounding in sunshine.</textOne>
        <textTwo>test</textTwo>
    </card>
    <card>
        <word>weather</word>
        <translation>pogoda</translation>
        <textOne>D: the state of the atmosphere with respect to wind, temperature, cloudiness, moisture, pressure, etc.</textOne>
        <textTwo>blank</textTwo>
    </card>
    <card>
        <word>temperature</word>
        <translation>temperatura</translation>
        <textOne>D: a measure of the warmth or coldness of an object or substance with reference to some standard value.</textOne>
        <textTwo>blank</textTwo>
    </card>
    <card>
        <word>hail</word>
        <translation>grad</translation>
        <textOne>D: showery precipitation in the form of irregular pellets or balls of ice.</textOne>
        <textTwo>blank</textTwo>
    </card>
</cards>

XML2: testTwo.xml

<?xml version="1.0" encoding="UTF-8"?>
<cards>
    <card>
        <word>cloud</word>
        <translation>chmura</translation>
        <textOne>D: a visible collection of particles of water or ice suspended in the air.</textOne>
        <textTwo>blank</textTwo>
    </card>
    <card>
        <word>rain</word>
        <translation>deszcz</translation>
        <textOne>D: water that is condensed from the aqueous vapor in the atmosphere and falls to earth in drops.</textOne>
        <textTwo>blank</textTwo>
    </card>
</cards>
  • 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-10T16:19:46+00:00Added an answer on June 10, 2026 at 4:19 pm

    I’ve solved my problem by:

    1. Moving the JButton from XML creating mechanic to its own method (before it was placed in method that was creating whole GUI;
    2. Setting up new JPanel for the JButtons generated according to the XML data by method;
    3. Calling removeAll() for the JPanel with those JButtons in ActionListener for load new XML file JButton right before calling method for the new file.

    It works fine.

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

Sidebar

Related Questions

I am trying to create a simple GUI form which has only 2 elements
I'm trying to create a responsive gui, which basically means that I have an
I am trying to create my first GUI application using (Java + Eclipse +
I'm trying to create a GUI in Python using the Tkinter module, and part
I'm fairly new to Java and I'm trying to create a GUI application with
I'm trying to create a dialog that uses two GUI elements: KDEUI KActionSelector and
I am trying to make an application which will allow you to create and
I am trying to create a GUI to display information read from a file.
i m trying to create windows executable from python program which has GUI .
I am trying to create a thread which will continuously check for changes to

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.