Dispose JDialog from another class - java

I want to dispose a JDialog from another class, because i am trying to keep classes and methods clean, and not create buttons and handle the listeners in the same class. So here is the problem.
I tried creating a get method from the first class to get the dialog and then dispose it on the third but didnt work.
public class AddServiceListener extends JFrame implements ActionListener {
/**
* Creates listener for the File/New/Service button.
*/
public AddServiceListener() {
}
/**
* Performs action.
*/
public void actionPerformed(ActionEvent e) {
AddServiceWindow dialog = new AddServiceWindow();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
}
}
public class AddServiceWindow extends JDialog {
private JPanel contentPanel;
private JFrame frame;
private JPanel buttonPanel;
private JLabel nameLabel;
private JTextField nameField;
private JLabel destinationLabel;
private JTextField destinationField;
private JButton destinationButton;
private JButton okButton;
private JButton cancelButton;
/**
* Creates the dialog window.
*/
public AddServiceWindow() {
ManageMinder mainFrame = new ManageMinder();
frame = mainFrame.getFrame();
contentPanel = new JPanel();
contentPanel.setLayout(null);
setTitle("New Service File");
setSize(340, 220);
setLocationRelativeTo(frame);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
createLabels();
createTextFields();
createButtons();
addListeners();
}
/**
* Creates the labels.
*/
private void createLabels() {
nameLabel = new JLabel("Name:");
nameLabel.setHorizontalAlignment(SwingConstants.RIGHT);
nameLabel.setBounds(25, 25, 52, 16);
contentPanel.add(nameLabel);
destinationLabel = new JLabel("Path:");
destinationLabel.setHorizontalAlignment(SwingConstants.RIGHT);
destinationLabel.setBounds(7, 70, 70, 16);
contentPanel.add(destinationLabel);
}
/**
* Creates the text fields.
*/
private void createTextFields() {
nameField = new JTextField();
nameField.setBounds(87, 22, 220, 22);
contentPanel.add(nameField);
nameField.setColumns(10);
destinationField = new JTextField();
destinationField.setBounds(87, 68, 220, 20);
contentPanel.add(destinationField);
destinationField.setColumns(10);
}
/**
* Creates the buttons of the window.
*/
private void createButtons() {
destinationButton = new JButton("Select...");
destinationButton.setBounds(87, 99, 82, 23);
destinationButton.setFocusPainted(false);
contentPanel.add(destinationButton);
okButton = new JButton("OK");
okButton.setFocusPainted(false);
buttonPanel.add(okButton);
cancelButton = new JButton("Cancel");
cancelButton.setFocusPainted(false);
buttonPanel.add(cancelButton);
}
/**
* Adds listeners to buttons.
*/
private void addListeners() {
ActionListener destinationAction = new AddDestinationListener(destinationField);
destinationButton.addActionListener(destinationAction);
ActionListener okAction = new SaveNewServiceFileListener(nameField, destinationField);
okButton.addActionListener(okAction);
}
}
public class SaveNewServiceFileListener extends JFrame implements ActionListener {
private JTextField nameField;
private JTextField destinationField;
private String path;
private File newService;
/**
* Creates listener for the File/New/Add Service/OK button.
*/
public SaveNewServiceFileListener(JTextField nameField, JTextField destinationField) {
this.nameField = nameField;
this.destinationField = destinationField;
}
/**
* Performs action.
*/
public void actionPerformed(ActionEvent e) {
path = destinationField.getText() + "\\" + nameField.getText() + ".csv";
try {
newService = new File(path);
if(newService.createNewFile()) {
System.out.println("Done!");
// DISPOSE HERE
}
else {
System.out.println("Exists!");
}
} catch (IOException io) {
throw new RuntimeException(io);
}
}
}
What should i do, so the dialog disposes on the third one, when OK is clicked and file is created?

