Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Swing

  Asked By: Jason    Date: Feb 11    Category: Java    Views: 932
  

I am working on a swing & suppose to do drag & drop swing component
from one Jframe to another Jframe.
I read about (invokeAndWait) but it is not working in this case
any ideas
& if you have a small working example that would help a lot.

Share: 

 

15 Answers Found

 
Answer #1    Answered By: Julian Long     Answered On: Feb 11

There is a Frame containing a button n i want to create
another window whenever the button is pressed.Is it possible ?

I tried it by writing the creation of frame in
actionPerformed() of the ActionListener interface,but it didn't work.

 
Answer #2    Answered By: Omar Walker     Answered On: Feb 11

Yes it is possible. Here is a fragment that show you
how you should do it...
.......
JButton loginButton = new JButton("ClickMe");
loginButton.addActionListener(

new ActionListener() { // inner class

public void actionPerformed(ActionEvent event) {
MyOtherFrame myScreen = new MyOtherFrame();

}

} // end inner class

); // end call to addActionListener

.........
Note:
MyOtherFrame is another class defined in their own
class file. What you can do is copy the class you
already have and rename the copy MyOtherFrame. Just a
suggestion I think will be easier to help  you, if you
can post the program or fragment where you're having
problem(s)!

 
Answer #3    Answered By: Bonnie Hughes     Answered On: Feb 11

you can do this incrementing a variable if the button is clicked and if the
variable is greater than certain number(10) then a following code is executed

 
Answer #4    Answered By: Percy Morgan     Answered On: Feb 11

Here is a simple and primitive example  for you.Don't forget to use setVisible
method of JFrame.
Regards

public class JFrmMainWindow extends JFrame implements
java.awt.event.ActionListener{
JButton newWindow = new JButton();
public JFrmMainWindow() {
init();
}
public static void main(String[] args) {
JFrmMainWindow jFrmMainWindow = new JFrmMainWindow();

}
private void init() {
newWindow.setBounds(new Rectangle(134, 211, 132, 45));
newWindow.setText("New Window");
newWindow.addActionListener(this);
this.setSize(500,400);
this.getContentPane().setLayout(null);
this.getContentPane().add(newWindow, null);
this.setVisible(true);
}

private void createNewWindowAndShowIt(){
JFrame newWindow=new JFrame();
newWindow.setSize(100,100);
newWindow.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
createNewWindowAndShowIt();
}
}

 
Answer #5    Answered By: Aaron Kennedy     Answered On: Feb 11

There is a window,which contains a button n the code
is written in such a way that whenever the button is pressed , it
displays another window,but the problem is i want the child window
to be created only once evenif i press the button 10 times or
infinite number of times.

How is it possible , plz do mail me.

I have one more doubt , at wht points of time will a
person encounter NullPointerException.

 
Answer #6    Answered By: Ana Silva     Answered On: Feb 11

1. Use singleton design pattern(have a look to
http://www.patterndepot.com/put/8/Singleton.PDF) for creating if you want to
create only one instance of a class.
If it is not easy to understand you can do sometihng like that
Use a handle in the class which listens the action of your button but it should
be static (such as static boolean isWindowCreated)
Set this flag when you create a window to true.And control the state of this
flag before handling the event of your button.An example  code is in below:
public class JFrmMainWindow extends JFrame implements

static boolean isWindowCreated=false;
java.awt.event.ActionListener{
JButton newWindow = new JButton();
public JFrmMainWindow() {
init();
}
public static void main(String[] args) {
JFrmMainWindow jFrmMainWindow = new JFrmMainWindow();

}
private void init() {
newWindow.setBounds(new Rectangle(134, 211, 132, 45));
newWindow.setText("New Window");
newWindow.addActionListener(this);
this.setSize(500,400);
this.getContentPane().setLayout(null);
this.getContentPane().add(newWindow, null);
this.setVisible(true);
}

private void createNewWindowAndShowIt(){
if(!isWindowCreated){
JFrame newWindow=new JFrame();
newWindow.setSize(100,100);
newWindow.setVisible(true);
isWindowCreated=true;
}
}
public void actionPerformed(ActionEvent e) {
createNewWindowAndShowIt();
}
}

2. Null pointer exception means you did not create an instance but you are
trying to use it.
E.g
SimpleClass anInstanceOfSimpleClass;
anInstanceOfSimpleClass.makeASimpleThing();

3.You can navigate a folder with javax.swing.JFileChooser
Maybe you can navigate a web page with Runtime.getRuntime().exec method you can
execute iexplore but you can't naigate a web a page with swing  directly.maybe
there can be some libs maybe you can look to www.sf.net

Your questions are not clear maybe you can put some example codes to your mails

 
Answer #7    Answered By: Dustin Dean     Answered On: Feb 11

to me your problem looks simple....i will tell u how can u solve it..
First thing....dont a new window when you press the button. create the window
only once and
keeep that window instance as global. soo u just neeed to show the window whenn
u click the button.
hope u got wat m saying.......and null pointer exception occurs when you declare
a variable or object but you do not initialize it... and also when you are
tryinng to read  an object which has null value...that object can b a string can
b and array or anything which is null or empty.

 
Answer #8    Answered By: Ruairidh Anderson     Answered On: Feb 11

1.to create or open a single window check for boolean condition if frame enabled
or shown then display your window


2.nullpointerexception will be thrown when u refer to a object which is not
available at runtime.

 
Answer #9    Answered By: Jay Richards     Answered On: Feb 11

Is it possible to create Hyperlinks in Java using
the concept of "Swing". If yes, how ?

 
Answer #10    Answered By: Wade Jordan     Answered On: Feb 11

Can we achieve navigation using Swing,i mean to ask,
is it possible to navigate to a file of a folder or to any webpage
using Swing.

 
Answer #11    Answered By: Roderick King     Answered On: Feb 11

what exactly do you mean, navigating through the landscape using
Swing? Do you want to build something the like Internet Explorer? Or
the File Manager from Windows 3.1? Or download a web page at a given
URL? What exactly do you want to do?

 
Answer #12    Answered By: Fatih Farooq     Answered On: Feb 11

There is a program , which displays a window
whenever a button is pressed but the problem is , i want the window
to be displayed only once even if i click it 10 times or infinite
times . How can i achieve that .

 
Answer #13    Answered By: Isaac Evans     Answered On: Feb 11

The easiest way is to use a static method of the class which uses a
static counter (or flag) variable; the principle goes like this:

1) In your class, declare a static int (or boolean) alreadyCreated.
2) Define a static method which first makes sure that it's called
only once (not twice or more often in parallel executions, that means
you have to execute this method in a single thread always).
3) This method checks whether the flag variable already has been set
to 1 (or greater). If so, it returns null.
4) Otherwise (flag is still 0 or false, depending on the type of the
variable) create a new instance of this class and return it to the
caller _after_ incrementing the counter (or setting the flag to true).

This sort of methods (static methods creating instances of their
class) is called factory methods.

 
Answer #14    Answered By: Erin Dunn     Answered On: Feb 11

When will we get a NullPointerException in the
concept of Swing ?

Is it correct n whether it works , if we write an
actionPerformed() for a Button and a TextField in an inner class.

 
Answer #15    Answered By: Ana Bradley     Answered On: Feb 11

a NullPointerException is thrown if you try to reference an instance
of any class where the reference is still null.

Would you please explain in a bit more detail what you mean by the
Button and the TextField? Without more info we can't give more than
general hints about your possible problems.

 
Didn't find what you were looking for? Find more on Swing Or get search suggestion and latest updates.




Tagged: