How to press button to open another window
Need to call another call that opens another window without closing the current windows that called the app. And need this window to open slightly off so it does not cover the windows with the button that called it. When the button "Network device" is pressed I wanted to call application SubApp1.java as a new window that does not open up over the main app window.
MainAppWin3.java
Code java:
import javax.swing.*;
import java.awt.*;
public class MainAppWin3 extends JFrame {
AppEvent home = new AppEvent(this);
JPanel row1 = new JPanel();
JLabel startLabels = new JLabel("Current System");
TimePanel time = new TimePanel();
JPanel row2 = new JPanel();
JButton actionDevice = new JButton("Network Device");
JButton actionExit = new JButton("Exit");
public MainAppWin3() {
super("Toolbox");
setSize(700, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout layout = new GridLayout(2, 1);
setLayout(layout);
// Add listeners
actionDevice.addActionListener(home);
actionExit.addActionListener(home);
FlowLayout layout1 = new FlowLayout(FlowLayout.LEFT, 5, 5);
row1.setLayout(layout1);
row1.add(startLabels);
row1.add(time);
add(row1);
FlowLayout layout2 = new FlowLayout(FlowLayout.CENTER, 10, 10);
row2.setLayout(layout2);
row2.add(actionDevice);
row2.add(actionExit);
add(row2);
setVisible(true);
}
public static void main(String[] args) {
MainAppWin3 frame = new MainAppWin3();
}
}
AppEvent.java
Code java:
import javax.swing.*;
import java.awt.event.*;
public class AppEvent implements ActionListener {
MainAppWin3 gui;
public AppEvent(MainAppWin3 in) {
gui = in;
}
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command.equals("Network Device")) {
startNetworkDevice();
}
if (command.equals("Exit")) {
exitExit();
}
}
void startNetworkDevice() {
gui.actionDevice.setEnabled(false);
gui.actionExit.setEnabled(true);
}
void exitExit() {
gui.actionDevice.setEnabled(true);
gui.actionExit.setEnabled(true);
System.exit(0);
}
}
TimePanel.java
Code java:
import javax.swing.*;
import java.util.*;
public class TimePanel extends JPanel {
public TimePanel() {
super();
String currentTime = getTime();
JLabel time = new JLabel("Time: ");
JLabel current = new JLabel(currentTime);
add(time);
add(current);
}
String getTime() {
String time;
// get current time and date
Calendar now = Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int month = now.get(Calendar.MONTH);
int day = now.get(Calendar.DAY_OF_MONTH);
int year = now.get(Calendar.YEAR);
String monthName = "";
switch (month) {
case (1):
monthName = "January";
break;
case (2):
monthName = "February";
break;
case (3):
monthName = "March";
break;
case (4):
monthName = "April";
break;
case (5):
monthName = "May";
break;
case (6):
monthName = "June";
break;
case (7):
monthName = "July";
break;
case (8):
monthName = "August";
break;
case (9):
monthName = "September";
break;
case (10):
monthName = "October";
break;
case (11):
monthName = "November";
break;
case (12):
monthName = "December";
}
time = monthName + " " + day + ", " + year + " " + hour + ":" + minute;
return time;
}
}
SubApp1.java
Code java:
import javax.swing.*;
import java.awt.*;
public class SubApp1 extends JFrame {
public SubApp1() {
super("Test Toolkit");
setSize(700, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
FlowLayout beginButtons = new FlowLayout();
setLayout(beginButtons);
JLabel startLabels = new JLabel("Current System");
TimePanel time = new TimePanel();
add(startLabels);
add(time);
setVisible(true);
}
public static void main(String[] args) {
SubApp1 StartWindow = new SubApp1();
}
}
Re: How to press button to open another window
Please don't post the same question under multiple forums. Also, the Java tips/snippets forums is reserved for tips you are giving, not questions about how to do something. I've removed the other post.
Also, please surround your code with [highlight=Java]code goes here[/highlight]. It makes it much easier to read.
The easiest solution is to simply add an action listener to the button you want to listen to, then when that action gets fired, create a new JFrame of the item you want, and set the location of that window using the setBounds() method.