The way to change the state of another object is to 1) have a reference to that object, and 2) call a public method on it.
Here the other object is the JDialog, and the state that you wish to change is its visibility by calling either .close() or .dispose() on it. The problem that you're having is that the reference to the JDialog is not readily available since it is buried within the actionPerformed(...) method of your AddServiceListener class.
So don't do this -- don't bury the reference but rather put it into a field of the class that needs it.
If you absolutely need to have stand-alone ActionListener classes, then I think that the simplest thing to do is to take the dialog out of the listener class and into the view class, your main GUI, and then have the listener call methods on the view. For example
Assume that the dialog class is called SomeDialog and the main GUI is called MainGui. Then put the reference to the dialog in the MainGui class:
public class MainGui extends JFrame {
private SomeDialog someDialog = new SomeDialog(this);
And rather than have your listeners create the dialog, have them call a method in the main class that does this:
public class ShowDialogListener implements ActionListener {
private MainGui mainGui;
public ShowDialogListener(MainGui mainGui) {
// pass the main GUI reference into the listener
this.mainGui = mainGui;
}
#Override
public void actionPerformed(ActionEvent e) {
// tell the main GUI to display the dialog
mainGui.displaySomeDialog();
}
}
Then MainGUI can have:
public void displaySomeDialog() {
someDialog.setVisible(true);
}
An example MRE program could look like so:
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.*;
import javax.swing.*;
public class FooGui002 {
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
MainGui mainGui = new MainGui();
mainGui.setVisible(true);
});
}
}
#SuppressWarnings("serial")
class MainGui extends JFrame {
private SomeDialog someDialog;
private JButton showSomeDialogButton = new JButton("Show Some Dialog");
private JButton closeSomeDialogButton = new JButton("Close Some Dialog");
public MainGui() {
super("Main GUI");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setPreferredSize(new Dimension(500, 200));
someDialog = new SomeDialog(this);
showSomeDialogButton.addActionListener(new ShowDialogListener(this));
showSomeDialogButton.setMnemonic(KeyEvent.VK_S);
closeSomeDialogButton.addActionListener(new CloseSomeDialogListener(this));
closeSomeDialogButton.setMnemonic(KeyEvent.VK_C);
setLayout(new FlowLayout());
add(showSomeDialogButton);
add(closeSomeDialogButton);
pack();
setLocationByPlatform(true);
}
public void displaySomeDialog() {
someDialog.setVisible(true);
}
public void closeSomeDialog() {
someDialog.setVisible(false);
}
}
#SuppressWarnings("serial")
class SomeDialog extends JDialog {
public SomeDialog(Window window) {
super(window, "Some Dialog", ModalityType.MODELESS);
setPreferredSize(new Dimension(300, 200));
add(new JLabel("Some Dialog", SwingConstants.CENTER));
pack();
setLocationByPlatform(true);
}
}
class ShowDialogListener implements ActionListener {
private MainGui mainGui;
public ShowDialogListener(MainGui mainGui) {
this.mainGui = mainGui;
}
#Override
public void actionPerformed(ActionEvent e) {
mainGui.displaySomeDialog();
}
}
class CloseSomeDialogListener implements ActionListener {
private MainGui mainGui;
public CloseSomeDialogListener(MainGui mainGui) {
this.mainGui = mainGui;
}
#Override
public void actionPerformed(ActionEvent e) {
mainGui.closeSomeDialog();
}
}

Related

Updating TextField from different class than GUI

