Close JFrame in actionPerformed-method (JFrame is private static void createGUI()) - java

I have copied most of my Code from an oracle-example, so I think at least the code I did not add should be correct and I don´t like to change that. But in the oracle-code I can not implement this line to properly close my JFrame: frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
private static void createGUI() {
JFrame frame = new JFrame("NameChooser");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
...
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createGUI();
}
});
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==skipButton){
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING)); WindowEvent.WINDOW_CLOSING)); // does not work ofc
}
}
How can I close my JFrame in the actionPerformed-method without destroying this correct way of opening a JFrame ?
Or is this oracle-code just suitable for examples and not for real applications ?

You should make frame an instance field like this:
private static JFrame frame;
private static void createGUI()
{
frame = new JFrame( "NameChooser" );
...
}

Related

i got a java frame when my job done with it i just want to pass other frame but when i try to open in frame1 its just open default frame

clickmetoframe2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e)
{
Frame2 f2=new Frame2();
f2.setVisible(true);
}
public static void main(String args[])
{
FirstFrame screen=new FirstFrame();
screen.setContentPane(new FirstFrame().Main);
screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
screen.setPreferredSize(new Dimension(600 ,600));
screen.setLocation(600,150);
screen.setVisible(true);
screen.pack();
}
second frame also got little features like there is a label says frame 2
public class Frame2 extends JFrame {
private JPanel panel1;
private JPanel KargoMain;
private JButton sehirkaydet;
public Frame2() {
sehirkaydet.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("we got this bro this is frame 2");
}
});
}
public static void main(String args[])
{
Frame2 fr2=new Frame2();
fr2.setContentPane(new KargoKaydet().KargoMain);
fr2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr2.setPreferredSize(new Dimension(600 ,600));
fr2.setLocation(600,150);
fr2.setVisible(true);
fr2.pack();
}
}
i don't even know where is my mistake in my code
it just open empty a panel i'm sorry i'm not that code i search everywhere
but i can't find anything about my way
All the code below Frame2 fr2=new Frame2();:
Frame2 fr2=new Frame2();
fr2.setContentPane(new KargoKaydet().KargoMain);
fr2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr2.setPreferredSize(new Dimension(600 ,600));
fr2.setLocation(600,150);
fr2.setVisible(true);
fr2.pack();
needs to be within the Frame2 fr2=new Frame2();
fr2.setContentPane(new KargoKaydet().KargoMain);
fr2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr2.setPreferredSize(new Dimension(600 ,600));
fr2.setLocation(600,150);
fr2.setVisible(true);
fr2.pack();
needs to be within the clickmetoframe2.addActionListener(....) code. So change this:
clickmetoframe2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Frame2 f2=new Frame2();
f2.setVisible(true);
}
}
to this:
clickmetoframe2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Frame2 fr2=new Frame2();
fr2.setContentPane(new KargoKaydet().KargoMain);
fr2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fr2.setPreferredSize(new Dimension(600 ,600));
fr2.setLocation(600,150);
fr2.pack();
fr2.setVisible(true);
}
}
This will allow the Frame2 JFrame to hold and display the new KargoKaydet().KargoMain JPanel
Having said that, note:
Best to avoid directly setting preferred sizes, if you can help it and instead let components and containers size themselves
Read The Use of Multiple JFrames, Good/Bad Practice? to see why throwing a whole bunch of JFrames at the user may not be the best idea, and what to do instead.

How do I add a Canvas3D to a JFrame without losing window focus?

