Move from a frame to a frame in Swing [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Unfortunately, our team leader has decided that we'll use Swing for this light desktop app. I have no previous experience in working with Swing. I will be working on the GUI side of the project. I've already created a frame with elements within it and wrote the logic in the event listeners. Now I want to redirect the user to a new frame after he's logged in.
How do I do that? Thanks
P.S. I'd appreciate if you could point me to a good tutorial for beginners

It sounds like you have your terminology mixed up. When we hear "Frame" we think JFrame which is equivalent to "Window". So most of the time we'd recommend not using multiple windows, but changing the content of the window. The content is generally made with a "JPanel".
So generally, you set up your JFrame, you set the content with this:
JPanel loginPanel = new JPanel();
frame.setContentPane(loginPanel);
If you want to replace your login panel with a new panel, just pass the new panel to that function:
JPanel mainMenuPanel = new JPanel();
frame.setContentPane(mainMenuPanel);
(of course you want some content in those panels)
Here's a simple example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PanelRetriever{
Box panel1;
JPanel panel2;
public PanelRetriever(final JFrame frame){
//Build the first login panel
panel1 = Box.createVerticalBox();
panel1.setBackground(Color.orange);
panel1.setOpaque(true);
panel1.add(Box.createVerticalGlue());
panel1.add(new JTextField(10));
JButton login = new JButton("Login");
login.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent arg0) {
frame.setContentPane(getPanel2());
frame.validate();
}});
panel1.add(login);
panel1.add(Box.createVerticalGlue());
//Build second panel
panel2 = new JPanel();
panel2.setBackground(Color.blue);
panel2.setOpaque(true);
JButton logout = new JButton("Logout");
logout.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
frame.setContentPane(getPanel1());
frame.validate();
}});
panel2.add(logout, BorderLayout.CENTER);
}
public Container getPanel1(){
return panel1;
}
public Container getPanel2(){
return panel2;
}
public static void main(String args[])
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new JFrame();
PanelRetriever pr = new PanelRetriever(frame);
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setContentPane(pr.getPanel1());
frame.setPreferredSize(new Dimension(500, 400));
frame.pack();
frame.setVisible(true);
}
});
}
}

Your frame should stay the same, just create and show new JPanel instead old one.
See below JPanel painting process:

As #Gilbert Le Blanc has pointed out (+1 to him). In Swing it is bad practice to use multiple JFrames.
To accomplish what you want:
Use CardLayout which allows dynamic switching of components
Or use JFrame#removeAll() and add a new JPanel (+1 Fess)
Try using JDialog/JOptionPane and then redirect to main JFrame
here is a good link on the topic: How to Make Dialogs

In Swing, you have one JFrame. You use JDialogs to get user id and password input from the user.
Here's a link to the Oracle Swing Tutorial.

You can just create a new frame in much the same way as you already have;
MyFrame f = new MyFrame(); //MyFrame extends javax.swing.JFrame
And then bring it to the front with;
f.setVisible(true);
f.toFront();

Related

How to display one Jframe at a time? [duplicate]

