Clicking a JLabel to open a new frame - java

I am designing the graphics for a game i am programming, i wanted to know if there is an easy way of opening a frame when a JLabel is cliked?
Is there easy code for this?

Implement MouseListener interface and use it mouseClicked method to handle the clicks on the JLabel.
label.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
// you can open a new frame here as
// i have assumed you have declared "frame" as instance variable
frame = new JFrame("new frame");
frame.setVisible(true);
}
});

create a label and add click event in it .
Something like this :
JLabel click=new JLabel("Click me");
click.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
JFrame jf=new JFrame("new one");
jf.setBackground(Color.BLACK);
jf.setSize(new Dimension(200,70));
jf.setVisible(true);
jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
});

don't to create a new JFrame, never bunch of JFrames, have to calculating with OutOfMemoryException, because this Object never will be GC'ed,
for multiple of views to use CardLayout
see answer The Use of Multiple JFrames, Good/Bad Practice? by #Andrew Thompson

You could do that like this:
label.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e)
{
JPanel j = new JPanel();
frame.setContentPane(j);
}
});

1:- Implement your class containing the JLabel with MouseListener interface
2:- add MouseListener to your JLabel
3:-Override mouseClicked Event in your class
4:- In mouseClicked Even't body add your code to open a new JFrame/Frame .

Related

exit button of a JFrame application [duplicate]

I would like to apply my own close and minimize buttons. Is there any way to change the JFrame design?
The trick lies in the PLAF and setDefaultLookAndFeelDecorated(true) (Specifying Window Decorations).
E.G.
import java.awt.BorderLayout;
import javax.swing.*;
public class FrameCloseButtonsByLookAndFeel {
FrameCloseButtonsByLookAndFeel() {
String[] names = {
UIManager.getSystemLookAndFeelClassName(),
UIManager.getCrossPlatformLookAndFeelClassName()
};
for (String name : names) {
try {
UIManager.setLookAndFeel(name);
} catch (Exception e) {
e.printStackTrace();
}
// very important to get the window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame f = new JFrame(UIManager.getLookAndFeel().getName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel gui = new JPanel(new BorderLayout());
f.setContentPane(gui);
JTree tree = new JTree();
tree.setVisibleRowCount(4);
gui.add(tree, BorderLayout.LINE_START);
gui.add(new JScrollPane(new JTextArea(3,15)));
JToolBar toolbar = new JToolBar();
gui.add(toolbar, BorderLayout.PAGE_START);
for (int ii=1; ii<5; ii++) {
toolbar.add(new JButton("Button " + ii));
if (ii%2==0) {
toolbar.addSeparator();
}
}
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new FrameCloseButtonsByLookAndFeel();
}
});
}
}
think you are after a JWindow
http://docs.oracle.com/javase/7/docs/api/javax/swing/JWindow.html
You can then create your own buttons which actions can minimize/close your window
The only thing I'm aware that can be done is to add a WindowListener to the JFrame and handle closing events in that listener. You can make virtually anything, like displaying dialogs or even cancelling the closing of the JFrame.
See this tutorial for more details about how to write such listeners.
As for minimizing: as far as I know, there is no way to control or modify such behaviour, it's completely controlled by the operating system.
The only way to change the aspect of the minimize/close/maximize buttons is to use a custom LookAndFeel and setting JFrame.setDefaultLookAndFeelDecorated (true);.
Set jframe undecorated.
Place a jlabel for each button.
Put own icon for each Btn.
Put mouseListeners for each jlabel and
specify code eg, System.exit(0);/set ICONIFIED option

could not remove/add jpanel in jframe