I am trying to make a Swing GUI that includes some 3D stuff using Java3D's Canvas3D object. The problem is that it takes a while for a Canvas3D object to initialize, and I want the Swing GUI to come up right away. My solution to this problem is to initialize the Canvas3D in a separate thread, and then add it to the JFrame once it is initialized. However, when that separate thread adds the Canvas3D to the JFrame, the window loses focus for a moment, which is undesirable. How can I prevent that from happening? I have included a simple example to illustrate what I am trying to do:
public class Main extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Main()::setup);
}
private void setup() {
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
Thread thread = new Thread() {
#Override
public void run() {
Canvas3D canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
SwingUtilities.invokeLater(() -> {
Main.this.add(canvas); //the window loses focus for a moment here
Main.this.revalidate();
});
}
};
thread.start();
}
}
I am using Java3D 1.7.1.
I have modified my code as per R VISHAL's comment, but the problem still persists.
public class Main extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Main()::setup);
}
private void setup() {
setSize(600, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
SwingWorker<Canvas3D, Object> worker = new SwingWorker<Canvas3D, Object>() {
#Override
public Canvas3D doInBackground() {
return new Canvas3D(SimpleUniverse.getPreferredConfiguration());
}
#Override
public void done() {
try {
Main.this.add(get());
} catch (InterruptedException|ExecutionException e) {
throw new RuntimeException();
}
Main.this.requestFocusInWindow();
Main.this.revalidate();
}
};
worker.execute();
}
}
Ok so this might not be the answer but it was to large to put it as an comment
Use a JPanel having a CardLayout as your frame's contentpane, have one screen inside this panel set as the background[or whatever initial screen you want to display before the canvas is displayed], and then once the canvas is initialized add it to the content pane as the second screen and then call the CardLayout's show() method do display the canvas
public class Add
{
public static void main(String args[])
{
JFrame frame=new JFrame("Test");
JPanel mainPanel=new JPanel(new CardLayout());
mainPanel.addMouseListener(new MouseAdapter()
{
int count=0;
#Override
public void mouseClicked(MouseEvent m)
{
System.out.println("Focus "+(++count));
}
});
JPanel background=new JPanel();
background.setBackground(Color.WHITE);
mainPanel.add("Screen1",background); //Initial Screen To Show Something To The User While Canvas Is Being Initialized
frame.setContentPane(mainPanel);
SwingWorker worker=new SwingWorker<Canvas3D,Object>()
{
#Override
public Canvas3D doInBackground(){return new Canvas3D();}
#Override
public void done()
{
try
{
mainPanel.add("Screen2",get()); //Add Canvas To MainPanel
CardLayout layout=(CardLayout)mainPanel.getLayout();
layout.show(mainPanel,"Screen2"); //Remember This While Using CardLayout
}
catch(InterruptedException | ExecutionException ex){}
}
};
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
worker.execute();
}
private static final class Canvas3D extends JPanel
{
private Canvas3D()
{
super(new BorderLayout());
try{Thread.sleep(5000);}//Mimicing Long Operations
catch(Exception ex){}
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(0,0,500,500);
g.setFont(new Font("",Font.BOLD,15));
g.setColor(Color.RED);
g.drawString("CANVAS 3D",100,100);
}
}
}
Of course you would use your actual canvas3d instead of this custom one and there is no need to revalidate or requestFocus()
If you are still worried about focus you could always create an javax.swing.Timer class to request focus to your mainPanel or frame[Or Both See What Works For You] every second/or millisecond
Timer timer=new Timer(1000,new ActionListener() //milliseconds make it 1 if your are dead serious about focus
{
#Override
public void actionPerformed(ActionEvent e)
{
mainPanel.requestFocusInWindow();
frame.requestFocusInWindow(); //May not be required since we are already requesting focus in mainPanel
}
});
timer.start();
If you want to get even more paranoid about focus you could always add an focus listener
mainPanel.addFocusListener(new FocusListener()
{
#Override
public void focusLost(FocusEvent e)
{
mainPanel.requestFocusInWindow();
frame.requestFocusInWindow();
}
#Override
public void focusGained(FocusEvent e){}
});
If any of these suggestions did/didn't work comment below :)

why is there problem without windowClosing

I know that this program needs window Closing function to close the frame but I don't know why
explanation would be helpful
public class NoLayoutDemo {
public static void main(String[] args) {
Frame frame= new Frame("no layout teset");
frame.setLayout(null);
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
}
According to java documentation
WINDOW_CLOSING:
If the program doesn't explicitly hide or dispose the window while processing this
event, the window close operation is canceled.
Then you have to do the close operation yourself, otherwise the close operation has no effect.
Hope this helps
Maybe like this?
public class NoLayoutDemo {
public static void main(String[] args) {
Frame frame = new Frame("no layout teset");
frame.setLayout(null);
frame.setSize(200, 300);
frame.show();
frame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent evt) {
frame.dispose();
}
});
}
}

Making JButton in one class open seperate JDialog class

