Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to press button to open another window

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    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
    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
    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

    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();
    }
    }
    Last edited by copeg; July 18th, 2010 at 04:18 PM. Reason: Please use the code tags


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

Similar Threads

  1. press a button, get textfield content
    By chopficaro in forum AWT / Java Swing
    Replies: 1
    Last Post: May 2nd, 2010, 01:28 PM
  2. How to make user press enter to continue in program?
    By BC2210 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 3rd, 2009, 06:08 AM
  3. Java program to open jsp page on client side instead of server side
    By khanshakil in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: July 8th, 2009, 07:26 AM
  4. Facing problem with open modeless dialog from modal window
    By Divya in forum Java Theory & Questions
    Replies: 1
    Last Post: May 14th, 2009, 04:18 AM
  5. Java program to open and close computer CD/DVD drive
    By JavaPF in forum Java Programming Tutorials
    Replies: 0
    Last Post: January 28th, 2009, 12:04 PM