I'm trying to update the GUI from a different Class. Every time I have tried calling the method on a different class it doesn't work but If I do it in the GUI Class it works just fine.
GUI Class:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test7 extends JPanel implements ActionListener {
private JButton jcomp1;
private JButton jcomp2;
private JButton jcomp3;
private JTextField jcomp4;
public Test7() {
//construct components
jcomp1 = new JButton("Button 1");
jcomp2 = new JButton("Button 2");
jcomp3 = new JButton("Button 3");
jcomp4 = new JTextField(5);
jcomp1.addActionListener(this);
//adjust size and set layout
setPreferredSize(new Dimension(723, 455));
setLayout(null);
//add components
add(jcomp1);
add(jcomp2);
add(jcomp3);
add(jcomp4);
//set component bounds (only needed by Absolute Positioning)
jcomp1.setBounds(160, 320, 100, 20);
jcomp2.setBounds(340, 320, 100, 20);
jcomp3.setBounds(490, 315, 140, 20);
jcomp4.setBounds(180, 125, 365, 110);
}
public static void main(String[] args) {
JFrame frame = new JFrame("MyPanel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new Test7());
frame.pack();
frame.setVisible(true);
}
public void setTextArea(JTextField jcomp4) {
this.jcomp4 = jcomp4;
}
public JTextField getTextArea() {
return this.jcomp4;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jcomp1) {
Class class1 = new Class();
class1.start();
}
}
}
Second Class:
public class Class extends Test7{
public void start(){
Test7 form = new Test7();
form.getTextArea().setText("nesto");
}
}
I have also tried putting the code on a same Thread but that doesn't seem to work either.
Test7 is JPanel. It is added to JFrame in your main method. When you call Class.start() you create new Test7. This new JPanel is not related to JPanel previously added to JFrame, so it is not even visible.
Your Class class should neither extend nor create new Test7 instance. Instead Test7 instance could be passed in constructor to Class:
Class class1 = new Class(this);
class1.start();
Your Class class:
public class Class {
private final Test7 test;
public Class(Test7 test) {
super();
this.test = test;
}
public void start() {
test.getTextArea().setText("nesto");
}
}
Andrew`s comments are also worthwile to think about.

Trying to add ActionListener to JButtons

I cannot figure out how to add Actionlisteners to the JButtons, any help would be much appreciated.
public class Translator extends JPanel implements MouseListener, ActionListener {
private JButton french = new JButton();
private JButton german = new JButton();
private JButton irish = new JButton();
public Translator(){
french = new JButton("French");
german = new JButton("German");
irish = new JButton("Irish");
setLayout(new GridLayout(2,1));
buttonPanel.setLayout(new GridLayout(1,3));
buttonPanel.add(french);
buttonPanel.add(german);
buttonPanel.add(irish);
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
#Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
}
There are plenty of ways to add an ActionListener, to a given JComponent (that supports it's use). I have added some comments in the code snippets, to help explain them a bit better, and some links in the comments for future reference.
1.) If the class implements the ActionListener interface, i.e. the class itself contains the actionPerformed(...) method, then one can do it, in this manner:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Skeleton implements ActionListener {
private JFrame frame;
private JPanel contentPane;
private JButton button;
private void displayGUI() {
frame = new JFrame("Skeleton");
/*
* EXIT_ON_CLOSE is same as putting System.exit(0),
* which in some sense, doesnot allows one's
* application to terminate graciously.
*/
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = new JPanel();
button = new JButton("This is a button.");
/*
* This is one way of attaching an ActionListener
* to the JButton, but the main disadvantage of
* this approach is, it breaks encapsulation,
* as you can see the public method, actionPerformed(),
* is lying free to be accessed by any code outside
* the scope of the class
*/
button.addActionListener(this);
contentPane.add(button);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new Skeleton().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "BINGO!",
"Information: ", JOptionPane.INFORMATION_MESSAGE);
}
}
2.) If one doesn't wants to create unnecessary class files. Then this approach, which uses, EventHandler can be used:
import java.awt.*;
import java.awt.event.*;
import java.beans.EventHandler;
import javax.swing.*;
public class Example1 {
private JFrame frame;
private JPanel contentPane;
private JButton button;
private void displayGUI() {
frame = new JFrame("Skeleton");
/*
* EXIT_ON_CLOSE is same as putting System.exit(0),
* which in some sense, doesnot allows one's
* application to terminate graciously.
*/
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = new JPanel();
button = new JButton("This is a button.");
/*
* This is another way of attaching
* an ActionListener to the JButton,
* the main advantage of this approach
* is, that one does not have to create
* a new class to handle events
* More info regarding the use of this
* approach, can be found on this link :
* http://docs.oracle.com/javase/tutorial/uiswing/events/generalrules.html
*/
button.addActionListener((ActionListener)
EventHandler.create(ActionListener.class
, Example1.this, "buttonAction", ""));
contentPane.add(button);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new Example1().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
public void buttonAction(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "BINGO!",
"Information: ", JOptionPane.INFORMATION_MESSAGE);
}
}
3.) If one is more concern about the concept of Encapsulation, then this approach is beneficial:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Example2 {
private JFrame frame;
private JPanel contentPane;
private JButton button;
private ActionListener buttonActions =
new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
JOptionPane.showMessageDialog(frame, "BINGO!",
"Information: ", JOptionPane.INFORMATION_MESSAGE);
}
};
private void displayGUI() {
frame = new JFrame("Skeleton");
/*
* EXIT_ON_CLOSE is same as putting System.exit(0),
* which in some sense, doesnot allows one's
* application to terminate graciously.
*/
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = new JPanel();
button = new JButton("This is a button.");
/*
* This is another way of attaching
* an ActionListener to the JButton,
* the main advantage of this approach
* is, it adheres to encapsulation.
*/
button.addActionListener(buttonActions);
contentPane.add(button);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new Example2().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
4.) If one is more inclined towards creation of Anonymous Classes, then this approach can be used:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Example3 {
private JFrame frame;
private JPanel contentPane;
private JButton button;
private void displayGUI() {
frame = new JFrame("Skeleton");
/*
* EXIT_ON_CLOSE is same as putting System.exit(0),
* which in some sense, doesnot allows one's
* application to terminate graciously.
*/
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = new JPanel();
button = new JButton("This is a button.");
/*
* This is the fourth way of attaching
* an ActionListener to the JButton,
* the main advantage of this approach
* is, it adheres to encapsulation, the
* public method remains hidden
* inside the Anonymous Class
* More info can be found on this link :
* http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
* The main disadvantage of this approach is
* that it doesnot gives you the privilege
* of separation of concerns, which can
* be done using the fifth approach,
* which is MVC - Pattern (Model-View-Controller)
* and moreover, it creates a hell lot of classes, in
* your project, which can lead to extra overhead
*/
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
JOptionPane.showMessageDialog(frame, "BINGO!",
"Information: ", JOptionPane.INFORMATION_MESSAGE);
}
});
contentPane.add(button);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new Example3().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
EDIT:
5.) This approach, includes using Action instead of ActionListener. This is to be used for sharing same functionality among various JComponents, which leads to code reusability
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Example4 {
private JFrame frame;
private JPanel contentPane;
private JMenuItem showMenuItem;
private JButton button;
private Action myActions;
/*
* This approach is basically used, when
* one wants to share the same functionality
* of different JComponents among each other,
* without writing redundant codes for each
* one of those components. Here JMenuItem
* and JButton are both using the same
* functionality, to perform the same task.
* More info can be found on this link:
* http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html
*/
private class MyActions extends AbstractAction {
public MyActions(String title, String desc) {
super(title);
putValue(SHORT_DESCRIPTION, desc);
}
#Override
public void actionPerformed(ActionEvent ae) {
JOptionPane.showMessageDialog(frame, "BINGO!",
"Information: ", JOptionPane.INFORMATION_MESSAGE);
}
}
private void displayGUI() {
frame = new JFrame("Skeleton");
/*
* EXIT_ON_CLOSE is same as putting System.exit(0),
* which in some sense, doesnot allows one's
* application to terminate graciously.
*/
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
contentPane = new JPanel();
button = new JButton("This is a button.");
myActions = new MyActions("Show", "A small description");
button.setAction(myActions);
contentPane.add(button);
frame.setJMenuBar(getJMenuBar());
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JMenuBar getJMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
showMenuItem = new JMenuItem(myActions);
fileMenu.add(showMenuItem);
menuBar.add(fileMenu);
return menuBar;
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new Example4().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
french.addActionListener(an_instance_of_the_class_where_actionPerformed_is);
which, as I can see after the edit, would be this
Also, see this Example and some tutorial text in this corner of the web
I think that the reference to the tutorial and the many examples is highly relevant.
button.addActionListener(<your_ActionListener_here>);
In your case, it'll be:
french.addActionListener(this);
If you want to use the same ActionListener for all the three buttons, you can use the getSource() function of ActionEvent e to detect which button was actually pressed.
If you are using Java8, you can try this.
JButton french = new JButton("French");
french.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent ae){
System.out.println("Button french clicked!");
}
});
french.addActionListener(button -> System.out.println("Button Click listener..."));
JFrame frame = new JFrame("Button Listener Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(french, BorderLayout.CENTER);
frame.setSize(250, 250);
frame.setVisible(true);
Obviously the answer is place this into your addActionListener method.
The addActionListener method takes as an argument an object that implements ActionListener interface, this interface force you to implements/place-in-your-code theactionPerformed method which is the one that is called, when an action is triggered to the component that is assigned.
So, placing this in your method, it will search inside the object you passed, in our case, the Translator object for an actionPerformed method and call it.
this.french.addActionListener(this);
Of course there is a lot of code missing in order to work.
I really liked #Sandeep answer take use of lambda expression.
You can see a full example below.
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Translator extends JPanel implements ActionListener {
private JButton french = new JButton();
private JButton german = new JButton();
private JButton irish = new JButton();
#SuppressWarnings("empty-statement")
public Translator(){
french = new JButton("French");
german = new JButton("German");
irish = new JButton("Irish");
// setLayout(new GridLayout(2,1));
this.setLayout(new GridLayout(1,3));
this.add(french);
this.add(german);
this.add(irish);
ActionListener ac = (ActionEvent ae) -> { System.out.println(((JButton) ae.getSource()).getText()); };
this.french.addActionListener(ac);
this.german.addActionListener(ac);
this.irish.addActionListener(ac);
this.irish.addActionListener(Translator.this);
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println(((JButton) e.getSource()).getText());
}
public static void main(String[] args) {
JFrame jframe = new JFrame("StackOverflow");
jframe.add(new Translator());
jframe.pack();
jframe.setLocationRelativeTo(null);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setVisible(true);
}
}
JButton button = new JButton("Button");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//To-Do
//Button clicked
});
Hope this helps, it's relatively simple! You just need to add an ActionListener to your desired JButton
To give you a broader idea of how to implement it in a case-scenario where I would like to run a new GUI frame after pressing a button:
startNewFrame.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Starting new frame");
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
NewFrame newFrame = new NewFrame();
newFrame.setVisible(true);
dispose();//Disposes of current frame
}
});
}
});

jDialog not showing

I have tried to add my jPanel to a jDialog and when I trigger the button nothing happens. Why? I have the code below:
public class fontFormat{
public void fontPanel(){
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.getPreferredSize();
Dimension size = new Dimension();
size.width = 400;
size.height = 600;
panel.setPreferredSize(size);
panel.add(new JLabel("label"));
panel.add(new JButton("button"));
JDialog fontDialog = new JDialog();
fontDialog.add(fontDialog);
}
}
Here:
JDialog fontDialog = new JDialog();
fontDialog.add(fontDialog);
You appear to be trying to add your JDialog to itself, which should cause your code to not function. While this code may compile, running this method should cause the JVM to throw an IllegalArgumentException on the fontDialog.add(fontDialog); line.
Please note that you show a JDialog similar to how you show a JFrame:
When you call your JDialog constructor, you will want to pass in the parent window into it, especially if your desire is to display a modal dialog.
You will also want to pass into the constructor the correct ModalityType enum.
You give your JDialog content, often a JPanel with your components on it.
You pack it
then you call setVisible(true) on it, and it should display
For example,
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DialogEg {
private static void createAndShowGUI() {
MainPanelGen mainPanelGen = new MainPanelGen();
JFrame frame = new JFrame("DialogEg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanelGen.getMainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
class MainPanelGen {
private JPanel mainPanel = new JPanel();
private JTextField field = new JTextField(10);
private JButton btn = new JButton(new BtnActn());
private JDialog dialog;
private DialogPanel dialogPanel = new DialogPanel();
public MainPanelGen() {
mainPanel.add(field);
mainPanel.add(btn);
field.setEditable(false);
field.setFocusable(false);
}
public JPanel getMainPanel() {
return mainPanel;
}
private class BtnActn extends AbstractAction {
BtnActn() {
super("Button");
}
#Override
public void actionPerformed(ActionEvent arg0) {
if (dialog == null) {
Window win = SwingUtilities.getWindowAncestor(mainPanel);
if (win != null) {
dialog = new JDialog(win, "My Dialog",
Dialog.ModalityType.APPLICATION_MODAL);
dialog.getContentPane().add(dialogPanel);
dialog.pack();
dialog.setLocationRelativeTo(null);
}
}
dialog.setVisible(true); // here the modal dialog takes over
System.out.println (dialogPanel.getFieldText());
field.setText(dialogPanel.getFieldText());
}
}
}
class DialogPanel extends JPanel {
private JTextField field = new JTextField(10);
private JButton exitBtn = new JButton(new ExitBtnAxn("Exit"));
public DialogPanel() {
add(field);
add(exitBtn);
}
public String getFieldText() {
return field.getText();
}
private class ExitBtnAxn extends AbstractAction {
public ExitBtnAxn(String name) {
super(name);
}
#Override
public void actionPerformed(ActionEvent arg0) {
Window win = SwingUtilities.getWindowAncestor(DialogPanel.this);
if (win != null) {
win.dispose();
}
}
}
}

Static Input Dialog with JDialog

I made this sample:
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class JDialogTest extends JDialog implements ActionListener{
private boolean actionPerformed;
private JButton okButton;
private JTextField textField;
private JDialogTest(String title, JFrame frame){
super(frame, title, true);
setDefaultCloseOperation(HIDE_ON_CLOSE);
setMinimumSize(new Dimension(200, 200));
init();
}
public static String getInput(String title, JFrame frame){
JDialogTest input = new JDialogTest(title, frame);
input.setVisible(true);
while(true){
if(input.actionPerformed){
input.setVisible(false);
String text = input.textField.getText();
return text;
}
}
}
private void init(){
textField = new JTextField();
okButton = new JButton("OK");
okButton.addActionListener(this);
setLayout(new GridLayout(2, 1, 5, 5));
add(textField);
add(okButton);
pack();
}
#Override
public void actionPerformed(ActionEvent evt) {
System.out.println("click");
actionPerformed = true;
}
public static void main(String[] args) {
System.out.println(JDialogTest.getInput("Test", null));
}
}
I create new Dialog via a static method witch returns a string.
But the while-loop witch should detect if the button was pressed won't get started!
I know about JOptionPanes but I don't want to use them. I tried using a JFrame instead but it doesn't work if I try to init the Dialog inside of another JFrame/JDialog (it doesn't render).
With while(true) running in the same thread as the gui, is gonna to freeze your view, and is not the proper way you are using listeners. As your dialog is modal then the dialog has the flowcontrol.
Look at this SSCCE based in your example , with a few changes.
Example:
public class JDialogTest {
private JDialog dialog;
private JTextField textField;
private JDialogTest (String title, JFrame frame){
dialog = new JDialog(frame, title, true);
dialog.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
dialog.setMinimumSize(new Dimension(200, 200));
init();
}
public void setVisible(Boolean flag){
dialog.setVisible(flag);
}
public static String getInput(String title, JFrame frame){
JDialogTest input = new JDialogTest (title, frame);
input.setVisible(true);
String text = input.textField.getText();
return text;
}
private void init(){
textField = new JTextField();
JButton okButton = new JButton("OK");
okButton.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
dialog.dispose();
}
});
dialog.setLayout(new GridLayout(2, 1, 5, 5));
dialog.add(textField);
dialog.add(okButton);
dialog.pack();
}
public static void main(String args []){
String s = getInput("Dialog",null);
System.out.println(s);
}
}
Here is one possible solution. Take special note of the changes to the JDialogTest constructor, getInput() and actionPerformed().
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JDialogTest extends JDialog implements ActionListener{
private JButton okButton;
private JTextField textField;
private JDialogTest(String title, JFrame frame){
super(frame, title, true);
this.setModal(true); // Set the dialog as modal
setDefaultCloseOperation(HIDE_ON_CLOSE);
setMinimumSize(new Dimension(200, 200));
init();
}
public static String getInput(String title, JFrame frame){
JDialogTest input = new JDialogTest(title, frame);
input.setVisible(true);
return input.textField.getText(); // If this is executed, the dialog box has closed
}
private void init(){
textField = new JTextField();
okButton = new JButton("OK");
okButton.addActionListener(this);
setLayout(new GridLayout(2, 1, 5, 5));
add(textField);
add(okButton);
pack();
}
#Override
public void actionPerformed(ActionEvent evt) {
System.out.println("click");
setVisible(false); // Close the modal dialog box
}
public static void main(String[] args) {
System.out.println(JDialogTest.getInput("Test", null));
}
}
Again you wan to use a a modal JDialog and then query it for the text it holds once it has been dealt with. Since the dialog is modal, your calling application will know when the button has been pressed, if you make the dialog invisible within the button's ActionListener. This will then return control to the calling program. Your calling program can then query the dialog by calling a public method that you give it (here I called it getText() then then returns the String held by the dialog's JTextField.
For example, using your code...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JDialogTest extends JDialog implements ActionListener {
// private boolean actionPerformed;
private JButton okButton;
private JTextField textField;
private JDialogTest(String title, JFrame frame) {
super(frame, title, true);
setDefaultCloseOperation(HIDE_ON_CLOSE);
setMinimumSize(new Dimension(200, 200));
init();
}
// public static String getInput(String title, JFrame frame) {
// JDialogTest input = new JDialogTest(title, frame);
// input.setVisible(true);
//
// while (true) {
// if (input.actionPerformed) {
// input.setVisible(false);
// String text = input.textField.getText();
// return text;
// }
// }
// }
private void init() {
textField = new JTextField();
okButton = new JButton("OK");
okButton.addActionListener(this);
// UGLY layout
setLayout(new GridLayout(2, 1, 5, 5));
add(textField);
add(okButton);
pack();
}
// I've added this method to allow outside methods to get text
public String getText() {
return textField.getText();
}
#Override
public void actionPerformed(ActionEvent evt) {
System.out.println("click");
// actionPerformed = true;
setVisible(false);
}
private static void createAndShowGui() {
final JTextField textField = new JTextField(20);
JFrame frame = new JFrame("Test JFrame");
final JDialogTest jDialogTest = new JDialogTest("Dialog", frame);
JButton button = new JButton(
new AbstractAction("Press Me to Show Dialog") {
#Override
public void actionPerformed(ActionEvent arg0) {
jDialogTest.setVisible(true); // code is frozen here
// until the dialog is no longer visible
// when code flow reaches here, we know that the dialog
// is no longer visible and
// we now can query our JDialog to get its text
textField.setText(jDialogTest.getText());
}
});
textField.setFocusable(false);
JPanel panel = new JPanel();
panel.add(button);
panel.add(textField);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Edit
If you want to display the dialog in a static way, change your getInput method to simply:
public static String getInput(String title, JFrame frame) {
JDialogTest input = new JDialogTest(title, frame);
input.setVisible(true);
return input.getText();
}
Changes to calling code:
final JTextField textField = new JTextField(20);
final JFrame frame = new JFrame("Test JFrame");
JButton button = new JButton(
new AbstractAction("Press Me to Show Dialog") {
#Override
public void actionPerformed(ActionEvent arg0) {
String result = JDialogTest.getInput("Get Input", frame);
textField.setText(result);
}
});
Edit 2
Note that JOptionPane works great:
String result = JOptionPane.showInputDialog(frame, "Please enter input:",
"Get Input", JOptionPane.PLAIN_MESSAGE);
Edit 3
You ask:
The solution in the edit seems to work but what method should I use in the action listener to close the JDialog and return the text?
The dialog button's ActionListener will make the dialog invisible by either calling setVisible(false) or dispose(). See my code above or nachokk's code for examples.

How to start 2nd Jframe by click menu item in 1st java windows application (Jframe)

I am using eclipse 4.2 with Java.
I have 2 java program : AppWin.java Form1.java
AppWin.java is gui windows application with menu/menu item1.
Form1.java is a Gui Jframe
I like to call Form1.java from AppWin.java by click the menu/menu item1.
When close Form1.java, it is back to AppWin.java.
This is something like MDIFORM. I really cannot find answer.
Please help , if you know eclipse menu.
Thanks
package top;
import java.awt.EventQueue;
public class AppWin {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AppWin window = new AppWin();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
...
With your help, I made a big step.
Thanks to all of you!
Next is my final demo, in windows 7, eclipse 4.2, java Gui
Hope it is helpful to others.
There are 3 parts : AppWin, Form1, Form2. AppWin is top main which call Form1 and Form2 with menu/item.
//1
package top;
import java.awt.EventQueue;
public class AppWin {
private JFrame frame;
private Form1 form1;
private Form2 form2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AppWin window = new AppWin();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public AppWin() {
initialize();
form1 = new Form1();
form2 = new Form2();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu mnNewMenu = new JMenu("Menu1");
menuBar.add(mnNewMenu);
JMenuItem mntmNewMenuItem = new JMenuItem("menu item1");
mntmNewMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
form1.setVisible(true);
}
});
mnNewMenu.add(mntmNewMenuItem);
JMenuItem mntmNewMenuItem_1 = new JMenuItem("menu item2");
mntmNewMenuItem_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
form2.setVisible(true);
}
});
mnNewMenu.add(mntmNewMenuItem_1);
JMenu mnNewMenu_1 = new JMenu("Menu2");
menuBar.add(mnNewMenu_1);
JMenuItem mntmMenuItem = new JMenuItem("Menu item3");
mnNewMenu_1.add(mntmMenuItem);
}
}
//2
package top;
import java.awt.BorderLayout;
public class Form1 extends JFrame {
private JPanel contentPane;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Form1 frame = new Form1();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Form1() {
// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JLabel lblNewLabel = new JLabel("this Form1");
contentPane.add(lblNewLabel, BorderLayout.WEST);
textField = new JTextField();
contentPane.add(textField, BorderLayout.CENTER);
textField.setColumns(10);
JButton btnNewButton = new JButton("New button");
contentPane.add(btnNewButton, BorderLayout.EAST);
}
}
//3
package top;
import java.awt.BorderLayout;
public class Form2 extends JDialog {
private final JPanel contentPanel = new JPanel();
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
Form2 dialog = new Form2();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public Form2() {
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setLayout(new FlowLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
{
JLabel lblThisForm = new JLabel("This Form2");
contentPanel.add(lblThisForm);
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("OK");
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
buttonPane.add(cancelButton);
}
}
}
}
Thanks again
You better use JDesktopPane + JInternalFrame for that purpose instead. Here's a quick sample.
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class JInternalFrameSample {
private JPanel pnlMain;
private JDesktopPane desk;
public JInternalFrameSample(){
pnlMain = new JPanel(new BorderLayout()){
#Override public Dimension getPreferredSize(){
return new Dimension(600,600);
}
};
desk = new JDesktopPane();
JMenuBar bar = new JMenuBar();
JMenu menu = new JMenu("Internal Frame");
JMenuItem item = new JMenuItem();
item.setAction(new AbstractAction("Create New") {
#Override
public void actionPerformed(ActionEvent arg0) {
JInternalFrame iFrame = new JInternalFrame("Created from Menu");
iFrame.setResizable(true);
iFrame.setClosable(true);
iFrame.setIconifiable(true);
iFrame.setSize(new Dimension(300, 300));
iFrame.setLocation(0, 0);
//iFrame.getContentPane().setLayout(new BorderLayout());
//iFrame.getContentPane().add( new YourCustomUI().getUI() );
iFrame.setVisible(true);
desk.add(iFrame);
}
});
menu.add(item);
bar.add(menu);
pnlMain.add(bar, BorderLayout.PAGE_START);
pnlMain.add(desk, BorderLayout.CENTER);
}
private JPanel getUI(){
return pnlMain;
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Demo");
frame.getContentPane().setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JInternalFrameSample().getUI());
frame.pack();
frame.setVisible(true);
}
});
}
}
See also : How to Use Internal Frames
If you do not like the JDesktopPane and JInternalFrame solution, just use your AppWin JFrame as is, and open modal JDialogs for the rest of the forms, instead of JFrames. Modal dialogs can float around the desktop and do not allow you to click your AppWin, until they are closed.
It is usually better to use just one main JFrame for an application, unless you have some wizard application that moves progressively from one JFrame to the other and back. Even with a wizard app, you can stick with one JFrame and update progressively just the ContentPane with JPanels.
Here is the AppWin JFrame:
public class AppWin extends javax.swing.JFrame {
private Form1 form1;
private Form1 form2;
...
private FormN formN;
public AppWin() {
initComponents();
form1 = new Form1(this, true);
form2 = new Form2(this, true);
...
formN = new FormN(this, true);
}
...
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
form1.setVisible(true);
}
And here is your Form1 JDialog:
public class Form1 extends javax.swing.JDialog {
public Form1(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();
}
...
private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {
setVisible(false);
}
I only use NetBeans for GUI building because that's more convenient. In the following I can tell you how to achieve what you want to do but I can't tell you how to layout all the components because NetBeans do that for me.
So basically you want to 1. show secondFrame by clicking a menuitem and then close mainFrame, 2. show mainFrame after closing secondFrame, yes? Then, the key is to pass the reference of mainFrame to secondFrame, and write your own method of formClosing event of secondFrame. Something like this:
In the menuItem method in your mainframe:
private void menuItemActionPerformed(java.awt.event.ActionEvent evt) {
//pass 'this' frame's (mainFrame) reference to the secondFrame
SecondFrame newFrame = new SecondFrame(this);
newFrame.setVisible(true); //show the secondFrame
this.setVisible(false); //hide this frame (mainFrame)
}
In your secondFrame:
public class SecondFrame extends javax.swing.JFrame {
private MainFrame mainFrame;
//define your own constructor that can use mainFrame's reference
public SecondFrame(MainFrame mainFrame) {
initComponents();
this.mainFrame = mainFrame;
}
private void initComponents(){
//bind your own event for closing second frame
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
/***********your stuff***************/
}
//show mainFrame when closing this frame and then dispose this frame
private void formWindowClosing(java.awt.event.WindowEvent evt) {
mainFrame.setVisible(true);
this.dispose();
}
}
The codes above is for disposing the secondFrame when closing it. If you just want to hide the secondFrame when closing for future use, then the codes will be slightly different. Let me know what you up to.
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
form1.setVisible(true);
dispose();
}

Categories