JDialog and JFrame and new instance - java

I have 2 classes - a JFrame with a button, and a JDialog (a pop up) with few textfields on it. Now, when I click the button in JFrame a JDialog shows up:
/*** someClass class ****/
JButton btnNewButton = new JButton("");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jd = new jDialog();
jd.setModal(true);
jd.setVisible(true);
}
});
public void doStuff(String one....String five){
... ..
}
Now a jDialog pops up and i need to fill up some textfields inside it and click another button to confirm.
/*** jDialog class ***/
JButton btnConfirm = new JButton("");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
one = 1_tf.getText();
two = 2_tf.getText();
three = 3_tf.getText();
four = 4_tf.getText();
five = 5_tf.getText();
doAnything(one,two,three,four,five);
1_tf.setText("");
2_tf.setText("");
3_tf.setText("");
4_tf.setText("");
5_tf.setText("");
}
});
public void doAnything(String one,String two,String three,String four,String five){
someClass sc = new someClass();
sc.doStuff(one,two,three,four,five);
}
The textfield's values will be passed to doAnything() method, inside doAnything() is an instance of someClass class to access sc.doStuff() method and pass the values.
The problem now is that, Whenever I click confirm on jDialog class a new JFrame appears so there are 2 JFrames which is not what I want.. It's like whenever I do something on jdialog it creates a new JFrame instead of over lapping the original JFrame.

In the doAnything() don't recreate the someClass (it's better to read java naming convention -classes should start from capital letter) instances.
Define class field for the someClass instance? create it just once (if it's null) and reuse it.

That's probably because in the someClass constructor you create an show a JFrame. To avoid this problem, make a static reference to the doStuff() method.
Declare it as:
public static doStuff(one, two, three, four, five) {
...
}
and use it as:
someClass.doStuff(one, two, three, four, five);
Now, when you use the method, you don't have to create an object of someClass class, and then you don't have to call the constructor method.

Related

How to call setVisible from another class?

What I have:
Two Classes that instantiate two JFrames.
What I am trying to achieve:
One with a button and the other that will become invisible when a action is fired on the button.
Problems:
I do not know how to pursue this. How should I go about coding this?
Class 1
public class test1{
public static void main(String[] args){
JFrame frame = new JFrame("Blinkly Frame");
frame.setSize(100, 100);
frame.setVisible(true);
}
}
Class 2
public class test2{
public static void main(String[] args){
JButton button = new JButton();
//when i will click this button i want to make invisible frame
}
}
Solution:
Create an instance of the class that has a Jframe or extends a JFrame.
First we need the JFrame that will be disappearing.
public class BClass extends JFrame{
// Disappearing frame
public BClass()
{
this.setSize(300,300);//sets frame properties
this.setLocationRelativeTo(null);
}
}
Next we need the Frame that will be holding the buttons. (Documentation added)
public class ACLass {
public static void main(String[] args) {
JFrame frame = new JFrame("Magician"); // instantiates
BClass b = new BClass(); // instantiates class that extends JFrame
b.setVisible(true);//
frame.setSize(300,300);//
frame.setVisible(true);//
frame.setLocationRelativeTo(null);//
JButton disappearButton = new JButton("Disappear"); //Adds button
disappearButton.addActionListener(new ActionListener() { // Adds action -When button is "clicked"
public void actionPerformed(ActionEvent e) { // method called when action fired
b.setVisible(false); //visibility changed
}
});
disappearButton.setBounds(0,0,300,150);
JButton appearButton = new JButton("appear"); //Adds button
appearButton.addActionListener(new ActionListener() { // Adds action -When button is "clicked"
public void actionPerformed(ActionEvent e) { // method called when action fired
b.setVisible(true); //visibility changed
}
});
disappearButton.setBounds(0,100,300,150);
frame.add(disappearButton, BorderLayout.PAGE_START); //adds button to frame
frame.add(appearButton, BorderLayout.PAGE_END); //adds button to frame
//I used border layout however use the a layout manager that works with your components/frame
}
}
Exotic Explanation
I'm going to explain this in terms of a magic show as it provides better understanding.
So first, we have one magician(JFrame) and his wand(JButton) and then we have the helper(the second JFrame) that will disappear and a stage that has been set(all properties defined etc.)
First the magician adds some magic power to his wand(actionListener that handles the button being pushed) that will react when the magician waves it(action fired a.k.a button being pushed).
Next we show the audience the helper(instantiating JFrame to disappear).
As we have shown the audience the helper, we can now show him/her disappearing (Now we can call setVisible through the instance variable of the class).
Finally, the magician waves his wand(firing an action), and the helper gets the signal(actionPerformed method) to disappear. The helper then can call the
b.setVisible(false); //making the frame invisible
General Explanation
https://docs.oracle.com/javase/tutorial/java/javaOO/usingobject.html
Basically, by instantiating an object in another class you can also call the objects methods in that class i.e. setVisible(boolean b).
Other less preferable solutions:
If your disappearing class has does not extend a JFrame but instantiates one..
You would need to create an instance variable
private JFrame j;
Then use getters/setters to access the object which will then allow you to call its methods.
secondClass.getFrame().setVisible(true);//getFrame returns the JFrame
Then add that to the actionPerformed method.
You can also use a static instance variable and statically reference it in the actionPerformed method... (not recommended)
secondClass.frame.setVisible(true);

actionPerformed with JButton [duplicate]

private JButton jBtnDrawCircle = new JButton("Circle");
private JButton jBtnDrawSquare = new JButton("Square");
private JButton jBtnDrawTriangle = new JButton("Triangle");
private JButton jBtnSelection = new JButton("Selection");
How do I add action listeners to these buttons, so that from a main method I can call actionperformed on them, so when they are clicked I can call them in my program?
Two ways:
1. Implement ActionListener in your class, then use jBtnSelection.addActionListener(this); Later, you'll have to define a menthod, public void actionPerformed(ActionEvent e). However, doing this for multiple buttons can be confusing, because the actionPerformed method will have to check the source of each event (e.getSource()) to see which button it came from.
2. Use anonymous inner classes:
jBtnSelection.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
selectionButtonPressed();
}
} );
Later, you'll have to define selectionButtonPressed().
This works better when you have multiple buttons, because your calls to individual methods for handling the actions are right next to the definition of the button.
2, Updated. Since Java 8 introduced lambda expressions, you can say essentially the same thing as #2 but use fewer characters:
jBtnSelection.addActionListener(e -> selectionButtonPressed());
In this case, e is the ActionEvent. This works because the ActionListener interface has only one method, actionPerformed(ActionEvent e).
The second method also allows you to call the selectionButtonPressed method directly. In this case, you could call selectionButtonPressed() if some other action happens, too - like, when a timer goes off or something (but in this case, your method would be named something different, maybe selectionChanged()).
Your best bet is to review the Java Swing tutorials, specifically the tutorial on Buttons.
The short code snippet is:
jBtnDrawCircle.addActionListener( /*class that implements ActionListener*/ );
I don't know if this works but I made the variable names
public abstract class beep implements ActionListener {
public static void main(String[] args) {
JFrame f = new JFrame("beeper");
JButton button = new JButton("Beep me");
f.setVisible(true);
f.setSize(300, 200);
f.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// Insert code here
}
});
}
}
To add an action listener, you just call addActionListener from Abstract Button.

