Subclass of abstract JDialog - NullPointerException

Hello!
I wanted to make an abstract subclass of JDialog that will handle a few things that are common to several of the dialogs in my program. Things like creating and setting up the Ok/Cancel buttons, handling the case where the user closes the dialog with the red X as though they clicked Cancel, etc. The method that is called when the Ok button is clicked is abstract and is supposed to be implemented by the subclasses.
The problem is, when the subclass implements that ok() method and tries to access one of its own instance variables (a JTextField), it throws a NullPointerException. I'm certain this JTextField instance was created, because it was added to the dialog's content pane; I can see it and type text into it. The reference is also visible across method calls after it's created. But when the Ok button is clicked and the method tries to access it, it's always null for some reason that I can't figure out. I tried refactoring the inner classes to their own class files, but it gave me the same results.
Here's a SSCCE. I tried to keep it as short as possible. The comments should highlight the relevant parts of the code. I can do this a few other ways, so I'm not really looking for a JDialog tutorial or anything. I'm mainly just curious why this doesn't work. Can anyone tell me what I'm doing wrong here?
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Main {
     // This creates and shows JFrame with a button.  Clicking the
     // button displays a SomeDialog instance that prompts the user
     // to enter a String value.
     public static void main(String[] args) {
          final JFrame frame = new JFrame();
          final JButton btnShowDialog = new JButton("Show Dialog");
          frame.setSize(340,280);
          frame.setLocationRelativeTo(null);
          frame.getContentPane().add(btnShowDialog, BorderLayout.SOUTH);
          frame.addWindowListener(new WindowAdapter() {
               public void windowClosing(WindowEvent e) {
                    frame.dispose();
          btnShowDialog.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                    SomeDialog dlg = new SomeDialog(frame);
                    dlg.setVisible(true);
                    if (dlg.isCancelled() == true) {
                         System.out.println("Cancelled");
                    } else {
                         String value = dlg.getValue();
                         System.out.println("Value: "+value);
          EventQueue.invokeLater(new Runnable() {
               public void run() {
                    frame.setVisible(true);
     // This is intended to provide some basic dialog behavior.
     private static abstract class AbstractDialog extends JDialog {
          private boolean cancelled = false;
          private JButton btnOk = new JButton("Ok");
          private JButton btnCancel = new JButton("Cancel");
          protected abstract void initComponents();
          protected abstract void ok();
          public AbstractDialog(JFrame parent) {
               super(parent, true);
               addWindowListener(new WindowAdapter() {
                    public void windowClosing(WindowEvent e) {
                         cancelled = true;
               // This call to initComponents() should cause the subclass to
               // create its txtValue instance long before the Ok button is
               // clicked.
               initComponents();
               pack();
               setLocationRelativeTo(parent);
          protected JPanel buildButtons() {
               btnOk.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                         cancelled = false;
                         ok();
               btnCancel.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                         cancelled = true;
                         dispose();
               JPanel pnlGrid = new JPanel(new GridLayout(1,2,5,5));
               pnlGrid.add(btnOk);
               pnlGrid.add(btnCancel);
               return pnlGrid;
          public boolean isCancelled() {
               return cancelled;
     // This implements AbstractDialog to prompt the user for a
     // String value.
     private static class SomeDialog extends AbstractDialog {
          // txtValue is a class-level instance in the subclass.
          private JTextField txtValue = null;
          private String value = null;
          public SomeDialog(JFrame parent) {
               super(parent);
          @Override
          protected void initComponents() {
               // The class-level JTextField instance is created here.
               // I would expect this instance to persist throughout the
               // life of this dialog.
               txtValue = new JTextField();
               getContentPane().add(txtValue, BorderLayout.NORTH);
               getContentPane().add(buildButtons(), BorderLayout.SOUTH);
               // This check is to make sure that the txtValue reference
               // is non-null across method calls.  It always prints
               // "Not null"
               checkNull();
          private void checkNull() {
               if (txtValue == null) {
                    System.out.println("Null");
               } else {
                    System.out.println("Not null");
          @Override
          protected void ok() {
               // *** The NullPointerException is thrown at this line: ***
               value = txtValue.getText();
               dispose();
          public String getValue() {
               return value;
}

The problem is that the text field is created, but then you set it to null in the derived class. This may seem counter-intuitive at first, but remember that the super-class constructor always runs first, and then additional initialization takes place in the derived class. You have the following:
          // txtValue is a class-level instance in the subclass.
          private JTextField txtValue = null;
          private String value = null;
          public SomeDialog(JFrame parent) {
               super(parent);
          }First super(parent) is called, and your text field is created through the call to initComponents(). Then the initialization in the SomeDialog class takes place, and there you set txtValue to null.
There are two bad practices contributing to this confusing situation:
1. You are calling an overridden method from a constructor. If used carefully this is not a problem, if used carelessly you end up wasting hours tracking down mysterious bugs (like this one).
2. You are explicitly initializing instance variables to null. This is always completely unnecessary, and in this case it actually causes your program to fail.
In short, to get rid of this problem, do not set txtValue explicitly to null:
          // txtValue is a class-level instance in the subclass.
          private JTextField txtValue;
          private String value;
          public SomeDialog(JFrame parent) {
               super(parent);
          }

Similar Messages

  • Enforce setting private variable in subclass with abstract method

    Hi,
    Is this something that is common usage/practice?:
    public abstract class Base {
        private int importantPrivateVariable = setImportantPrivateVariable();
        protected abstract int setImportantPrivateVariable();
    }I would like to enforce the extender of the class to set a private variable, but as there is no abstract variable in java, I can only use a method for it.
    Thanks,
    lemonboston
    Edit: the variable could be protected as well, I suppose this is not important here, but correct me if I am wrong

    lemonboston wrote:
    Hi,
    Is this something that is common usage/practice?:I don't think that's so common, but that's code easily understandable. However there are several problems with this approach:
    public abstract class Base {
    private int importantPrivateVariable = setImportantPrivateVariable();
    protected abstract int setImportantPrivateVariable();
    }I would like to enforce the extender of the class to set a private variableThat's no what your code implements: your base class forces the subclasses to return an int value, and the Base class uses that value to assign it to the variable.
    Therefore the method should be called get<Something> (e.g. <TT>getInitialValueOfImportantVariable()</TT>+ to have a naming consistent with its signature (it returns a value, whereas a regular setter method should declare a void return type: <TT>protected abstract void setImportantPrivateVariable(int someValue);</TT>).
    Edit: the variable could be protected as well, I suppose this is not important here,Well, yes, this is "important" - at least, there a noticeable difference: the variable being private, the base class is free to handle it as it sees fit (e.g., assign the value at construction time and never modify it afterwards). If the variable was protected, the subclass could modify in ways and at times not known by the base class.
    but correct me if I am wrongThere's a trap in this construct: the method is called in the variable initializer, that is, behind the scenes, approximately during the execution of the Base class constructor, so before the subclass constructor. So, you are calling a method on an object that is not fully initialized (e.g. some of its attributes may still be <TT>null</TT> at this stage). There is a rule that discourages such situations, that goes something like "don't call non-private and non-final methods from a constructor".
    To avoid this trap, two options:
    - require an int argument in the Base class's constructor , as was suggested above
    - don't get and set the value of the important variable in the initializer or constructor code, but from a special method in the base class instead:
    public abstract class Base {
        private int importantPrivateVariable; // default value is zero
    // or alternatively:
    //    private int importantPrivateVariable = ...; // Some default value
        protected abstract int getImportantPrivateVariable();
        public void initializeImportantPrivateVariable() {
            importantPrivateVariable = getImportantPrivateVariable();
    }That construct is a degenerate form of a common design pattern known under the name of Template Method (where a base class method calls generally several subclass methods in a specified order and with a specified chaining, leaving it to the subclass to implement the details of the methods).
    The drawback is that the client code (the one that uses the Base instance) has to know when to call that initialization method, whereas the constructor-based initialization lets the client code free to not care at all.
    Much luck,
    J.

  • Superclass and subclass and abstract method

    Hi all,
    I am a little bit confused about abstract methods.
    I define a superclass without abstract keyword and an abstract method inside the superclass. Then I write a subclass to extend the superclass.
    When I compile the source code I get error. It looks like I can't initiate an instance from the supercalss and/or subclass. If I put abstract key word in the superclass definition there is no problem at all. So my question: does abstract method needed to be defined in an abstract superclass only?
    Thank for you input.

    Abstract methods can only be declared in an abstract class.

  • Problem subclassing a modal JDialog

    Hi, I've run into a problem when creating a subclass of a class which itself extends JDialog. The problem is this:
    I have a class, lets call it SuperDialog, which extends JDialog. In the constructor, I set it to be a modal dialog. This works fine.
    However, when I try to create a subclass of SuperDialog, (lets call it SubclassDialog), this is where I run into difficulty. The problem is that the constructor for the SubclassDialog seems to get stuck when it calls on the constructor of SuperDialog, and does not do the rest of the constructor, which is really wierd, and I can't understand why.
    For example, if the constructor for the SubclassDialog is something like this:
    public SubclassDialog()
         super();   // The constructor gets stuck here
         do something here      // This code does not get executed
    }The 'do something here' code is not executed. The problem disappears if the SuperDialog is set to NOT modal. Can anyone explain to me why, and how to get round this problem? Here's a test program to illustrate what I'm on about. If you run the program, you will see the subclass dialog window appear containing the text "this is the superclass dialog" - the text SHOULD read "this is the subclass-dialog", because this was set in the subclass dialog constructor. Thanks for any help. James
    import javax.swing.*;
    import java.awt.Color;
    public class ModalDialogTest extends JFrame
        public static void main(String[] args)
            ModalDialogTest test = new ModalDialogTest();
            test.run();
        public void run()
            class SuperclassModalDialog extends JDialog
                protected JTextField text = new JTextField("This is the superclass dialog");
                public SuperclassModalDialog(JFrame frame)
                    super(frame, "This is the superclass dialog", true); 
                    // Setting this to 'false' eliminates the problem
                    frame.setVisible(true);
                    add(text);
                    pack();
                    setVisible(true);
            class SubclassModalDialog extends SuperclassModalDialog
                public SubclassModalDialog(JFrame frame)
                    super(frame);
                    text.setText("This is the SUBCLASS dialog");
            SubclassModalDialog dialog = new SubclassModalDialog(this);
    }

    Ah right, I see! Thanks a lot for that, solves my problem. Duke stars on their way!
    BTW, I'm curious why you would never subclass JDialog. I find that subclassing JDialog is a convenient way to create a pop-up window which blocks all other windows until it is closed - is there an easier / better way to do this?
    Thanks again,
    James

  • Invoking method of final subclass of abstract class is slow

    hi,
    I've diagnosed a performance regression in my code which boils down to the example at the end of the post.
    Would it be possible to have the vm dynamically figure out that within the hotspot in the test method, its
    safe to ditch the dynamic dispatch and skip that overhead?
    I thought marking the method as final would allow the vm to peform some run-time optimisations - I'm pretty sure I've been able to measure that in different situations; marking the test method's parameter (a) as final, also doesn't help.
    Rather than have an abstract class, its looking like the performance requirements will dictate some copy and paste to
    help the vm along - does anyone know if I'm missing something here?
    thanks in advance,
    asjf
    The output I see (using 6.0 update 10) is:
    B foo 28
    B bar 95
    C foo 29
    C bar 86
    D foo 26
    D bar 92
    E foo 29
    E bar 82
    public class MethodCallPerfTest {
         public static void main(String[] args) {
              test(new B());
              test(new C());
              test(new D());
              test(new E());
         public static void test(A a) {
                   long start= System.currentTimeMillis();
                   for(int i=0; i<8000000; i++)
                        a.foo();
                   System.out.println(a.getClass().getSimpleName()+" foo "+(System.currentTimeMillis() - start));
                   long start= System.currentTimeMillis();
                   for(int i=0; i<8000000; i++)
                        a.bar();
                   System.out.println(a.getClass().getSimpleName()+" bar "+(System.currentTimeMillis() - start));
    abstract class A {
         int j;
         final void foo() {j++;}
         abstract void bar();
    final class B extends A {
         int k;
         void bar() {k++;}
    final class C extends A {
         int l;
         final void bar() {l++;}
    class D extends A {
         int m;
         final void bar() {m++;}
    class E extends A {
         int n;
         void bar() {n++;}
    }

    is it because references to the final fields are resolved at compile-time ?
    such that the compiler must know the value of USE in:if(USE){
      // a
    // b at compile time so it can remove either "b" or "a". I think so. hmm.

  • Access overriden method of an abstract class

    class Abstract
    abstract void abstractMethod(); //Abstract Method
    void get()
    System.out.print("Hello");
    class Subclass extends Abstract
    void abstractMethod()
    System.out.print("Abstract Method implementation");
    void get()
    System.out.print("Hiiii");
    In the above code, i have an abstract class called "Abstract", which has an abstract method named "abstractMethod()" and another method called "get()".
    Now, this class is extended by "Subclass", it provides implementation for "abstractMethod()", and also overrides the "get()" method.
    Now my problem is that i want to access the "get()" method of "Abstract" class. Since it is an abstract class, i cant create an object of it directly, and if i create an object like this:
    Abstract obj = new Subclass();
    then, obj.get() will call the get() method of Subclass, but how do i call the get() method of Abstract class.
    Thanks in advance

    hey thanks a lot,, i have another doubt regarding Abstract classes.
    i was just trying something, in the process, i noticed that i created an abstract class which does not have any abstract method, it gave no compilation errors. was wondering how come this is possible, and what purpose does it solve?

  • Abstract classes, Interfaces, and concrete classes

    I have another technical interview tomorrow and somehow I keep getting asked the same question and I feel my answer is not really up to par. The question is:
    "What is the advantage of subclassing an abstract class versus concrete class?"
    "What is the difference of using an interface versus an abstract class, which is better to use?"
    For the first question, I usually answer performance is the advantage because you don't have to instantiate the class.
    For the second question, I usually say that you can put implementation in an abstract class and you can't in an interface. I really can't answer the second part to this question.
    Any ideas?

    For the first question, I usually answer performance
    is the advantage because you don't have to instantiate
    the class. Try invoking the class B in the following somewhere in another class.
    abstract class A{
       A(){
          System.out.println("abstract instantiated");
    class B extends A{
      B(){super();}
    }

  • Problems with createRow() method

    I have a basic problem when creating a new row in my database. what I do is the following:
    1.ViewObject blobVO = (ViewObject) myAM.findViewObject ("BlobVO");
    2.BlobVORowImpl newRow = (BlobVORowImpl) blobVO.createRow();
    Line 1. is ok; it finds the view object. But when I get to line 2., I get the following error:
    oracle.jbo.RowCreateException: JBO-25017: Error while creating a new entity row for BlobEO.
         oracle.jbo.server.EntityImpl oracle.jbo.server.EntityDefImpl.createBlankInstance(oracle.jbo.server.DBTransaction)
              EntityDefImpl.java:1033
         oracle.jbo.server.EntityImpl[] oracle.jbo.server.ViewRowImpl.createMissingEntities(int[], oracle.jbo.server.EntityImpl[], java.sql.ResultSet, oracle.jbo.AttributeList)
              ViewRowImpl.java:1417
         oracle.jbo.server.ViewRowImpl oracle.jbo.server.ViewRowImpl.init(int[], oracle.jbo.server.EntityImpl[], oracle.jbo.server.QueryCollection, java.sql.ResultSet, oracle.jbo.AttributeList)
              ViewRowImpl.java:227
         oracle.jbo.server.ViewRowImpl oracle.jbo.server.ViewDefImpl.createBlankInstance(int[], oracle.jbo.server.EntityImpl[], oracle.jbo.server.ViewObjectImpl, oracle.jbo.server.QueryCollection, java.sql.ResultSet, oracle.jbo.AttributeList)
              ViewDefImpl.java:1029
         oracle.jbo.server.ViewRowImpl oracle.jbo.server.ViewDefImpl.newInstance(int[], oracle.jbo.server.EntityImpl[], oracle.jbo.server.ViewObjectImpl, oracle.jbo.server.ViewRowSetImpl, oracle.jbo.AttributeList)
              ViewDefImpl.java:930
         oracle.jbo.server.ViewRowImpl oracle.jbo.server.ViewObjectImpl.createInstance(oracle.jbo.server.ViewRowSetImpl, oracle.jbo.AttributeList)
              ViewObjectImpl.java:1916
         oracle.jbo.server.RowImpl oracle.jbo.server.QueryCollection.createRowWithEntities(int[], oracle.jbo.server.EntityImpl[], oracle.jbo.server.ViewRowSetImpl, oracle.jbo.AttributeList)
              QueryCollection.java:728
         oracle.jbo.Row oracle.jbo.server.ViewRowSetImpl.createRowWithEntities(int[], oracle.jbo.server.EntityImpl[], oracle.jbo.AttributeList)
              ViewRowSetImpl.java:1386
         oracle.jbo.Row oracle.jbo.server.ViewRowSetImpl.doCreateAndInitRow(oracle.jbo.AttributeList)
              ViewRowSetImpl.java:1432
    I searched for error JBO-25017, and I got this:
    JBO-25017: RowCreateException
    Cause: An unexpected exception occurred while creating a new entity instance.
    Action: The entity may not have a public default constructor. Fix the cause for InstantiationException or IllegalAccessException that appears in the details of this exception.
    I'm still clueless.. any idea

    Andy:
    I can think of three possibilities:
    1. Your custom EntityImpl subclass does not have a default constructor (the constructor that takes no argument). If that's the case, add the default cons.
    2. Your custom EntityImpl subclass is abstract.
    3. If neither #1 nor #2 is the case, take a look at your EntityImpl subclass to see if there could be problems creating an instance of it. In particularly, try executing the following line of Java code
    new <your-class>();
    to see if that gives you any more detailed explanation.
    Thanks.
    Sung

  • Design patterns for Dynamic Class Loading

    Hi,
    I have to develop a program for uni that dynamically loads classes based on names in a text file. All the classes subclass an abstract class with a constructor that takes one argument. This means I can't use the Class.forName(className).newInstance() method of class loading. I have researched some design patterns I could use and have come up with the following 3:
    Factory pattern; "Robocode" pattern (not it's real name, but Robocode uses it); and, the "one I made up myself" pattern.
    The robocode pattern instantiates a class using the default no-argument constructor then immediately sets all properties that shoud have been provided in the constructor:
    Object o = Class.forName(myClass).newInstance();
    o.setProperty(property);Personally I think this is ugly and a cheap fix instead of doing it properly.
    My own pattern finds the constructor that takes the arguments I need then calls it to create the object:
    Class c = Class.forName(myClass);
    Constructor cons = c.getConstructor(new Class[]{Class.forName("java.lang.String")});
    Object o = cons.newInstance(new Object[]{"hello"});What's the best to use? Are there any other patterns I should consider?

    My own pattern finds the constructor that takes the
    arguments I need then calls it to create the object:
    Class c = Class.forName(myClass);
    Constructor cons = c.getConstructor(new
    Class[]{Class.forName("java.lang.String")});
    Object o = cons.newInstance(new Object[]{"hello"});
    I have followed this basic 'pattern' several times though I would use
    Constructor cons = c.getConstructor(new Class[]{String.class});
    It works great.

  • Apropreate Throwable on contract violation?

    I'm just wondering, can anyone suggest how to deal with an implementation returning an incorrect value.
    Here's a simple example to show what I mean. Imagine I wanted to make an abstract class which allows you to roll dice and return the sum of the rolls, but the action of rolling one die is implemented by subclass.
    public abstract class DiceRoller {
         * Returns a dice roll (a number randomly chosen from 1 to 6)
         * @return a number from 1 to 6
        protected abstract int rollOneDie();
         * Returns a the sum of two dice rolls.
         * @return a number from 2 to 12
        public final int rollTwoDice() {
            int die1 = rollOneDie();
            int die2 = rollOneDie();
            if (die1 < 1 || die1 > 6 || die2 < 1 || die2 > 6) {
                throw new AssertionError("implementation of rollOneDie() did something bad!");
            return die1 + die2;
    }Is AssertionError apropreate here? I don't think IllegalArgumentException would be right because that would imply the programmer made a mistake in calling rollTwoDice(). Does anyone know what the convension should be in this case?
    This isn't actually what I'm trying to code, but I thought I'd come up with a simpleexample rather than trying to explain my program.

    I may not have explained correctly what I'm asking. I'm asking about this from an API point of view, where you would compile the abstract class DiceRoller withuot a concrete implementation, and then release it for clients to implement their own rollOneDie(). I want rollTwoDice() to check incase they screw up the implementation it's better to throw an exception at that point than let the program continue. (fast-fail) Yeh I could just miss out the test and hope the client reads the javadoc for the abstract method they are implementing, and if not that they spot their mistake.
    Imagine for instance the client implemented rollOnedie() as such:
    @Override
    protected int rollOneDie() {
        return (new Random()).nextInt(6); // OOPS! That return 0 to 5!!! Should be 1 to 6.
    }Then they unit test rollOneDie() a few times and it returns 3, 4, 2, 2, 4 and assume it to be okay, they they put it into their real application and it sometimes returns 0, meaning that rollTwoDice() (through no fault of it's own) can return 0 or 1 and can't return 11 or 12! But if I have the assertion in place, then when their implementation returns 0, they get an assertion error, find a stack trace and friendly message explaining that their rollOneDie() returned 0, outside of the expected range 1-6. e.g. a nicer version of what I suggested might look like this:
    int die1 = rollOneDie();
    int die2 = rollOneDie();
    if (die1 < 1 || die1 > 6) {
        throw new AssertionError("A call to rollOneDie() returned "
                + die1 + ", expected number in the range 1 to 6!");
    } else if (die2 < 1 || die2 > 6) {
        throw new AssertionError("A call to rollOneDie() returned "
                + die2 + ", expected number in the range 1 to 6!");
    } else {
        return die1 + die2;
    }- My unit testing rollTwoDice() wont be applicable here as the real implementation of the abstract method is, yes you can unit test rollTwoDice() to see how it behaves with a dummy implementation of rollOneDie(), but no ammount of testing rollTwoDice() can prevent the API client from writing an incorrect implementation of rollOneDie();
    - I wouldn't want to use the assert keyword here because that's strictly for checking returns of private implimentations and cases which can never happen. Plus it can be disabled, which I don't want.
    - IllegalArgumentException is defined as +"Thrown to indicate that a method has been passed an illegal or inappropriate argument."+ and and illegal arguments have been passed to anything here.
    - enums are a good idea for the dice problem but the real problem returns a number between 0L to Long.MAX_VALUE, not just numbers 1 to 6, besides even if this were the real problem what would you do if rollOneDie() returned null?
    Anyway, my question is, is AssertionError apropreate to use here?

  • MouseEvent getPoint() problem

    I have two JFrame windows in an application each of which has a JPanel listening to mouse events. In one the events's getPoint returns the click location relative to the clicked component, but in the other, it returns the click location relative to the top-left corner of the parent JFrame (e.g., a click at the top of the component does not have y=0 and the location shifts down if a menu bar is added to the window). I think they both used to be relative to the clicked component, but the result has recently changed. I cannot find a change I made to cause the change.
    How is this possible?

    Torgil wrote:
    John_A_Nairn wrote:
    I was able to solve it myself. It turns out I had recently added a getParent() method to my JPanel subclass not realizing that this was overriding a Component method. It must be that the mouse event workings use this method to set the click location. Renaming my method solved the problem.This is the third post (that I've seen) in just the last month where a program has not worked because a Swing component was extended and a method from a superclass was overridden by mistake.
    When will people learn not to always automatically create a subclass of JPanel, JDialog, JFrame, etc as soon as they need a JPanel, JDialog, JFrame, etc?!? :-)I make that point all the time: don't subclass when you don't need to, and you only need to if you are overriding a method (and make sure you absolutely need to do that override!). Then people respond: I absolutely need to subclass JPanel (JFrame/JDialog/JInternalFrame) because it has-a JButton/JTextField/JLabel/... sigh So that's what "has-a" means these days?

  • Child frame problem

    My intention:
    I wrote a parent frame. in one of the memuItem action performed, the program
    create a child frame and try to get some input. after get the input, in the same action performed code, the parent will update its display pane using the data obtained from the child frame.
    my code:
    private void jMenuItemAddCActionPerformed(java.awt.event.ActionEvent evt) {
    //get the type of the carrier set
    String sort = getSort("AddC");
    if ((sort != null) && (sort.length() > 0)) { //this is a pop up dialog
    CarrierSet aSet = new CarrierSet(); //create an empty set
    aSet.setSort(sort);
    //let user enter the elements
    // UAEnterElements is a subclass of JFrame( I also tried with extending JDialog
    // it will add elements to aSet . there is a text field to let user enter stuff
    // a NEXT, DONE and CANCEL button
    UAEnterElements getCarrierD = new UAEnterElements(this, aSet);
    if(getCarrierD.getStatus() == 1){   //if user clicked done in UAEnterElements
    algebra.addCarrierSet(aSet);   // algebra is a private member of this
    // redisplay the content of _algebra              
    jLabelShowAlg.setText(_algebra.toString());
    My problem:
    my problem:
    the parent just goes ahead to update its display pane without the child returning.
    So it display old information.
    I tried to make the child as a subclass object of JDialog, but still not working.
    I wrote _parent.setEnabled(false) in the child constructor. The parent still just go
    ahead to display old information.
    how should I make the parent wait until the user click DONE and then execute
    if(getCarrierD.getStatus() == 1 {
    } Can I avoid implementing thread?
    Thanks a lot for any help.

    Thanks for your help. 2 Duke dollars is in your accout now.
    it worked when I wrote a class extending the JDialog class and setModal(true) .
    one more question related to this one: what if I still want the child be a frame,
    what should I do to make the parent wait till child done?

  • Default size and location, dialog boxes

    how can I set default size and location of dialog boxes? I have tried several things to no avail,
    thanks in advance

    1. You could create your own subclass of Dialog/JDialog with your defaults set automatically
    2. You could create a DialogFactory that creates Dialogs (pretty much the same as 1)

  • Why constructors

    Hi,
    I m notable to understand why constructors in Abstract class, when it is not possible to instansitate Abstract class.
    For example java.util.Calendar is a abstract class and it have constructor
    But i can not instantiat it why.
    Thanks in advance
    Badri

    Not really (referring to the above post). If that were true, the compiler could recognize that the class subclassed is abstract and not make the call to the abstract superclass's constructor.
    The reason is that abstract classes can have instance variables of their own and their constructors must initialize them so that the object state is consistent.

  • MouseEvent listening problem

    I have following problem to solve:
    I need to develop a custom dropdown component (forced using older jvm -on PPC) that has only java choice which is inadequate for purpose needed. I'm almost finished with it but i have problem with with this thing: i want for my popupmenu (custom made also) to catch all the mouse events for owner component (and its children) in order to hide the menu when they are clicked i.e. when you click on the dropdown a popupmenu is shown they you click somewhere on the panel (where dropdown resided) or some of it children, the popupmenu should disappear. So far so good, this is working nicely, I'm listening to the mouse events of these components in my popup but the problem is that if one of the children is a button it recives the clicked event too and performs the action for that button, while i would like for it to ignore that event if popup is visible. this is solved nicely in normal dropdown component (in swing or in awt choice) but i dont know how to do it without coding ugly stuff in all of the children components.
    Any tips??

    Torgil wrote:
    John_A_Nairn wrote:
    I was able to solve it myself. It turns out I had recently added a getParent() method to my JPanel subclass not realizing that this was overriding a Component method. It must be that the mouse event workings use this method to set the click location. Renaming my method solved the problem.This is the third post (that I've seen) in just the last month where a program has not worked because a Swing component was extended and a method from a superclass was overridden by mistake.
    When will people learn not to always automatically create a subclass of JPanel, JDialog, JFrame, etc as soon as they need a JPanel, JDialog, JFrame, etc?!? :-)I make that point all the time: don't subclass when you don't need to, and you only need to if you are overriding a method (and make sure you absolutely need to do that override!). Then people respond: I absolutely need to subclass JPanel (JFrame/JDialog/JInternalFrame) because it has-a JButton/JTextField/JLabel/... sigh So that's what "has-a" means these days?

Maybe you are looking for

  • ITunes no longer syncs correctly!!!

    I haven't had any complaints about the updates - till now.    I tried to add PDF's to my iBooks and add or delete things from my iPad for work and home.  Apps seem to work, music sort of works.   But the arrangement for books and video is not the sam

  • Dead iBook G3

    A friend gave me her 500mhz iBook G3 to play with. Upon startup I get the missing system folder icon. I tried putting in an old G4 Restore CD and it begins to load into OS 9 but encounters a bus error and can not continue. I also tried my Tiger insta

  • Excise Value to Upload in Material Cost.

    Hi Guru's Our requirement is to add all the excise value to material cost. For that we have created new pricing procedure and it works ok as far as basic and education cess loading into material but this does not work for Higher Education Cess. Apart

  • IDE location in java Sneak Preview 04 SP16

    Hi, Could somebody specify me which one of the Sneak Preview archive files(1.9gB or 1.7gB) contains IDE. I mean NWDI. Thank you. wmminsk.

  • Home.uix file not found for Enterprise Manager 10g R1

    I installed Oracle 10 R1, Enterprise manager was not working. It was giving error 404 file not found. I checked the error log, found that home.uix file was not found. So is there any source from net to get any uix missing files. or what else could b