Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2007

Tip: Looking for answers? Try searching our database.

Question: jFranme and jDialog

Thread view: 
WhoWhatWhen - 05 Dec 2007 15:18 GMT
Given a jFrame that also contains a checkbox and a jDialog.

The jDialog has been made visible and is modal.

Is there a way to access the value of the checkbox on the parent
jFrame from the jdialog?

Many thanks,

Ken B.
RedGrittyBrick - 05 Dec 2007 16:17 GMT
> Given a jFrame that also contains a checkbox and a jDialog.
>
> The jDialog has been made visible and is modal.
>
> Is there a way to access the value of the checkbox on the parent
> jFrame from the jdialog?

There are many many ways

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class FrameDialog implements ItemListener {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new FrameDialog();
            }
        });
    }

    private JCheckBox checkbox;

    private JFrame frame;

    FrameDialog() {
        checkbox = new JCheckBox("Fries", false);
        checkbox.addItemListener(this);
        JPanel p = new JPanel();
        p.add(checkbox);

        frame = new JFrame("Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(p);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    } // constructor

    public void itemStateChanged(ItemEvent e) {

        final JDialog d = new JDialog(frame, "Dialog");

        JButton b = new JButton("True");
        b.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                d.setVisible(false);
                d.dispose();
            }
        });

        JPanel p = new JPanel();
        p.add(new JLabel(checkbox.isSelected() ? "Fatso" : "Beanpole"));
        p.add(b);

        d.add(p);
        d.pack();
        d.setLocationRelativeTo(frame);
        d.setVisible(true);

    }

}
WhoWhatWhen - 05 Dec 2007 17:25 GMT
Thank you but that is not exactly the solution to the question as
asked:  In your solution the jdialog is opened by checing the
checkbox.  

I need to be able to ascertain from within the dialog box what the
value of the checkbox is.

For example, in VB6 I would say from another window:
if (frmMain.chkbox1.value=vbchecked) then
    blah blah blah
endif

Again, thank you.

Ken B.

>> Given a jFrame that also contains a checkbox and a jDialog.
>>
[quoted text clipped - 69 lines]
>
>}
RedGrittyBrick - 05 Dec 2007 20:45 GMT
> Thank you but that is not exactly the solution to the question as
> asked:  In your solution the jdialog is opened by checing the
> checkbox.  

That has no bearing on the way the JDialog constructor accesses the
JCheckBox. It would be the same whether a JMenuUtem or JButton or
Exception handler invoked the JDialog constructor.

> I need to be able to ascertain from within the dialog box what the
> value of the checkbox is.

The code that creates the dialog refers to checkbox
   p.add(new JLabel(checkbox.isSelected() ? "Fatso" : "Beanpole"));
If that's not the sort of thing you want to do, you should explain in
more detail.

> For example, in VB6 I would say from another window:
> if (frmMain.chkbox1.value=vbchecked) then
>     blah blah blah
> endif

You can do it that way too.
  if (frame.checkbox.isSelected()) {
     blah blah blah;
  }

It's a question of the scope of either the variable which refers to the
JFrame instance or the scope of the variable that refers to the
JCheckBox instance. Or both.

Alternatively you could pass a reference to either of these variables
into the method or constructor that creates the JDialog instance.

I'd prefer something like ...

class Whatever {
   Whatever() {
       ...
       JButton dialogInvokingButton = new JButton("Click me");
       dialogInvokingButton.addActionListener(this);
       ...
       frame.setVisible(true);
   }

   void actionPerformed(ActionEvent e) {
       new DialogCreatingClass(frame, checkbox.isSelected());
   }
}

class DialogCreatingClass {
    DialogCreatingClass(JFrame frame, boolean likesFries) {
        JDialog d = new JDialog(frame, ...);
        ...
        if (likesFries)
        ...
        d.setVisible(true);
    }
}

There's zillions of ways to do what you describe.

What I posted before was an SSCCE (Google will explain). Maybe you
should construct one that better illustrates your question and post it.

A: It disturbs the natural flow of conversation
Q: Why is that?
A: Top-posting
Q: What irritates many comp.lang.java.programming readers?
Jeff - 08 Dec 2007 15:56 GMT
> Given a jFrame that also contains a checkbox and a jDialog.
>
[quoted text clipped - 6 lines]
>
> Ken B.

If I understand correctly - the object based on a jFrame is the parent
object of a jDialog, and you wish the behavior of the jDialog to vary
based on the value of a checkbox on the jFrame.

There are indeed several solutions. One is to push the information to
the dialog. For example, to display a simple AboutBox dialog that I
display with:
       AboutBox AboutBoxDlg =  new AboutBox(this, true);
       AboutBoxDlg.setSize(450,300);
       AboutBoxDlg.setVisible(true);
You can add a public variable to the definition of the AboutBox class
and set that variable value before the setVisible call.

Alternatively, pull the information. Make sure the checkbox value is
copied to a public value (example Boolean MyValue). Then pull the
information from the dialog with:

Boolean CheckBoxValue = parent.MyValue;

Since the dialog is passed a reference to the parent in its
constructor, you have access to the jFrame through the parent variable.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.