Using JButton ActionListener to run different class in same package

I'm having an issue as I'm relatively new to GUI.
Basically to put everyone in the picture, I have a package which consists of:
my MainClass (Includes the GUI)
A seperate Class (Don't want it to run unless button is clicked)
Another seperate class which I don't want to run unless it's specific button is clicked.
So my MainClass GUI is basically the controller.
However, I honestly have no clue how to go about it. Was told to have to create a constructor and use getters/setters? However I still don't understand how to call that specific class whilst leaving the other off "Turned off".
Thanks.
Well, there are quite a few ways to do this... Either you create anonymous listeners for each button, and then, depending on what you want to do, trigger methods in other classes or the like;
JButton b1 = new JButton();
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//Do something!
OtherClass other = new OtherClass();
other.myMethod();
}
});
JButton b2 = new JButton();
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
//Do something else!
...
}
});
Alternatively, you use the command string and associate a unique command (made final, preferably) which you compare with when receiving a actionPerformed in a common listener implementation;
//In your class, somewhere...
public final static String CMD_PRESSED_B1 = "CMD_PRESSED_B1";
public final static String CMD_PRESSED_B2 = "CMD_PRESSED_B2";
//Create buttons
JButton b1 = new JButton();
JButton b2 = new JButton();
//Assign listeners, in this case "this", but it could be any instance implementing ActionListener, since the CMDs above are declared public static
b1.addActionListener(this);
b2.addActionListener(this);
//Assign the unique commands...
b1.setActionCommand(CMD_PRESSED_B1);
b2.setActionCommand(CMD_PRESSED_B2);
And then, in your listener implementation;
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals(CMD_PRESSED_B1)
{
//Do something!
OtherClass other = new OtherClass();
other.myMethod();
}
else if (e.getActionCommand().equals(CMD_PRESSED_B2)
{
//Do something else!
...
}
}

Destroy instance of class then create instance of it again

