Java Project - how to freeze Frame - java

Good day!
I am doing a game in Java. My menu button includes New Game, HighScore, About and Quit.
But before the user can proceed to the main game, he needs to type his name first. I used this code as follows:
private void btnNewGameMouseClicked(java.awt.event.MouseEvent evt) {
Player p1 = new Player();
this.setVisible(false); // I must replace this code
p1.setVisible(true);
}
My problem is, I don't want to make the main menu hidden. I want it to freeze and cannot be accessed when the player name is being asked.
My Main menu frame is bigger than the player frame.. Of course, I can just delete the code this.setVisible(false) but the problem is I can still access the main menu when clicked... I want the main menu to freeze and cannot be accessed when the player frame pops up.. (See image below) Please help me. Thank you.

What you want to do is make your player frame a modal dialog. You would want to make it a subclass of JDialog rather than JFrame or whatever you're using and set it to be modal either using its setModal method or with one of JDialog's constructors. For example:
public Player(JFrame owner) {
super(owner, true); // makes the dialog modal
// ...
}
Then you could create the dialog from the main frame like:
Player p1 = new Player(this);
When you call p1.setVisible(true), the main frame will be blocked and unclickable.

private void btnNewGameMouseClicked(java.awt.event.MouseEvent evt)
{
Player p1 = new Player();
p1.setVisible(true);
setEnabled(false);
}
/*
setEnabled(boolean b) (java.​awt.​Component)
Enables or disables this component, depending on the value of the parameter b. An enabled component can respond to user input and generate events. Components are enabled initially bydefault.enter code here
*/

Related

jInternalFrame not brought to the front

I have a JDesktopPane which contains a number of JInternalFrames. The first time I press one button to visible jinternalframe1 and second button to visible jinternalframe2, it appear above the main window without problems. However, if I press one of the buttons to Reopen jinternalframe1 or jinternalframe2, they are not brought in front of the main window... EDIT: actually, i can't do anything with jinternalframe on a button click...i can only click once on the button and then no operation can be perform on the jinternalframe through the button..why it doesn't work!!
this is the coding of button1...
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
jinternalframe1 frame1 = new jinternalframe1();
try {
if(Allow.flag == false) {
desktopPane.add(frame1);
frame1.setVisible(true);
Allow.flag = true;
} else if(Allow.flag == true) {
frame1.setSelected(true);
}
} catch(PropertyVetoException e) {
System.out.println(e);
}
}
Allow.java
public class Allow {
static boolean flag = false;
}
Every time you click on the button you create a new JInternalFrame object, but you only ever add the first internal frame that you create to the desktop pane.
Don't keep creating new internal frame objects. I would guess you should only create the internal frame if your "frame1" variable is null.
If you need more help then post a proper SSCCE that demonstrates the problem.

How to have next line of GUI read after JFrame is closed (Java)

Basically I'm making a music player in Java using Eclipse, and I have a JButton on the main GUI called "add song" - the user clicks this and another JFrame appears, allowing the user to click "browse" and select an mp3 file from the computer. I then store the data as a musicFile object I created, and I want to send this information back to the main function. My code for the "add song" action listener is the following:
private ActionListener song(final JButton button)
{
return new ActionListener(){
public void actionPerformed(ActionEvent event)
{
addSongGUI addSong = new addSongGUI(); //the JFrame that opens
//once the user presses the "add song" button
listOfSongs.add(addSong.musicFile); //the addSongGUI has a musicFile variable that I want to read and get information from
String songName = addSong.musicFile.getSongName();
//... and do more stuff
}
};
}
When this runs, "String songName = addSong.musicFile.getSongName();" gives me a null pointer exception, because it tries to read the musicFile from the addSongGUI right away, before the user can pick a song to set the musicFile. So, how can I wait until the user picks a song, closes the window, and then have this line of code read (what can I do to get rid of this null pointer exception)? Thanks.
As noted, the correct and easy solution is not to display a JFrame when you want a modal dialog -- use a modal JDialog instead:
private ActionListener song(final JButton button) {
return new ActionListener(){
public void actionPerformed(ActionEvent event) {
// AddSongDialog is a modal JDialog
AddSongDialog addSong = new AddSongDialog(mainJFrame);
addSong.setVisible(true); // show it -- this pauses flow of code here
String songName = addSong.musicFile.getSongName();
//... and do more stuff
}
};
}
Again, addSongDialog is a modal JDialog, which is why you would need to pass in the application's main JFrame into it, since the JFrame (or parent JDialog) will be needed when calling the JDailog's super constructor in your constructor.
An alternative and far weaker solution is to use a JFrame and add a WindowListener to it, but why do that when the JDialog solution works so easily and simply?

How to connect two JFrames in netbeans without using swing?

