How to open another frame in main frame? - java

I've created two jframes main_frame and sub_frame where main_frame holds a jbutton. Now i want that button to open sub_frame in the same frame(main_frame) and set main_frame disable until sub_frame is opened.
Note that I dont want main_frame to setVisible(false).

I suggest you use a CardLayout
Instead of multiple JFrames, you have multiple JPanels and switch between them.
Here is an example:
package main.frames;
import javax.swing.*;
import java.awt.*;
public class MainFrame extends JFrame
{
static JPanel homeContainer;
static JPanel homePanel;
static JPanel otherPanel;
static CardLayout cl;
public MainFrame()
{
JButton showOtherPanelBtn = new JButton("Show Other Panel");
JButton backToHomeBtn = new JButton("Show Home Panel");
cl = new CardLayout(5, 5);
homeContainer = new JPanel(cl);
homeContainer.setBackground(Color.black);
homePanel = new JPanel();
homePanel.setBackground(Color.blue);
homePanel.add(showOtherPanelBtn);
homeContainer.add(homePanel, "Home");
otherPanel = new JPanel();
otherPanel.setBackground(Color.green);
otherPanel.add(backToHomeBtn);
homeContainer.add(otherPanel, "Other Panel");
showOtherPanelBtn.addActionListener(e -> cl.show(homeContainer, "Other Panel"));
backToHomeBtn.addActionListener(e -> cl.show(homeContainer, "Home"));
add(homeContainer);
cl.show(homeContainer, "Home");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setTitle("CardLayout Example");
pack();
setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(MainFrame::new);
}
}

It is really easy, just call the constructor and set visibility:
SubFrameClass frame = new SubFrameClass();
frame.setVisible(true);

Related

Java - Why doesn't my buttons show in the panel?

I'm trying to learn Java Swing. Right now, I'm making a simple program and I need to make a button. I have two classes: driver and swing.
I create the button and import the javax.swing.JButton and added the button. Finally, the button added to the panel but Idk why I just get the panel?
Can anyone help me, please? Here's my code:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Swing extends JFrame {
private JFrame f;
private JButton button;
private JLabel label;
private JPanel panel;
public Swing() {
}
public Swing(String titleName) {
creatButton();
creatFrame(titleName);
}
public void creatButton() {
JButton btn = new JButton("click me");
JPanel panel = new JPanel();
panel.add(btn);
btn.setBounds(50, 100, 95, 30);
add(panel);
}
private void creatFrame(String title) {
JFrame f = new JFrame(title);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setSize(400, 500);
f.setLayout(null);
}
}
public class Driver {
public static void main (String [] args) {
new Swing ("calculator");
}
}
Okay,lets start with...
JButton btn = new JButton("click me");
JPanel panel = new JPanel();
panel.add(btn);
btn.setBounds(50, 100, 95, 30);
add(panel);
You:
Create a button
Create a panel
You add the button to panel
You add the panel to the frame
And then...
JFrame f = new JFrame("calculator");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new
f.setVisible(true);
You create a brand new instance of JFrame and show it, but it has nothing on to it?! 😱!
Instead, you should avoid extending from JFrame and maybe use JPanel instead, something like...
public class Swing extends JPanel {
private JButton button;
private JLabel label;
public Swing() {
creatButton();
add(button);
}
public void creatButton() {
JButton btn = new JButton("click me");
}
}
Then you can just create a window (or other container) and add it to it
JFrame f = new JFrame(title);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Swing());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
As a general rule, JFrame is a really poor extension point, it's a complex, compound component and locks you into a single use case. It's generally a better idea to start with something JPanel which provides you with a lot more flexibility and a lot less complexity and is easily reusable.
You really, really, really need to avoid null layouts
creatFrame is creating a new JFrame different than the frame itself (your Swing class extending JFrame).
Remove the line:
JFrame f = new JFrame(title);
and call the methods over this instead of f.