i have a class (Class ButtonX) that contains a button
when user clicks the button, it will create instance of the class DialogX
when I create instance of the class DialogX it will show up JDialog
public class ButtonX {
public ButtonX() {
JFrame me = new JFrame();
JButton n = new JButton("show dialog");
n.addActionListener(ListenerX.listen);
me.getContentPane().add(n);
me.pack();
me.setVisible(true);
}
public static void main (String[]args){
new ButtonX();
}
}
listener of that JButton
public class ListenerX {
public static ActionListener listen = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
DialogX dialogx = null;
dialogx = new DialogX();
}};
}
class that contains JDialog
public class DialogX {
static JDialog g = new JDialog();
public DialogX() {
JLabel label = new JLabel("label");
g.getContentPane().setLayout(new FlowLayout());
g.getContentPane().add(label);
g.pack();
g.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
g.setVisible(true);
}
}
what I try to achieve is, that when user clicks the button, it will destroy instance of class DialogX ( if it exist ) and then create again instance of DialogX
What to do?
thanks..
forgive my english..
You cannot explicitly destroy objects in Java. Once there are no more references (think of pointers) to an Object left, it will be marked as eligible for being garbage collected. Your code therefore is almost fine, as it removes the old reference to the DialogX instance and creates a new one.
What you need to do is either extend JDialog with your DialogX class (then you can delete the JDialog variable completely) or remove the static keywoard before the JDialog variable g. Then you can call dialogx.dispose() (you extended JDialog) or a custom method you need to implement which forwards the call to g.dispose() (you did not extend JDialog).

Difficulty removing all components from a Jpanel

G'day all,
I am coding a main menu for a project. The menu displays properly. I have also set up ActionListeners for the three buttons on the menu.
What I wish to do is reuse the JPanel for a new set of radio buttons when the user chooses "Start a New Game".
However, coding ActionPerformed to remove the existing components from the JPanel has me stumped. I know removeAll is somehow important, but unfortunately NetBeans informs me I cannot call it on my mainMenu JPanel object within ActionPerformed. So i have commented it out in my code below, but left it in so you can see what I am trying to do.
Your thoughts or hints are appreciated.
Here is my main code:
public class Main {
public static void main(String[] args) {
MainMenu menu = new MainMenu();
menu.pack();
menu.setVisible(true);
}
}
Here is my mainMenu code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainMenu extends JFrame implements ActionListener {
JButton startNewGame = new JButton("Start a New Game");
JButton loadOldGame = new JButton("Load an Old Game");
JButton seeInstructions = new JButton("Instructions");
public MainMenu() {
super("RPG Main Menu");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainMenu = new JPanel();
mainMenu.setLayout(new FlowLayout());
startNewGame.setMnemonic('n');
loadOldGame.setMnemonic('l');
seeInstructions.setMnemonic('i');
startNewGame.addActionListener(this);
loadOldGame.addActionListener(this);
seeInstructions.addActionListener(this);
mainMenu.add(startNewGame);
mainMenu.add(loadOldGame);
mainMenu.add(seeInstructions);
setContentPane(mainMenu);
}
public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source == startNewGame) {
// StartNewGame code goes here
// mainMenu.removeAll();
}
if (source == loadOldGame) {
// LoadOldGame code goes here
}
if (source == seeInstructions) {
// Quit code goes here
}
}
}
Consider using a CardLayout instead, which manages two or more components (usually JPanel instances) that share the same display space. That way you don't have to fiddle with adding and removing components at runtime.
You need mainMenu to be a member variable:
public class MainMenu extends JFrame implements ActionListener {
JButton startNewGame = new JButton("Start a New Game");
JButton loadOldGame = new JButton("Load an Old Game");
JButton seeInstructions = new JButton("Instructions");
JPanel mainMenu = new JPanel();
Why do you feel the need to re-use this object?
You don't have a reference to mainMenu actionPerformed use. If you declare mainMenu with the buttons. It would work.
The problem is that the actionPerformed method is trying to call the JPanel mainMenu which is out of scope, i.e. the mainMenu variable is not visible from the actionPerformed method.
One way to get around this is to have the JPanel mainMenu declaration in the class itself and make it an instance field which is accessible to all instance methods of the class.
For example:
public class MainMenu extends JFrame implements ActionListener
{
...
JPanel mainMenu;
public MainMenu()
{
...
mainMenu = new JPanel();
...
}
public void actionPerformed(ActionEvent e)
{
...
mainMenu.removeAll();
}
}
Avoid attempting to "reuse" stuff. Computers are quite capable of tidying up. Concentrate on making you code clear.
So instead of attempting to tidy up the panel, simply replace it with a new one.
Generally a better way to write listeners is as anonymous inner classes. Code within these will have access to final variables in the enclosing scope and to members of the enclosing class. So, if you make mainMenu final and you ActionListeners anonymous inner classes, your code should at least compile.
Also don't attempt to "reuse" classes. Try to make each class do one sensible thing, and avoid inheritance (of implementation). There is almost never any need to extend JFrame, so don't do that. Create an ActionListener for each action, rather than attempting to determine the event source.
Also note, you should always use Swing components on the AWT Event Dispatch Thread. Change the main method to add boilerplate something like:
public static void main(final String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() { public void run() {
runEDT();
}});
}

Categories

Resources