I am creating a quiz and have created a class with a JFrame which sort of acts like the main menu. On this menu, I have created a JButton which I want to open the seperate JDialog (which will contain the questions etc).
The JDialog is a seperate class called questionDialog.java
I believe you have to implement an action listener calling setVisible(true) however when I do that, I get a cannot make static reference to non-static method setvisible error.
Any help would be greatly appreciated, I am using eclipse and Jigloo for the GUI
here is my code in my main menu JFrame class, specifically the code for the button I want to open the new JDialog
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
NewJFrame inst = new NewJFrame();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
startButton = new JButton();
getContentPane().add(startButton);
startButton.setText("Start Quiz");
startButton.setBounds(454, 239, 65, 23);
And here is the code which gives me the error
startButton = new JButton();
getContentPane().add(startButton);
startButton.setText("Start Quiz");
startButton.setBounds(454, 239, 65, 23);
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionevent)
{
questionDialog.setVisible(true);
}
});
Here is the code from the seperate JDialog class
package ZillionaireGUI;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class questionDialog extends javax.swing.JDialog {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
questionDialog inst = new questionDialog(frame);
inst.setVisible(true);
}
});
}
public questionDialog(JFrame frame) {
super(frame);
initGUI();
}
private void initGUI() {
try {
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Here's what you should do.
Get rid of the main method in the JDialog class. Your application should only have one main method, and that should be in your JFrame class.
Don't create a new JFrame to pass it to your dialog.
To open it on a button click just create a new questionDialog() passing the current frame to it. Something like this
public class MyFrame extends JFrame {
public MyFrame() {
JButton but = new JButton("but");
but.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
QuestionDialog dialog = new QuestionDialog(MyFrame.this);
dialog.setVisible(true);
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new MyFrame();
}
});
}
}
public class QuestionDialog extends JDialog {
public QuestionDialog(Frame parent) {
super(parent);
}
}
Bonus
You are getting the error doing questionDialog.setVisible(true) because setVisible is an instance method and you are trying to call it in a static way. You need to create a new instance of your dialog class to call it.
Use Java naming convention. Class names begin with capital letters.questionDialog → QuestionDialog
In your main menu write the following
startButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
questionDialog inst = new questionDialog(frame);
inst.setVisible(true);
}
});
}
});
I think it is likely that you are trying to do something to a non-static member while you are in main (which is a static method). You should just use main to create an instance and then call some method of that instance. I've put some working code below:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Zillionaire extends JFrame implements ActionListener {
JButton startButton;
public static void main(String[] args) {
Zillionaire zillionaire = new Zillionaire();
zillionaire.init();
}
private void init() {
startButton = new JButton();
// Removed: we just use add now, and bets to do this last.
// getContentPane().add(startButton);
startButton.setText("Start Quiz");
startButton.setBounds(454, 239, 65, 23);
startButton.addActionListener(this);
// Add after the button is configured, not before
add(startButton);
// This just makes our JFrame pretty and visible
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
// Anything that implements the ActionListener interface can listen
// for the button being pressed and activate the JDialog
#Override
public void actionPerformed(ActionEvent ae) {
System.out.println("Button pressed, but Dialog doesn't do much");
QuestionDialog questionDialog = new QuestionDialog(this);
}
}
class QuestionDialog extends javax.swing.JDialog {
public QuestionDialog(JFrame frame) {
super(frame);
initGUI();
}
// Set it visible when we make our GUI
private void initGUI() {
try {
setSize(400, 300);
// Added so we can see something
add(new JLabel("I should add something!"));
setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}

JFrame size is too small

i have created a JFrame in netbeans. But when i run the program, the Jframe size is too small.
here is my code.
import javax.swing.JFrame;
public class Window {
private static void demo()
{
JFrame frame =new JFrame();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run()
{
demo();
}
});
}
}
You can use frame.setSize(width, height) in order to set its size or frame.setBounds(x, y, width, height) for setting both the location and size.
A better choice would be to call frame.pack() after you add some components to its content pane.
Try this way...
Use the setSize(width, height) method of JFrame.
public class Myframe extends JFrame{
public Myframe(){
this.setSize(300,300);
}
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
public void run(){
Myframe f = new Myframe("Frame");
f.setVisible(true);
}
});
}
}
If you want to maximize it you could try
this.setVisible(false);
this.setExtendedState(MAXIMIZED_BOTH);
this.setVisible(true);
this.setResizable(false);
Else for some specific size use
this.setSize(width,height);
You just need to add one line line so that you can give your frame a size.The line is
frame.setSize(300,300);
Here is the full code:
import javax.swing.JFrame;
public class Window {
private static void demo()
{
JFrame frame =new JFrame();
frame.setSize(300,300);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run()
{
demo();
}
});
}
}
You must have a public component method like
public gestion_name_of_class() {
initComponents();
}

Categories

Resources