I am make a project on cars. How can I make distributor frame popup and cars frame not visible and close automatic? Kindly send any solution in simple and effective way.
I have done coding this way:-
{
Cars frm1=new Cars();
Distributor frm2=new Distributor();
frm2.setVisible(true);
frm1.setVisible(false);
frm1.setDefaultCloseOperation(frm1.DISPOSE_ON_CLOSE);
}
".Please help me to how I can make distributor frame popup and cars frame is not visible and close automatic."
Ok so in Netbeans GUI Builder, you may want to do the following (this is assuming you have created two separate JFrame form files
In the frame that is the launching program (we'll call it MyFrame1) add a button to it (we'll call it jButton1)
Add a listener to the button, then the following code should be auto-generated
public void jButton1ActionPerforemd(javax.swing.ActionEvent evt) {
}
In that actionPerformed, just instantiate the second frame (we'll call it MyFrame2) and setVisible(false) to MyFrame1. MyFrame2 should already be visible upon instantiation, so you wouldn't have to setVisisble(true) on it
public void jButton1ActionPerforemd(javax.swing.ActionEvent evt) {
MyFrame2 frame2 = new MyFrame2();
MyFrame1.this.setVisible(false);
// You can also use MyFrame1.this.dispose(); dependind if you ever need to use that frame again
}
I think this should work
you need to setVisible Jframe2 as true...so it can apear on output sceen
public void jButton1ActionPerforemd(javax.swing.ActionEvent evt)
{
myFrame2 frame2=new myframe2();
myframe1.this.setVisible(false);
frame2.setVisible(true);
}
create action event for the button such that when when you click will take
you
to the next page for my case next page is nextjFrame
private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {
setVisible(false);
nextjFrame ob=new nextjFrame();
ob.setVisible(true);
}
private void BTNConvertActionPerformed(java.awt.event.ActionEvent evt) {
/*
This is the action performed event for my Button "BTNConvert"
*/
java.awt.EventQueue.invokeLater
(new Runnable()
{
public void run()
{
new JFrame2().setVisible(true);
}
});
/*
This will set the second Frame Visible.
*/
JFrame1.this.setVisible(false);
/*
This should set the first frame invisible or whatever. Any other code should be
written before the curly brace below.
*/
}
//You're Welcome.

Perform action when button clicked once and do a different action when clicked twice, three times etc. (Netbeans, Java)

I am fairly new to Java programming and want to make a basic game that shows an image when clicked once, a different image when clicked twice and etc.
I know how to do all this but I don't know how to keep track of how any clicks and then do an actions based on how many clicks have been done (Hard to explain, my apologies...)
I ... want to make a basic game that shows an image when clicked once, a different image when clicked twice and etc. I know how to do all this but I don't know how to keep track of how any clicks
As per my comment, give the class with the ActionListener an int field, say called buttonCount, and increment it each time the button is pressed -- inside of the button ActionListener's actionPerformed method: buttonCount++
and then do an actions based on how many clicks have been done (Hard to explain, my apologies...)
In the ActionListener's actionPerformed method change the image displayed. How you change it all depends on how you show it, something that you've yet to show us, and so I can't give you any code.
One way to make it easy is to create an ArrayList of ImageIcons to hold your images (as ImageIcons of course), and then call get(buttonCount) on the ArrayList to get the appropriate ImageIcon and display it in a JLabel via its setIcon(...) method. Make sure that the buttonCount is less than the size of the ArrayList so as not to get an ArrayIndexOutOfBoundsException. One way to do this is to mod your buttonCount by the size of the ArrayList. This will allow you to cycle through your collection of images.
Again, you will want to read the Swing tutorials on how to use JButtons and then break down your big problem into small steps, trying to solve each step one at a time.
Again if you need greater detail and more specific help, then you must show what you've tried and explain in detail what problems you may be having with it. It is my sincere believe and philosophy that you learn most by by forcing your brain to do new and unfamiliar things, by mental effort and sweat. So have at it, you've nothing to lose.
You can count the the mouse clicks in this way. By using an if-else or switch case you can display the images.
public class ButtonStart extends Frame {
private int mouseclicked = 0;
TextField objTextField;
public static void main(String args[]) {
ButtonStart BS = new ButtonStart();
}
public ButtonStart() {
Frame objFrame;
Button objButton;
TextField objTextField;
objFrame = new Frame("Clicking Buttons");
objButton = new Button("Click me!");
objTextField = new TextField("0");
objFrame.addMouseListener(new MyMouseListener());
objFrame.add(objButton);
objFrame.add(objTextField);
objFrame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
public class MyMouseListener extends MouseAdapter {
public void mouseClicked(MouseEvent me) {
int mouseclicked = me.getClickCount();
objTextField.setText("Mouse clicked this many times:"
+ mouseclicked);
}
}
}

Closing the Main JFrame

I have a main jFrame with the help of which i press button and open new JFrames but the problem is that when i open other JFrame the previous ones still remains there where as what i want is that when i press next button then i move forward to the new JFrame (the previous one should not be there) and when i press previous button i move back to the previous JFrame.
I know there are functions of dispose,they do well like jframe1.dispose() but i dont get it how to hide the very first JFrame whose code in the main is written like this
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run()
{
new GraphicalInterface().setVisible(true);
}
});
}
how do i set this as .setVisible(false) in the button code?
You need to retain a reference to the JFrame, so you can set it's visibility later.
private JFrame myFrame;
public void run() {
myFrame = new GUI();
myFrame.setVisible(true);
}
public void hide() {
myFrame.setVisible(false);
}
Note that a JFrame is a top-level container, you are only really supposed to have one per application. It may be better if instead of using multiple JFrames you use just one, and swap in various JPanels instead.
It would safe resources if you keep just one frame and set a new panel as content.
Your question was how to handle the reference to the frame? Please provide the code where the next frame is created.
You could assign your GUI (extends JFrame I suppose) to a variable and call .setVisible(false) on that object. Since your object from the code above is more or less anonymous, you won't have access on that.
You could also check this.

Categories