I'm trying to make a little game that will first show the player a simple login screen where they can enter their name (I will need it later to store their game state info), let them pick a difficulty level etc, and will only show the main game screen once the player has clicked the play button. I'd also like to allow the player to navigate to a (hopefully for them rather large) trophy collection, likewise in what will appear to them to be a new screen.
So far I have a main game window with a grid layout and a game in it that works (Yay for me!). Now I want to add the above functionality.
How do I go about doing this? I don't think I want to go the multiple JFrame route as I only want one icon visible in the taskbar at a time (or would setting their visibility to false effect the icon too?) Do I instead make and destroy layouts or panels or something like that?
What are my options? How can I control what content is being displayed? Especially given my newbie skills?
A simple modal dialog such as a JDialog should work well here. The main GUI which will likely be a JFrame can be invisible when the dialog is called, and then set to visible (assuming that the log-on was successful) once the dialog completes. If the dialog is modal, you'll know exactly when the user has closed the dialog as the code will continue right after the line where you call setVisible(true) on the dialog. Note that the GUI held by a JDialog can be every bit as complex and rich as that held by a JFrame.
Another option is to use one GUI/JFrame but swap views (JPanels) in the main GUI via a CardLayout. This could work quite well and is easy to implement. Check out the CardLayout tutorial for more.
Oh, and welcome to stackoverflow.com!
Here is an example of a Login Dialog as #HovercraftFullOfEels suggested.
Username: stackoverflow Password: stackoverflow
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import javax.swing.*;
public class TestFrame extends JFrame {
private PassWordDialog passDialog;
public TestFrame() {
passDialog = new PassWordDialog(this, true);
passDialog.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new TestFrame();
frame.getContentPane().setBackground(Color.BLACK);
frame.setTitle("Logged In");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
});
}
}
class PassWordDialog extends JDialog {
private final JLabel jlblUsername = new JLabel("Username");
private final JLabel jlblPassword = new JLabel("Password");
private final JTextField jtfUsername = new JTextField(15);
private final JPasswordField jpfPassword = new JPasswordField();
private final JButton jbtOk = new JButton("Login");
private final JButton jbtCancel = new JButton("Cancel");
private final JLabel jlblStatus = new JLabel(" ");
public PassWordDialog() {
this(null, true);
}
public PassWordDialog(final JFrame parent, boolean modal) {
super(parent, modal);
JPanel p3 = new JPanel(new GridLayout(2, 1));
p3.add(jlblUsername);
p3.add(jlblPassword);
JPanel p4 = new JPanel(new GridLayout(2, 1));
p4.add(jtfUsername);
p4.add(jpfPassword);
JPanel p1 = new JPanel();
p1.add(p3);
p1.add(p4);
JPanel p2 = new JPanel();
p2.add(jbtOk);
p2.add(jbtCancel);
JPanel p5 = new JPanel(new BorderLayout());
p5.add(p2, BorderLayout.CENTER);
p5.add(jlblStatus, BorderLayout.NORTH);
jlblStatus.setForeground(Color.RED);
jlblStatus.setHorizontalAlignment(SwingConstants.CENTER);
setLayout(new BorderLayout());
add(p1, BorderLayout.CENTER);
add(p5, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
jbtOk.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (Arrays.equals("stackoverflow".toCharArray(), jpfPassword.getPassword())
&& "stackoverflow".equals(jtfUsername.getText())) {
parent.setVisible(true);
setVisible(false);
} else {
jlblStatus.setText("Invalid username or password");
}
}
});
jbtCancel.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
parent.dispose();
System.exit(0);
}
});
}
}
I suggest you insert the following code:
JFrame f = new JFrame();
JTextField text = new JTextField(15); //the 15 sets the size of the text field
JPanel p = new JPanel();
JButton b = new JButton("Login");
f.add(p); //so you can add more stuff to the JFrame
f.setSize(250,150);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Insert that when you want to add the stuff in. Next we will add all the stuff to the JPanel:
p.add(text);
p.add(b);
Now we add the ActionListeners to make the JButtons to work:
b.addActionListener(this);
public void actionPerforemed(ActionEvent e)
{
//Get the text of the JTextField
String TEXT = text.getText();
}
Don't forget to import the following if you haven't already:
import java.awt.event*;
import java.awt.*; //Just in case we need it
import java.x.swing.*;
I hope everything i said makes sense, because sometimes i don't (especially when I'm talking coding/Java) All the importing (if you didn't know) goes at the top of your code.
Instead of adding the game directly to JFrame, you can add your content to JPanel (let's call it GamePanel) and add this panel to the frame. Do the same thing for login screen: add all content to JPanel (LoginPanel) and add it to frame. When your game will start, you should do the following:
Add LoginPanel to frame
Get user input and load it's details
Add GamePanel and destroy LoginPanel (since it will be quite fast to re-create new one, so you don't need to keep it memory).

Java Swing Tabs with multiple panels [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
is it possible to create a (new)panel inside a tab when you press a button inside the (current)panel?So that the layout is different to the (current) panel? For example the button is just "next".
i can't find anything on google, i have the feeling that my approach is not possible. I tried to illustrate it which a picture but i don't have enough points.
Yes this is definitely possible as Tim B described using a CardLayout. Take a look and maybe this example will help out a bit.
public class JavaApplication2 extends JFrame {
private JPanel mainPanel, cpOne, cpTwo;
private JButton btnContine, btnGoBack;
private CardLayout c1;
public JavaApplication2()
{
super("Card Layout");
mainPanel = new JPanel(new CardLayout());
mainPanel.add(cardOne(), "card1");
mainPanel.add(cardTwo(), "card2");
c1 = (CardLayout) (mainPanel.getLayout());
add(mainPanel);
setSize(200,200);
setVisible(true);
btnContine.addActionListener((ActionEvent e) -> {
c1.show(mainPanel,"card2");
});
btnGoBack.addActionListener((ActionEvent e) -> {
c1.show(mainPanel,"card1");
});
}
private JPanel cardOne()
{
cpOne = new JPanel();
btnContine = new JButton("Next Panel");
cpOne.add(btnContine);
cpOne.add(new JLabel("First Panel"));
return cpOne;
}
private JPanel cardTwo()
{
cpTwo = new JPanel();
btnGoBack = new JButton("Previous Panel");
cpTwo.add(btnGoBack);
cpTwo.add(new JLabel("SECOND PANEL!!!"));
return cpTwo;
}
public static void main(String[] args) {
JavaApplication2 jp = new JavaApplication2();
}
}
If you look at my example we are just creating a frame and adding one main panel to this frame. To that main panel we are setting a CARD LAYOUT as the layout manager. From here we can add as many cards as our heart desires! So there are just some little methods to make our panels with buttons. We add them to the main panel as mainPanel.add(cardOne,"card1") by showing the method as what to add as the card and then naming it card1. When we run the above program you get something like this:
and after clicking the button we get this:
Yes, you can easily do this. Use a CardLayout, place all the panels you want inside that layout, and then switch between them when they press the button.
See this for more information: https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html

Java - How to make contents of JFrame appear in the same Window instead of multiple windows

I am new to java and am getting to the advanced level of it, i have a problem in the GUI Controls, i made a button that when clicked opens up a new window like this:
JButton b = new JButton("Open New Window");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Window w = new Window();
w.setVisible(true);
}
});
this window contains other objects but i have been thinking of making the button in such a way that instead of opening a new JFrame, it opens everything in that same window without opening a new window, honestly i dont know how to do so please could i get some professional help
I think you want a card layout for this situation. Here is some code which should point you in the right direction.
class MyFrame extends JFrame {
public MyFrame() {
JComponent allMyStuff = new JComponent();
JComponent allMyOtherStuff = new JComponent();
this.getContentPane().setLayout(new CardLayout());
this.getContentPane().add(allMyStuff, "1");
this.getContentPane().add(allMyOtherStuff, "2");
CardLayout cl = (CardLayout) (this.getContentPane().getLayout());
cl.show(this.getContentPane(), "1");
JButton b = new JButton("Open New Window"); //add somewhere to first compoonent
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CardLayout cl = (CardLayout) (this.getContentPane().getLayout());
cl.show(this.getContentPane(), "2");
}
});
}
}
I doubt the code runs but generally it holds the idea. You have stuff in one panel, and stuff in another panel, and you just want to switch between the two. The button of course needs to be added in the first panel (allMyStuff) somewhere.
I"m not clear on what it is exactly that you want to show in the GUI when the button is pressed, but perhaps you should consider creating different JPanel "views" and swap these views in the GUI using a CardLayout.
For example, check out these StackOverflow questions and answers:
Java CardLayout Main Menu Problem
Change size of JPanel using CardLayout
Java CardLayout JPanel moves up, when second JPanel added
Java swing; How to toggle panel's visibility?
Clear components of JFrame and add new componets on the same JFrame
gui multiple frames switch
JLabel displaying countdown, java
Within the action listener that you have introduced, you have the possibility to access to instance variables. Therefore you can add further elements to your GUI if you want. I've done a small demo, maybe this is kind of, what you want to do. In order to make your GUI better, you should consider of using layout managers.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GUI {
JFrame frame;
JButton btn;
JButton compToAdd;
public GUI() {
frame = new JFrame("Testwindow");
frame.setSize(500, 500);
frame.setLayout(null);
btn = new JButton("test btn");
btn.setBounds(20, 20, 200, 200);
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
compToAdd = new JButton("new Button");
compToAdd.setBounds(20, 220, 200, 200);
frame.add(compToAdd);
frame.repaint();
}
});
frame.add(btn);
frame.setVisible(true);
}
public static void main(String[] args) {
GUI gui = new GUI();
}
}

