0

I am learning java and starting with the concepts of JPanels. currently the program i am testing with consists of two windows. the first window contains one button. on clicking the button the second window containing a numpad opens up. now the question i have is 1. how can i return the number i have selected by clicking the numpad back to the parent panel? I need to use the method for the ease of accepting integers. 2. how do i restrict the length of the JTextField? say i need to accept only two characters... 3. how do i transfer back the control from the child panel to the parent panel by clicking a button along with the data ofcourse? 4. how can i design the child panel so that it does not show the header bar along with the close button?

Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46
Prasad S.
  • 3
  • 1
  • 3
  • for 1: Create an attribute `number` in the `child` class that is accessible to the `parent` class through a getter function. You can alter the attribute in the child class whenever the numpad is used. 2. will involve layouts/preferred size of JTextFields and is generally a pain to deal with. Try setting the text to two spaces and see if it sets it automatically for you. 3. Not sure what you mean by this, GUIs aren't sequential code so there's not necessarily `control` that needs to be transferred. You create events to do certain things when they're triggered. – Mdev Apr 10 '14 at 04:59
  • as for 4 check out this answer: http://stackoverflow.com/a/8701948/2142219 – Mdev Apr 10 '14 at 05:01

1 Answers1

0

How can i return the number i have selected by clicking the numpad back to the parent panel? I need to use the method for the ease of accepting integers.

You could:

  • Have a property in the parent class with the appropriate setter.
  • When you call the child frame, pass an instance of the parent frame to the child frame.
  • When you are done with your child frame, use the parent instance to change the property described in step 1 and call dispose() on the frame. This should allow your parent frame to have the value which was selected in the child frame.

How do i restrict the length of the JTextField? say i need to accept only two characters

You will need to create something like this since it seems that Java does not provide such functionality out of the box.

How do i transfer back the control from the child panel to the parent panel by clicking a button along with the data ofcourse?

There seems to be two options for this:

How can i design the child panel so that it does not show the header bar along with the close button

You will need to take a look at the setUndecorated(boolean bool)

Community
  • 1
  • 1
npinti
  • 51,780
  • 5
  • 72
  • 96