I'm stuck with an issue that is, I have a JFrame with 2 JPanels added in it as showed in Figure :
in figure above, one JPanel have some JButtons and second JPanel have some form fields, I want to change/(remove old and add new JPanel) when I click on JButtons in first JPanel accordingly as shown bellow :
I have code snippet :
myPanel.clickListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
MainFrame.this.getContentPane().remove(((BorderLayout)getLayout()).getLayoutComponent(BorderLayout.CENTER));
MainFrame.this.getContentPane().add(twoPanel, BorderLayout.CENTER);
MainFrame.this.invalidate();
MainFrame.this.validate();
}
});
myPanel.clickListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
MainFrame.this.getContentPane().remove(((BorderLayout)getLayout()).getLayoutComponent(BorderLayout.CENTER));
MainFrame.this.getContentPane().add(customerPanel, BorderLayout.CENTER);
MainFrame.this.invalidate();
MainFrame.this.validate();
}
});
MainFrame.this.setMaximumSize(new Dimension(600, 550));
MainFrame.this.setMinimumSize(new Dimension(599, 549));
MainFrame.this.setSize(600, 550);
MainFrame.this.setResizable(false);
MainFrame.this.setVisible(true);
}
});
through above code I'm able to add new JPanel but unable to remove first JPanel.
in my opinion you should use CardLayout.
It allows you to change visibility of JPanel, so that is actually what you want to do.
You define two JPanels for the right side and then in listner just toggle them.
Look here for the example:
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 can I minimize/iconify a JWindow in Java?

My app has a JWindow that needs to be minimized when the custom minimizer button clicked.
Please reply if anyone knows how to minimize a JWindow. I have searched a lot but couldn't find any suitable method to minimize.
I know how to minimize a JFrame. So please don't bother answering regarding JFrame.
Thanks.
I know you don't want to hear this, but the terrible truth is that there is no big difference between undecorated jframes (with setstate methods) and jwindows... :)
JFrame f = new JFrame("Frame");
f.setUndecorated(true);
Due to the fact that a JWindow is not decorated with any control icons, no setState method is provided. One workaround is to allow your custom minimizer button to set the window visible as required:
public class JWindowTest extends JFrame {
JWindow window = new JWindow();
JButton maxMinButton = new JButton("Minimize Window");
public JWindowTest() {
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
maxMinButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (window.isVisible()) {
maxMinButton.setText("Restore Window");
} else {
maxMinButton.setText("Minimize Window");
}
window.setVisible(!window.isVisible());
}
});
add(maxMinButton);
window.setBounds(30, 30, 300, 220);
window.setLocationRelativeTo(this);
window.add(new JLabel("Test JWindow", JLabel.CENTER));
window.setVisible(true);
}
public static void main(String[] args) {
new JWindowTest().setVisible(true);
}
}

Custom design for Close/Minimize buttons on JFrame

I would like to apply my own close and minimize buttons. Is there any way to change the JFrame design?
The trick lies in the PLAF and setDefaultLookAndFeelDecorated(true) (Specifying Window Decorations).
E.G.
import java.awt.BorderLayout;
import javax.swing.*;
public class FrameCloseButtonsByLookAndFeel {
FrameCloseButtonsByLookAndFeel() {
String[] names = {
UIManager.getSystemLookAndFeelClassName(),
UIManager.getCrossPlatformLookAndFeelClassName()
};
for (String name : names) {
try {
UIManager.setLookAndFeel(name);
} catch (Exception e) {
e.printStackTrace();
}
// very important to get the window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame f = new JFrame(UIManager.getLookAndFeel().getName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel gui = new JPanel(new BorderLayout());
f.setContentPane(gui);
JTree tree = new JTree();
tree.setVisibleRowCount(4);
gui.add(tree, BorderLayout.LINE_START);
gui.add(new JScrollPane(new JTextArea(3,15)));
JToolBar toolbar = new JToolBar();
gui.add(toolbar, BorderLayout.PAGE_START);
for (int ii=1; ii<5; ii++) {
toolbar.add(new JButton("Button " + ii));
if (ii%2==0) {
toolbar.addSeparator();
}
}
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new FrameCloseButtonsByLookAndFeel();
}
});
}
}
think you are after a JWindow
http://docs.oracle.com/javase/7/docs/api/javax/swing/JWindow.html
You can then create your own buttons which actions can minimize/close your window
The only thing I'm aware that can be done is to add a WindowListener to the JFrame and handle closing events in that listener. You can make virtually anything, like displaying dialogs or even cancelling the closing of the JFrame.
See this tutorial for more details about how to write such listeners.
As for minimizing: as far as I know, there is no way to control or modify such behaviour, it's completely controlled by the operating system.
The only way to change the aspect of the minimize/close/maximize buttons is to use a custom LookAndFeel and setting JFrame.setDefaultLookAndFeelDecorated (true);.
Set jframe undecorated.
Place a jlabel for each button.
Put own icon for each Btn.
Put mouseListeners for each jlabel and
specify code eg, System.exit(0);/set ICONIFIED option

Categories