I have two java projects. Both with runnable GUIs.
I would like to add a button in Project1 that when pressed opens Project2 application window and closes Project 1.
Since I am using MVC for both applications it would become too cluttered to just copy paste all the code from the first project into the other one, which is why I am looking for an easier alternative.
I have built a path between the two projects, but I can't seem to find a way to start the second application on a button press.
i made you simple example of how to close JFrame after clicking a button and open new one.It is better if you write us the code you are useing note: i am not using MVC here
package test;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test {
private JButton btn;
JFrame frame = new JFrame();
public Test(){
btn = new JButton("new window");
frame.setVisible(true);
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(btn);
frame.setLayout(new FlowLayout());
btn.addActionListener(new addButtonWatcher());
}
private class addButtonWatcher implements ActionListener{
#Override
public void actionPerformed(ActionEvent e) {
JFrame newWindow = new JFrame();
newWindow.setVisible(true);
newWindow.setSize(200,200);
frame.dispose();
}
}
public static void main(String[] args) {
Test t = new Test();
}
}
Related
I'm very confused on how to add an image to my GUI in java. Below is my code and I am using ImageIcon to implement the image "map.png" but when I run this program the image does not appear. is this because it is not in the same folder as my .java file or is there some other problem?
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GUI implements ActionListener
{
private int count = 0;
private JLabel label;
private JFrame frame;
private JPanel panel;
public GUI()
{
frame = new JFrame();
frame.setResizable(false);
ImageIcon icon = new ImageIcon("map.png");
JLabel picture = new JLabel(icon);
JButton button = new JButton("Click me");
button.addActionListener(this);
label = new JLabel("Number of clicks: 0");
panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(400, 700, 10, 30));
panel.setLayout(new GridLayout(0, 1));
panel.add(button);
panel.add(label);
frame.add(picture);
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Maynooth Thing");
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args)
{
new GUI();
}
#Override
public void actionPerformed(ActionEvent e)
{
count++;
label.setText("Number of clicks: " + count);
}
}
Java will search the image in the actual working directory, that is, the directory where java was started. This depends on how it is started - if command line, it is the same as the command line is using; IDEs normally have some settings (e.g. default for eclipse is the project main directory). Easy way to print it System.out.println(new java.io.File("").getAbsolutePath());
Unfortunately new ImageIcon does not throw an exception if the file is not found - the icon size (getIconHeight() or getIconWidth()) will be set to -1 if not found. ImageIO is better for reading images from file, given the image is not part of the application; otherwise, image is part of app, getResource() from class or class loader should be used.
The location of .java files is not relevant at all during execution. The location of .class files (classpath) would matter if using getResource methods from class loarder (recommended! specially if image/file should be read from JAR).
I'm learning about threads and I've got a problem with it. I'm trying to make 2 frames, one is a main frame and another will be shown later after clicking on a button. I want to stop the main frame while the new frame is running. Can you guys help me with a very simple example for this? (And the new frame will be closed after clicking on a button too). Just 2 frames with a button on each are enough. Much appreciated!
You should avoid the use of multiple JFrames, use modal dialogs instead. JOptionPane offers a ton of good, easy & flexible methods to do so.
Here's an example. When you click the button the dialog will appear on top of the JFrame. The main JFrame won't be clickable anymore, since JOptionPane.showMessageDialog() produces a modal window.
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Example {
public Example() {
JFrame frame = new JFrame();
JButton button = new JButton("Click me");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(frame, "I'm a dialog!");
}
});
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new Example();
}
});
}
}
Output:
I have created a simple VLCJ project that consists of a simple embedded player and a button to exit.
The code is as follows:
package test;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import uk.co.caprica.vlcj.binding.LibVlc;
import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.runtime.RuntimeUtil;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
public class Demo {
private final JFrame frame;
private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
private JPanel videoPane;
private JPanel buttonPane;
private Button exitButton;
private ActionListener a;
private static String vlc_location = "C:\\Program Files\\VideoLAN\\VLC";
public static void main(String[] args) {
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), vlc_location);
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Demo().run();
}
});
}
public Demo() {
mediaPlayerComponent = new EmbeddedMediaPlayerComponent();
a = new MyActionListener();
exitButton = new Button("Exit");
exitButton.setActionCommand("Exit app");
exitButton.addActionListener(a);
buttonPane = new JPanel();
buttonPane.setLayout(new BorderLayout());
buttonPane.setBackground(Color.black);
buttonPane.add(exitButton, BorderLayout.CENTER);
videoPane = new JPanel();
videoPane.setLayout(new BorderLayout());
videoPane.setBackground(Color.black);
videoPane.add(mediaPlayerComponent, BorderLayout.CENTER);
videoPane.add(buttonPane, BorderLayout.PAGE_END);
frame = new JFrame("vlcj demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(100, 100);
frame.setSize(1200, 800);
frame.setContentPane(videoPane);
frame.setVisible(true);
}
public void run() {
mediaPlayerComponent.getMediaPlayer().playMedia(video_file);
}
class MyActionListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent arg0) {
String s = arg0.getActionCommand();
if (s.equals("Exit")) {
System.exit(0);
}
}
}
}
The problem is that the button does show up but it cannot be clicked. When i removed the videoPane, it was back to clickable! Any ideas if I'm missing something?
I am using the version 2.1.0 for vlcj.
Thanks!
Thanks MadProgrammer for your advise. I went on to think about it and tried commenting away the line of code in run(). The JButton came back!
However, when i un-commented the code in run(), the JButton disappeared. I was thinking maybe the Swing runnable was causing issue with the creation of the JButton.
Hence, what i did was to comment away the whole Swing runnable and just use:
final Demo demo = new Demo();
demo.run();
The demo can now play video and display the Exit button, thanks!
package javaapplication1;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class JavaApplication1 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setTitle("Frame");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
device.setFullScreenWindow(frame);
device.setDisplayMode(new DisplayMode(800, 600, 32, 60));
frame.setVisible(true);
JButton btn = new JButton();
btn.setText("Button");
JPanel panel = new JPanel();
panel.add(btn);
frame.add(panel);
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame, "JOptionPane");
}
});
}
}
When I click the button, the application which is set to full screen will go to taskbar/minimized, so I need to click it first in the taskbar before seeing the JOptionPane that I triggered. What do you think is the problem with this? I'd like it to run smoothly without being minimized or going to taskbar. Looking forward for your answers. Thanks in advance. Or is there any other alternative to this?
That code works for me, though you might try this variant with 2 changes.
It creates and shows the GUI on the EDT.
It uses the content pane of the frame as the parent of the JOptionPane
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JavaApplication1 {
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
final JFrame frame = new JFrame();
frame.setTitle("Frame");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
device.setFullScreenWindow(frame);
device.setDisplayMode(new DisplayMode(800, 600, 32, 60));
frame.setVisible(true);
JButton btn = new JButton();
btn.setText("Button");
JPanel panel = new JPanel();
panel.add(btn);
frame.add(panel);
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//JOptionPane.showMessageDialog(frame, "JOptionPane");
JOptionPane.showMessageDialog(frame.getContentPane(), "JOptionPane");
}
});
}
};
SwingUtilities.invokeLater(r);
}
}
Update
When I add the following lines to the beginning of the source seen above..
System.out.println(System.getProperty("java.version"));
System.out.println(System.getProperty("java.vm.version"));
..the output & result is as follows.
Running in 1.7
Result: Failure as described in the question.
1.7.0_09
23.5-b02
Running in 1.6
Result: Success with no unusual artifacts or behavior.
1.6.0
1.6.0-b105
Analysis
Note that other results from comments suggest the behavior changed some time between that early 1.6 version, and 1.6.0_25. It seems like a regression bug. The OP should check the bug database & if nothing likely shows up, lodge a new report.
JOptionPane.showInternalMessageDialog(frame.getContentPane(), "JOptionPane");
I have a welcome (or menu) window (JFrame) with some buttons (JButton) for each possible action. Each of these should launch a new window and hide the welcome window. I know I can do it with setVisible(false);. But I can't make it work yet.
This is one example of code I have:
_startBtn.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e){
System.out.println("_startBtn pressed");
// Code to hide this JFrame and initialize another
}
My question is, how can I do it using a anonymous class like this one?
Thanks in advance!
I am posting an example for you i hope it will help you.
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class windows_test {
JFrame login = null;
JFrame inner_frame = null;
public windows_test() {
login = new JFrame();
login.setBounds(10, 10, 300, 300);
login.setLayout(new BorderLayout());
JButton button = new JButton("Login");
login.add(button, BorderLayout.CENTER);
login.setVisible(true);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
if (inner_frame == null) {
inner_frame = new JFrame();
}
inner_frame.setLayout(new FlowLayout(FlowLayout.CENTER));
inner_frame.add(new JButton("inner frame"));
inner_frame.setVisible(true);
login.setVisible(false);
inner_frame.setBounds(10, 10, 300, 300);
}
});
}
}
I will recommend you to use jpanel instead of jframes but you have asked for frames so i have created it with them. Hope it will help you ask if i am wrong somewhere or you are not able to understand.