CardLayout for JFrames?

I've been looking around, including in the Java documentation, but there isn't a clear answer that I've found for my question : I would like to switch from one JFrame to another at the click of a button; that is, have the old JFrame close while the new one opens. I've heard of "CardLayout" but I'm not so sure how it works. Would anyone mind explaining it, or some other method?
Thanks
Here is an example of a CardLayout
As you've heard other say, don't use multiple JFrames.
import javax.swing.*;
import java.awt.*;
public class MainFrame
{
static JPanel homeContainer;
static JPanel homePanel;
static JPanel otherPanel;
static CardLayout cl;
public MainFrame()
{
JFrame mFrame = new JFrame("CardLayout Example");
JButton showOtherPanelBtn = new JButton("Show Other Panel");
JButton backToHomeBtn = new JButton("Show Home Panel");
cl = new CardLayout(5, 5);
homeContainer = new JPanel(cl);
homeContainer.setBackground(Color.black);
homePanel = new JPanel();
homePanel.setBackground(Color.blue);
homePanel.add(showOtherPanelBtn);
homeContainer.add(homePanel, "Home");
otherPanel = new JPanel();
otherPanel.setBackground(Color.green);
otherPanel.add(backToHomeBtn);
homeContainer.add(otherPanel, "Other Panel");
showOtherPanelBtn.addActionListener(e -> cl.show(homeContainer, "Other Panel"));
backToHomeBtn.addActionListener(e -> cl.show(homeContainer, "Home"));
mFrame.add(homeContainer);
cl.show(homeContainer, "Home");
mFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
mFrame.setLocationRelativeTo(null);
mFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
mFrame.pack();
mFrame.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(MainFrame::new);
}
}
You don't need to use a CardLayout for anything in this case. In fact, JFrame's don't have layouts.
Here's some code to illustrate that idea (assuming you're using Java 8; otherwise, add the final modifier to oldFrame and newFrame:
JFrame parent = new JFrame();
JDialog oldFrame = new JDialog("My Old Frame's Title");
JDialog newFrame = new JDialog("My New Frame's Title");
JPanel panel = new JPanel();
JButton myButton = new JButton();
myButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
oldFrame.dispose(); //closes your old frame
newFrame.setVisible(true); //opens your new frame
}
});
panel.add(myButton);
oldFrame.add(panek);
oldFrame.setVisible(true);
Whenever you click your button, the old frame closes, and the new one opens.

How to add a button to a JFrame Gui

I'm trying to add a button to a frame gui.
i tried making a panel and adding it to that, but it does not work.
please help!
here is my code:
import javax.swing.*;
public class Agui extends JFrame {
public Agui() {
setTitle("My Gui");
setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JButton button;
JPanel panel;
// my error lines are under the "panel" and "button"
// it says i must implement the variables. what does that mean???
panel.add(button);
}
public static void main(String[] args) {
Agui a = new Agui();
}
}
Change:
JButton button;
JPanel panel;
to:
JButton button = new JButton();
JPanel panel = new JPanel();
You can also pass a String value in JButton() constructor for that string value to be shown on the JButton.
Example: JButton button = new JButton("I am a JButton");
Example Code:
import javax.swing.*;
public class Agui extends JFrame {
public Agui() {
setTitle("My Gui");
setSize(400, 400);
// Create JButton and JPanel
JButton button = new JButton("Click here!");
JPanel panel = new JPanel();
// Add button to JPanel
panel.add(button);
// And JPanel needs to be added to the JFrame itself!
this.getContentPane().add(panel);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Agui a = new Agui();
}
}
Output:
Note:
Create the JButton and JPanel using new JButton("..."); and new JPanel()
Add the JPanel to the JFrame's content pane using getContentPane().add(...);
If you can Change this Program, You can adjust the button place also
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class agui extends JFrame
{
agui()
{
setTitle("My GUI");
setSize(400,400);
setLayout(null);
JButton button = new JButton("Click Here..!");
button.setBounds(50,100,100,50); /*Distance from left,
Distance from top,length of button, height of button*/
add(button);
setVisible(true);
}
public static void main(String[] args)
{
JFrame agui = new agui();
}
}
Output

