Results 1 to 7 of 7
  1. #1
    vkokaram is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Post open another window via button press

    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();
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Please use code tags when posting code in this forum. My signature link can help you see how to do this. Also you may wish to use a JDialog here, and again the Swing tutorials (see link in other thread) and look here, can help you.

  3. #3
    vkokaram is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default

    Thank you but an example would be helpful using the posted code.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by vkokaram View Post
    Thank you but an example would be helpful using the posted code.
    I beg to differ. For one, the tutorials have excellent code examples, and most of the volunteers here agree that questioners learn best by applying the knowledge gained from the tutorial lessons and tutorial code in their own programs rather than being spoon-fed solutions. This requires your brain to take extra steps in processing and applying what you've learned and it is here where you receive extra benefit.

    Then if your code falters and does not perform the desired tasks, you post your code, your question, and we work with your code (using code tags of course). :)

    Much luck.

  5. #5
    vkokaram is offline Member
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default

    Wow, Since you do not know who I am or why I am doing this, your comments are short-sighted and insulting. Maybe I made a mistake, I thought people could post questions on this forum and if anyone was willing to they would answer them. Sorry to have wasted your time and this forums time, I did not know that you need to already know Java to post on this forum.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Sorry to have insulted you, and it wasn't meant as an insult, but if you want others to look at your code, you'll still want to add code tags. Luck.

  7. #7
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    ok i havent checked your code on my pc but at a quick glance you can go ahead and delete the

    public static void main(String[] args) {
    SubApp1 StartWindow = new SubApp1();
    }

    and then with the SubApp1 StartWindow = new SubApp1();, cut and paste that into the actionPerformed part of the code for when the network device button is pressed. This should create the object when that button is pressed.
    Also just below that code you also want to set visible to true and also do the setbounds, which allows you to set the position of the frame aswell as its size. If you want the frame to appear slightly to the right you can do it with setbounds.

    I made a little program below to show some code.

    PHP Code:
    package javaapplication25;
    
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class gui extends JFrame {
    
        BorderLayout bl = new BorderLayout();
        JButton jb;
        static int xbound; static int ybound;
    
        public gui(){
    
            super("Quick Test");
    
        }
        public void addComponents(){
    
            JPanel contentPane = (JPanel) getContentPane();
    
            jb = new JButton("next window");
            contentPane.add(bl.SOUTH,jb);
        }
    
        
        public void running(){
            
            jb.addActionListener(new ActionListener()
            {public void actionPerformed(ActionEvent e){
                
                gui2 gui2 = new gui2();
                gui2.setVisible(true);
                gui2.setBounds(xbound+120, ybound+20, 333, 333);
            }});   
        }
    
        public static void main(String[] args) {
    
            gui gui = new gui();
            gui.setVisible(true);
            gui.addComponents();
            gui.running();
            xbound = 200; ybound = 200;
            gui.setBounds(xbound, ybound, 333, 333);
            gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        }
    
        public class gui2 extends JFrame{
    
    
        }
    
    }
    Last edited by alacn; 07-19-2010 at 03:12 AM.
    Teaching myself java so that i can eventually join the industry! Started in June 2010

Similar Threads

  1. Replies: 10
    Last Post: 06-06-2010, 07:49 PM
  2. Replies: 0
    Last Post: 05-20-2010, 09:18 PM
  3. Press F1 key to open Help Contents
    By Doctor Cactus in forum New To Java
    Replies: 1
    Last Post: 12-20-2008, 02:22 PM
  4. simulate form type=button press with java application
    By redmoonzer01 in forum Networking
    Replies: 1
    Last Post: 03-29-2008, 07:28 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •