I have 2 frames (classes exactly) customer & items...

when a button clicked (button 'BUY') on customer frame it navigates to items frame.
mean time i made customer frame customer.setEnabled(false) .
i called to item frame by simply creating an object of item class.

then , when i coming back to customer frame from item frame.. (i can't simply create an object, i am recalling the disabled frame..) so i have to make customer.setEnabled(true)

how can i do it.....?

i simply called customer.setEnabled(true) with in the item class.
it doesn't work.

please help...

Dani AI

Generated

Quick diagnosis: the usual cause is that the code in the items window is not calling setEnabled(true) on the same Customer frame instance you originally disabled, or it’s being called at the wrong time/thread. Using two top-level JFrames makes this fragile. 's idea of a single-frame UI is solid; for a modal “stop everything until the user finishes items” behavior, prefer a modal JDialog. If you must keep two windows, hold a reference to the original frame and re-enable it on the child window's close event (always on the EDT).

A simple modal-dialog approach (no manual enable/disable needed):

JDialog d = new JDialog(parentFrame, "Items", Dialog.ModalityType.APPLICATION_MODAL);
d.getContentPane().add(new ItemsPanel()); // your UI
d.pack();
d.setLocationRelativeTo(parentFrame);
d.setVisible(true); // blocks until dialog is closed; parent returns active automatically

If you keep a separate JFrame, pass the parent reference and re-enable it when the items window closes:

public class ItemsFrame extends JFrame {
    private final JFrame parent;
    public ItemsFrame(JFrame parent) {
        this.parent = parent;
        parent.setEnabled(false);
        addWindowListener(new WindowAdapter() {
            @Override public void windowClosed(WindowEvent e) {
                SwingUtilities.invokeLater(() -> parent.setEnabled(true));
            }
            @Override public void windowClosing(WindowEvent e) { dispose(); }
        });
    }
}

Troubleshooting tips: verify you passed the exact same instance (e.g. new ItemsFrame(this) from the customer frame), and perform all Swing changes on the EDT (SwingUtilities.invokeLater). Avoid multiple JFrames where possible—use CardLayout or a modal JDialog for cleaner, more predictable UI behavior. Thanks to for the panel idea and for confirming it helped.

Recommended Answers

All 2 Replies

1) Have you considered doing something like this:

package snip.swing.frame;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Example extends JFrame implements ActionListener {
    /** Static data */
    private static final int DIM_X = 300;
    private static final int DIM_Y = 300;
    private static final String TITLE = "Example";

    /** 1-customers panel active */
    /** 2-items panel active */
    private static int state = 1;

    /** Components */
    private JPanel cPanel; // Customers panel
    private JLabel cLabel;
    private JPanel iPanel; // Items panel
    private JLabel iLabel;
    private JButton toggleButton;

    public Example() {
	setTitle(TITLE);
	setLayout(new FlowLayout());

	Dimension dim = new Dimension(DIM_X, DIM_Y);
	setSize(dim);

	cPanel = new JPanel();
	cLabel = new JLabel("Panel: Customers is now visible");
	cPanel.add(cLabel);
	/* other components related to this panel go here */
	this.add(cPanel);
	cPanel.setVisible(true);

	iPanel = new JPanel();
	iLabel = new JLabel("Panel: Items is now visible");
	iPanel.add(iLabel);
	/* other components related to this panel goes here */
	this.add(iPanel);
	iPanel.setVisible(false);

	toggleButton = new JButton("toggle");
	toggleButton.addActionListener(this);
	add(toggleButton);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {
	/** Alternate views when pressing the button */
	System.out.println("Action");
	if (state == 1) {
	    state = 2;
	    cPanel.setVisible(false); /* make customers panel invisible */
	    iPanel.setVisible(true);
	} else {
	    state = 1;
	    iPanel.setVisible(false); /* make items panel invisible */
	    cPanel.setVisible(true);
	}
    }

    public static void main(String args[]) {
	Example e = new Example();
	e.setVisible(true);
    }
}

You can have one frame, with two different panels: customers and items. The two panels will "share" the same object and constructs as the main frame, but will be able to act individually.
Help: google "JPanel tutorial"

2) Instead setEnabled, you cand try setVisible

Thanx
it helps me to solve my other problem.

pawan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.