need to init the JFrame

I dont know how to init the JFrame windows. What I need to write to init im?
I have created at the main this:
Panel Panel=new Panel();
Panel.init();
JFrame frame = new JFrame("Shape Project");
frame.add(Panel);
frame.setResizable(false);
frame.setSize(new Dimension(1200, 650));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
and in the JPanel class I have write this:
public class Panel extends JFrame
{
public void init()
{
}
}
But when I active the frame it's does not active. What I need to write at the init func that the windows will open?
Try pack(); method of JFrame. If you are planning to develop with Swing, I recommend you to follow this tutorial:
http://download.oracle.com/javase/tutorial/uiswing/index.html
You already have a JFrame (frame). so now you should add components for your panel (you may do it in the main class as well). Such components are JTextField, JButton, etc. (and even another JPanel) each component you can add to the panel using panel.add(component_name); it is also recommended to follow the tutorial as Erkan mentioned.
Your panel class should extend JPanel, not JFrame.
You can add components to JPanel such as JButton, JList, etc
This is an example code you need to init a JFrame if you don't create own classes:
public class LogMain
{
public static void main(String[] args)
{
JFrame window = new JFrame("Log");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(300,300);
window.setResizable(false);
JPanel panel = new JPanel();
JButton openFile = new JButton("Btn1");
JButton openDir = new JButton("Btn2");
panel.add(openFile);
panel.add(openDir);
window.add(panel);
window.setLocationRelativeTo(null);
window.setVisible(true);
}
}
this is my example about the init of JFrame :
public class Windows{
public static void main(String args[]){
SJFrame window = new SJFrame("NEWNEWNEW");
window.init();
}
}
public class SJFrame extends JFrame(){
public SJFrame(String s){
super(s);
}
void init(){
Container panel = this.getContentPane();
panel.setBackground(Color.green);
panel.setLayout(new GridLayout(5,1));
JLabel jl1 = new JLabel("UserName");
JLabel jl2 = new JLabel("PassWord");
this.add(jl1);
this.add(jl2);
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE) ;
this.setSize(300,100);
this.pack();
this.setVisible(true);
}
}

BoxLayout program not working

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class File
{
private JFrame frame1;
private JPanel panel1;
private JPanel panel2;
private JLabel labelWeight;
private JLabel labelHeight;
File()
{
frame1 = new JFrame();
panel1 = new JPanel();
panel2 = new JPanel();
labelWeight = new JLabel("Weight :");
labelHeight = new JLabel("Height :");
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
panel1.setLayout(new FlowLayout());
panel1.add(labelWeight);
panel2.setLayout(new FlowLayout());
panel2.add(labelHeight);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
frame1.setLayout(new BoxLayout(frame1,BoxLayout.X_AXIS));
panel1.setAlignmentY(0);
panel2.setAlignmentY(0);
frame1.add(panel1);
frame1.add(panel2);
frame1.setSize(400, 200);
frame1.setDefaultCloseOperation(frame1.EXIT_ON_CLOSE);
frame1.setVisible(true);
}
public static void main (String args[])
{
new File();
}
}
It gives BoxLayout Sharing error at runtime
Generally, LayoutManagers are set on a JPanel. I guess JFrame implements this method to forward it to the content pane of the frame. I would suggest you try:
Container contentPane = frame1.getContentPane();
contentPane.setLayout(new BoxLayout(contentPane,BoxLayout.X_AXIS));
If you still have problems take a look at the Swing tutorial on How to Use Box Layout for working examples.
Swing components should be created in the Event Dispatch Thread. Try this in your main():
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new File();
}
});
But your problem may be the same as this question.

Categories

Resources