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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:35:26+00:00 2026-06-14T22:35:26+00:00

Please have a look at the following code package test; import java.awt.*; import java.awt.event.*;

  • 0

Please have a look at the following code

package test;


import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;
import javax.swing.event.SwingPropertyChangeSupport;

public class MainGui {
   public MainGui() {
      new WizardPanel().setVisible(true);
   }

   public static void main(String[] args) {
      try {
         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
         new MainGui();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

class WizardPanel extends JDialog {
   private JPanel cardPanel, buttonPanel;
   private JButton next;
   private CardLayout c1;
   private SimpleModel simpleModel = new SimpleModel();

   private FileSelector fileSelector;
   private DelemeterSelector delemeterSelector;

   public WizardPanel() {
      fileSelector = FileSelector.getInstance();
      delemeterSelector = DelemeterSelector.getInstance();

      fileSelector.setModel(simpleModel); //!!
      delemeterSelector.setModel(simpleModel); //!!

      cardPanel = new JPanel();
      c1 = new CardLayout();
      cardPanel.setLayout(c1);

      cardPanel.add(fileSelector, "1");
      cardPanel.add(delemeterSelector, "2");

      c1.show(cardPanel, "1");

      buttonPanel = new JPanel();
      buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

      next = new JButton("Next");
      next.addActionListener(new WizardPanel.NextButtonAction()); 

      buttonPanel.add(next);

      // Creating the GUI
      this.setLayout(new BorderLayout());
      this.add(cardPanel, "Center");
      this.add(buttonPanel, "South");

      this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      this.setResizable(true);
      this.pack();
      this.setVisible(true);

   }

   private class NextButtonAction implements ActionListener {
      public void actionPerformed(ActionEvent ae) {

         // c1.show(cardPanel, "2");
         c1.next(cardPanel); //!!

      }
   }
}

class FileSelector extends JPanel {
   private JLabel fileName, description;
   private JTextField fileTxt;
   private JButton browse;

   private GridBagLayout gbl;
   private GridBagConstraints gbc;
   private SimpleModel simpleModel;

   private static FileSelector instance = null;

   private FileSelector() {
      // Intializing instance variables
      fileName = new JLabel("File Name: ");
      description = new JLabel("Specify the source of the data");

      fileTxt = new JTextField(10);

      browse = new JButton("Browse");
      browse.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            if (simpleModel != null) {
               simpleModel.setFileText(fileTxt.getText());
            }
         }
      });

      gbl = new GridBagLayout();
      gbc = new GridBagConstraints();

      // Creating GUI
      this.setLayout(new GridLayout(2,1));

      this.add(description);
      this.add(locationPanel());


      this.setBorder(BorderFactory.createEmptyBorder());
   }

   public void setModel(SimpleModel simpleModel) {
      this.simpleModel = simpleModel;
   }

   private JPanel locationPanel() {
      JPanel panel = new JPanel();
      panel.setLayout(new FlowLayout());

      panel.add(fileName);
      panel.add(fileTxt);
      panel.add(browse);

      return panel;
   }

   public static FileSelector getInstance() {
      if (instance == null) {
         instance = new FileSelector();
      }

      return instance;
   }
}

class DelemeterSelector extends JPanel {
   private JLabel description;
   private JRadioButton tabBtn, semicolanBtn, commaBtn, spaceBtn;
   private JTextArea txtArea;
   private JScrollPane scroll;
   private ButtonGroup btnGroup;

   private GridBagLayout gbl;
   private GridBagConstraints gbc;
   private SimpleModel simpleModel;

   private String str;

   private static DelemeterSelector instance = null;

   private DelemeterSelector() {
      description = new JLabel(
            "What delemeter separates your fields? Select the appropreiate delemeter");

      tabBtn = new JRadioButton("Tab");
      tabBtn.addItemListener(new RadioAction());
      semicolanBtn = new JRadioButton("Semicolan");
      semicolanBtn.addItemListener(new RadioAction());
      commaBtn = new JRadioButton("Comma");
      commaBtn.addItemListener(new RadioAction());
      spaceBtn = new JRadioButton("Space");
      spaceBtn.addItemListener(new RadioAction());

      btnGroup = new ButtonGroup();
      btnGroup.add(tabBtn);
      btnGroup.add(semicolanBtn);
      btnGroup.add(commaBtn);
      btnGroup.add(spaceBtn);

      txtArea = new JTextArea(20, 70);

      scroll = new JScrollPane(txtArea);

      gbl = new GridBagLayout();
      gbc = new GridBagConstraints();

      this.setLayout(new GridLayout(3,1));

      // Creating the GUI
      this.add(description);
      this.add(radioPanel());
      this.add(scroll);      
   }

   private JPanel radioPanel() {
      JPanel panel = new JPanel();
      panel.setLayout(new FlowLayout());

      panel.add(tabBtn);
      panel.add(semicolanBtn);
      panel.add(commaBtn);
      panel.add(spaceBtn);

      panel.setBorder(BorderFactory
            .createTitledBorder("Choose the Delimeter that seperates your fields"));

      return panel;
   }