How do you make an input that is entered in a textfield in the first jframe appear on a jlabel on another jframe? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
jframe 1: the user will input the information that is asked on the text fields and when they click the get started button, the input should show in another jframe.
jframe 2: the information that was inputted in the text fields on jframe1 should appear on the jlabels.
Is it considered good practice to use two multiple frames? NO
Are there alternatives to using two frames? Yes
What are the alternatives? CardLayout, modal JDialog, among others
Will I still help you solve this query? Sure, why not?
Have an instance of the second frame in the first frame. Pass the information to the second JFrame constructor. It's that simple.
public class Frame1 extends JFrame {
String text1;
String text2;
Frame2 frame2; <--- second frame
....
public void actionPerformed(ActionEvent e) {
text1 = textField1.getText();
text2 = textField2.getText();
frame2 = new Frame2(text1, text2);
}
}
public class Frame2 extends JFrame {
String text1;
String text2;
public Frame2(String text1, String text2){
this.text1 = text1;
this.text2 = text2;
}
}
When the second frame is instantiated in the actionPerformed, It will make the second frame appear. It is also being passed the information from the text fields. You may also want to dispose of the first frame.
A modal JDialog is just as easy to make as a JFrame it's the same structure. Only difference is you can set it up to be modal (meaning nothing else that it not the JDialog) can be accessed. See this answer to see hoe to set up a JDialog for a login
Here's a very simple program using a JDialog with your requirements
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class JDialogDemo {
MyDialog dialog;
JLabel label;
JFrame frame;
public JDialogDemo() {
label = new JLabel(" ");
frame = new JFrame("Hello World");
frame.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
dialog = new MyDialog(frame, true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new JDialogDemo();
}
});
}
class MyDialog extends JDialog {
private JButton button = new JButton("Submit");
private JTextField jtf1;
private JTextField jtf2;
public MyDialog(final Frame frame, boolean modal) {
super(frame, true);
jtf1 = new JTextField(15);
jtf2 = new JTextField(15);
add(button, BorderLayout.SOUTH);
add(jtf1, BorderLayout.CENTER);
add(jtf2, BorderLayout.NORTH);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String text1 = jtf1.getText();
String text2 = jtf2.getText();
label.setText(text1 + " " + text2);
dispose();
frame.revalidate();
frame.repaint();
frame.setVisible(true);
}
});
pack();
setVisible(true);
}
}
}
It should be fairly easy to follow. Let me know if you have any questions about it
If jframe 1 call jframe 2, then u can create a constructor in jframe 2, and pass this values.
Or you can do that by haveing a object contains jframe 1, then get in the second frame, get them by getter methods.
But for a good practice you should use single jframe and dialogs.
JFrame frame1 = new JFrame("frame1");
JPanel panel = new JPanel();
final JTextField textField = new JTextField("Text Here");
JButton button = new JButton("Copy label to other frame");
panel.add(textField);
panel.add(button);
frame1.add(panel);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.pack();
frame1.setLocationByPlatform(true);
frame1.setVisible(true);
JFrame frame2 = new JFrame("frame2");
final JLabel label2 = new JLabel("Label 2 Text");
frame2.add(label2);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.pack();
frame2.setLocationRelativeTo(null);
frame2.setVisible(true);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
label2.setText(textField.getText());
}
});
Of course, your question isn't worded very well to show what you're really trying to accomplish or better yet, what you've already tried. Nevertheless, this code does what your specifications ask for.
If you want to put on jlabels the text that the user has inputted, you must create first the jframe1, and then jframe1 will create jframe2 passing it the text. The jframe2 will create the jlabels setting the text in the constructor.
If you want that jframe1 desappears, I think you can't close it (because if you close it, like jframe1 created jframe2, this one will be closed too). You can hide the jframe1 setting it as not visible when the user has clicked the get started button.
jframe1.setVisible(false);