   //!!
   public void setModel(final SimpleModel simpleModel) {
      this.simpleModel = simpleModel;
      simpleModel.addPropertyChangeListener(new PropertyChangeListener() {

         @Override
         public void propertyChange(PropertyChangeEvent evt) {
            txtArea.append("File Text: " + simpleModel.getFileText() + "\n");            
         }
      });
   }

   public static DelemeterSelector getInstance() {
      if (instance == null) {
         instance = new DelemeterSelector();
      }

      return instance;
   }

   private class RadioAction implements ItemListener
   {
       public void itemStateChanged(ItemEvent ae)
       {
           if(ae.getStateChange()==ItemEvent.SELECTED){
           if(ae.getSource().equals(commaBtn))
           {
               str = ".";
           }
           else if(ae.getSource().equals(spaceBtn))
           {
               str = " ";
           }
           else if(ae.getSource().equals(semicolanBtn))
           {
               str = ";";
           }

           simpleModel.setDelemeter(str);}
       }
   }
}

class SimpleModel {
   public static final String FILE_TEXT = "file text";
   public static final String FILE_DELEMETER = "dELEMETERtext";
   private SwingPropertyChangeSupport pcSupport = 
      new SwingPropertyChangeSupport(this);
   private String fileText;
   private String str;

   public void addPropertyChangeListener(PropertyChangeListener listener) {
      pcSupport.addPropertyChangeListener(listener);
   }

   public void removePropertyChangeListener(PropertyChangeListener listener) {
      pcSupport.removePropertyChangeListener(listener);
   }

   public void setFileText(String fileText) {
      String oldValue = this.fileText;
      String newValue = fileText;
      this.fileText = fileText;
      pcSupport.firePropertyChange(FILE_TEXT, oldValue , newValue);

   }

   public String getFileText() {
      return fileText;
   }

   public void setDelemeter(String str)
   {
       String oldValue = this.str;
      String newValue = str;
      this.str = str;
      pcSupport.firePropertyChange(FILE_DELEMETER, oldValue , newValue);

      System.out.println(str);
   }

}

This code uses MVC design patters. It records the text of JTextField from the first sliding window. When user moves to the second window, it displays the text which you typed in first window, in JTextArea.

Now, click on any of the radio buttons allocated there. You will see that JTextArea is displaying the data which was typed in first window, even I am not calling that getFileText() method there. It seems like it is keep on calling getFileText() method and keep on updating the JTextArea whenever I click on a JRadioButton. But inside JRadioButton’s action class, I never called for such an action, I just set the delemeter!!

why this errors are happening? Please 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-06-14T22:35:27+00:00Added an answer on June 14, 2026 at 10:35 pm

    Your problem is this line:

      simpleModel.addPropertyChangeListener(new PropertyChangeListener() {
    
         @Override
         public void propertyChange(PropertyChangeEvent evt) {
            txtArea.append("File Text: " + simpleModel.getFileText() + "\n");            
         }
      });
    

    In conjunction with these:

     public void setFileText(String fileText) {
          String oldValue = this.fileText;
          String newValue = fileText;
          this.fileText = fileText;
          pcSupport.firePropertyChange(FILE_TEXT, oldValue , newValue);
    
       }
    
       public void setDelemeter(String str)
       {
           String oldValue = this.str;
          String newValue = str;
          this.str = str;
          pcSupport.firePropertyChange(FILE_DELEMETER, oldValue , newValue);
    
          System.out.println(str);
       }
    

    You’re not inspecting the name of the property getting fired (in the first set of code), so when setFileText or setDelemeter is called, the firePropertyChange will execute and display the file name.

    The easiest solution would be to check the name of the property like so:

      simpleModel.addPropertyChangeListener(new PropertyChangeListener() {
    
         @Override
         public void propertyChange(PropertyChangeEvent evt) {
            if(evt.getPropertyName().equale(SimpleModel.FILE_TEXT)
                txtArea.append("File Text: " + simpleModel.getFileText() + "\n");            
         }
      });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please have a look at the following code import java.awt.event.*; import javax.swing.*; import java.awt.*;
Please have a look at the following code import java.awt.*; import java.awt.event.*; import javax.swing.*;
Please have a look at the following code package email; import java.awt.*; import java.awt.event.*;
Please have a look at the following code. package normal; import java.awt.*; import java.awt.event.*;
Please have a look the following code import javax.swing.*; import java.awt.event.*; import java.awt.*; public
Please have a look at the following code. import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.*;
Please have a look at the following code Main.Java import java.awt.event.*; import java.awt.*; import
Please have a look at the following code WizardPanel.java package wizardGUI; import java.awt.*; import
Please have a look at the following code package Euler; import java.util.ArrayList; import java.util.List;
Please have a look at the following code import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout;

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.