How can I stack/overlay jPanels in Java?

I am really new to GUI programming in Java, I did a lot of research and I couldn't find an answer to this problem.
I have a simple JFrame with a menu, and inside this JFrame I have a JPanel with a log in form (were users input their username and password), and then I want to change that JPanel to another JPanel depending on what users want to do.
What would be the best way of doing this? I think that stacking JPanels is OK. But after I add new JLayeredPanels in Netbeans they don't stack. I read somewhere that I should use Z ordering or something like that, but I can't find it on the designer view.
Well, thank you very much for your patience!
CardLayout class has a useful API that can serve your requirements. Using methods like next(), first(), last() can be helpful.
I've prepared a simple demonstration of changing panels within a parent panel and/or frame.
Take a look at it:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PanelChanger implements ActionListener
{
JPanel panels;
public void init(Container pane)
{
JButton switcher = new JButton("Switch Active Panel!");
switcher.addActionListener(this);
JPanel login = new JPanel();
login.setBackground(Color.CYAN);
login.add(new JLabel("Welcome to login panel."));
JPanel another = new JPanel();
another.setBackground(Color.GREEN);
another.add(new JLabel("Yeah, this is another panel."));
panels = new JPanel(new CardLayout());
panels.add(login);
panels.add(another);
pane.add(switcher, BorderLayout.PAGE_START);
pane.add(panels, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent evt)
{
CardLayout layout = (CardLayout)(panels.getLayout());
layout.next(panels);
}
public static void main(String[] args)
{
JFrame frame = new JFrame("CardLayoutDemo");
PanelChanger changer = new PanelChanger();
changer.init(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
}

Categories