Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    shoeb83 is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default Multiple window (java program) simultaneous operation

    I want to make a vertical window having several button. when I click a button it will open another window where there is jTextfield, jTextarea, jButton etc and it can do something (taking input from textfield and giving the output in textarea). Not closing this window if i click another button of the first window (vertical) it will open another independent window and it can do something like the previous one.

    i.e, multiple window will work simultaneously. Please help me


    I made this type of java program using java swing. Two buttons in the first window. Two independent java program. But it can open only one program at a time. I am to close one to open another. i.e, it does not work simultaneously.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    How are you opening the other window then? You'll have to show the code that opens the window while closing the first one.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Suggest that this be moved to the Swing/AWT forum.

    Thanks, db

  4. #4
    shoeb83 is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default

    Tnx r035198x for reply and query about my problem

    To open program I used Runtime.getRuntime()

    ActionListener button1 contains the below code
    Process p = Runtime.getRuntime().exec("java program1");

    ActionListener button2 contains as below
    Process p = Runtime.getRuntime().exec("java program2");

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    For starters put the code that runs the Runtime.getRuntime().exec on their own threads so they don't run on the EDT.

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

    Default

    For another, are you sure that you want to call these other Java programs as outside processes? If at all possible it would be a lot cleaner if you could call the code from program1 and program2 directly in your program.

  7. #7
    shoeb83 is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default

    If someone explain in detail it will be very helpfull for me

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Which part do you wish explained in detail?

  9. #9
    shoeb83 is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    For starters put the code that runs the Runtime.getRuntime().exec on their own threads so they don't run on the EDT.
    Quote Originally Posted by Fubarable
    For another, are you sure that you want to call these other Java programs as outside processes? If at all possible it would be a lot cleaner if you could call the code from program1 and program2 directly in your program.
    Need to explain the above quotes as I'm not expert

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Perhaps let me ask a simple question first.
    Why do you have two separate programs?

  11. #11
    shoeb83 is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default

    program 1 takes input and give output after pressing button
    program 2 takes different input and give different output after pressing button
    ...
    ..

    I need to do these tasks simultaneously. Those will be opened at the same time and do their tasks accordingly.
    Actually main program will act as a menu for choosing different programs to open.

  12. #12
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You just need one program for that.
    You can open many Frames and dialogs in one Java program.

  13. #13
    shoeb83 is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default

    are u talking about JInternalframe? if not pls explain

  14. #14
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Java Code:
    class Five {
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			@Override
    			public void run() {
    				fiveFrames();
    			}
    		});
    	}
    
    	public static void fiveFrames() {
    		for (int i = 0; i < 5; i++) {
    			JFrame f = new JFrame("Frame " + i);
    			f.setSize(300, 300);
    			f.setVisible(true);
    			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		}
    	}
    }

  15. #15
    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 shoeb83 View Post
    are u talking about JInternalframe? if not pls explain
    The best help for you would be to go through the Sun Swing tutorial to help you get a better understanding of how to code Swing projects. You would not regret this effort, believe me.

  16. #16
    shoeb83 is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default

    Thanks a lot everybody for giving me suggestions. I just complete it by writing fresh copy of simple programs. Now it is working fine the code is as below.


    Java Code:
    //main program
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    
    class menu{
    JFrame frm=new JFrame("Menu");
    	menu(){
    	frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    	frm.setSize(150,450);
            frm.setLocation(300,300);
    	frm.setLayout(null);
    	JButton btn1=new JButton("Button1");
    	JButton btn2=new JButton("Button2");
    	JButton btn3=new JButton("Button3");
    	
    	frm.add(btn1);
    	btn1.setBounds(10,10,100,32);
    	frm.add(btn2);
    	btn2.setBounds(10,50,100,32);
    
    	
    	
    btn1.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent ae){
       	String line = null;
    
    	try {  
    		Process p = Runtime.getRuntime().exec("java pr1");  
      	} 
    	catch (IOException e) {  
    		e.printStackTrace();  
       	} 
      
    
    	}
    });
    
    
    
    btn2.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent ae){
    	
    	try {  
    		Process p = Runtime.getRuntime().exec("java pr2");  
      	} 
    	catch (IOException e) {  
    		e.printStackTrace();  
       	} 
      
    
    	}
    });
    		
    frm.setVisible(true); 	
    }
    
    	
    public static void main(String args[]){
    	SwingUtilities.invokeLater(new Runnable(){
    	public void run(){
    		new menu();
    		}
    	});
    	
    
    
    }
    
    }
    
    
    //program 1
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    class pr1{
    JFrame frm=new JFrame("Program 1");
    JTextField tf=new JTextField("ls -l");
    JButton btn=new JButton("Go");
    JButton exit=new JButton("Exit");
    JButton clr=new JButton("Clear");
    JTextArea ta=new JTextArea("");
    StringBuffer sb = new StringBuffer();
    
    String txt;
    String tt;
    String s;
    
    pr1(){
    frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frm.setLayout(null);
    frm.setSize(600,400);
    
    frm.add(exit);
    exit.setBounds(485,5,100,25);
    
    exit.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae){
    System.exit(0);
    
    }
    });
    
    frm.add(clr);
    clr.setBounds(380,5,100,25);
    clr.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae){
    ta.setText("");
    tt="";
    }
    });
    
    frm.add(tf);
    tf.setBounds(10,35,120,25);
    tf.setEditable(true);
    frm.add(btn);
    btn.setBounds(135,35,80,25);
    
    ta.setLineWrap(true);
    ta.setWrapStyleWord(true);
    JScrollPane tasp = new JScrollPane(ta);
    tasp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    tasp.setPreferredSize(new Dimension(250, 450));
    
    frm.add(tasp);
    tasp.setBounds(10,70,570,280);
    
    
    btn.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent ae){
    	txt=tf.getText();
    	tt=ta.getText();
        	String line = null;
    
    	try {  
    		Process p = Runtime.getRuntime().exec(txt);  
    		BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));    
        		while ((line = in.readLine()) != null) {  
        		sb.append(line+"\n");
    			}  
      	} 
    	catch (IOException e) {  
    		e.printStackTrace();  
       	} 
    
     s= sb.toString();
     ta.setText(tt+"\n"+s);
    
    	}
    });
    
    frm.setVisible(true); 
    }
    
    	public static void main(String args[]){
    		SwingUtilities.invokeLater(new Runnable(){
    			public void run(){
    				new pr1();
    			}
    		});
    	}
    
    
    }
    
    
    //program 2
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    class pr2{
    JFrame frm=new JFrame("Program 2");
    JTextField tf=new JTextField("ifconfig");
    JButton btn=new JButton("Go");
    JButton exit=new JButton("Exit");
    JButton clr=new JButton("Clear");
    JTextArea ta=new JTextArea("");
    StringBuffer sb = new StringBuffer();
    
    String txt;
    String tt;
    String s;
    
    pr2(){
    frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frm.setLayout(null);
    frm.setSize(600,400);
    
    frm.add(exit);
    exit.setBounds(485,5,100,25);
    
    exit.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae){
    System.exit(0);
    
    }
    });
    
    frm.add(clr);
    clr.setBounds(380,5,100,25);
    clr.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae){
    ta.setText("");
    tt="";
    }
    });
    
    frm.add(tf);
    tf.setBounds(10,35,120,25);
    tf.setEditable(true);
    frm.add(btn);
    btn.setBounds(135,35,80,25);
    
    ta.setLineWrap(true);
    ta.setWrapStyleWord(true);
    JScrollPane tasp = new JScrollPane(ta);
    tasp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    tasp.setPreferredSize(new Dimension(250, 450));
    
    frm.add(tasp);
    tasp.setBounds(10,70,570,280);
    
    
    btn.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent ae){
    	txt=tf.getText();
    	tt=ta.getText();
        	String line = null;
    
    	try {  
    		Process p = Runtime.getRuntime().exec(txt);  
    		BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));    
        		while ((line = in.readLine()) != null) {  
        		sb.append(line+"\n");
    			}  
      	} 
    	catch (IOException e) {  
    		e.printStackTrace();  
       	} 
    
     s= sb.toString();
     ta.setText(tt+"\n"+s);
    
    	}
    });
    
    frm.setVisible(true); 
    }
    
    	public static void main(String args[]){
    		SwingUtilities.invokeLater(new Runnable(){
    			public void run(){
    				new pr2();
    			}
    		});
    	}
    
    
    }

  17. #17
    shoeb83 is offline Member
    Join Date
    Aug 2009
    Posts
    17
    Rep Power
    0

    Default

    Here is the link of the screenshot of my program

    www . postimage.org/image.php?v=Pq1HJln0

    Probably I was unable to make you understand my problem. I'm happy to solve it.

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

    Default

    I hate to be the bearer of bad news, but your solution is bad, and in fact very bad. As I suspected in my first reply to your post, you are much better off not calling the two programs as external processes but instead by calling them through Java itself. This "solution" of yours will break and break badly in the near future.

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

    Default

    For instance, compare your code with this:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    
    public class MenuProg {
      JFrame frm = new JFrame("Menu");
      MenuProg() {
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setSize(150, 450);
        frm.setLocation(300, 300);
        frm.setLayout(null);
        JButton btn1 = new JButton("Button1");
        JButton btn2 = new JButton("Button2");
        JButton btn3 = new JButton("Button3");
    
        frm.add(btn1);
        btn1.setBounds(10, 10, 100, 32);
        frm.add(btn2);
        btn2.setBounds(10, 50, 100, 32);
    
        btn1.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            Prog1 prog = new Prog1();
            prog.setTextFieldText("ls -l");
            
            JDialog dialog = new JDialog(frm, "Prog 1", false);
            dialog.getContentPane().add(prog.getMainPanel());
            dialog.pack();
            dialog.setLocationRelativeTo(null);
            dialog.setVisible(true);
          }
        });
    
        btn2.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            Prog1 prog = new Prog1();
            prog.setTextFieldText("ifconfig");
            
            JDialog dialog = new JDialog(frm, "Prog 2", false);
            dialog.getContentPane().add(prog.getMainPanel());
            dialog.pack();
            dialog.setLocationRelativeTo(null);
            dialog.setVisible(true);
          }
        });
    
        frm.setVisible(true);
      }
    
      public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            new MenuProg();
          }
        });
    
      }
    
    }
    
    class Prog1 {
      //JFrame frm = new JFrame("Program 1");
      JPanel mainPanel = new JPanel();
      JTextField tf = new JTextField("ls -l");
      JButton btn = new JButton("Go");
      JButton exit = new JButton("Exit");
      JButton clr = new JButton("Clear");
      JTextArea ta = new JTextArea("");
      StringBuffer sb = new StringBuffer();
    
      String txt;
      String tt;
      String s;
    
      Prog1() {
        //frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainPanel.setLayout(null);
        mainPanel.setPreferredSize(new Dimension(600, 400));
    
        mainPanel.add(exit);
        exit.setBounds(485, 5, 100, 25);
    
        exit.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            //System.exit(0);
            Window win = SwingUtilities.getWindowAncestor(mainPanel);
            win.dispose();
          }
        });
    
        mainPanel.add(clr);
        clr.setBounds(380, 5, 100, 25);
        clr.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            ta.setText("");
            tt = "";
          }
        });
    
        mainPanel.add(tf);
        tf.setBounds(10, 35, 120, 25);
        tf.setEditable(true);
        mainPanel.add(btn);
        btn.setBounds(135, 35, 80, 25);
    
        ta.setLineWrap(true);
        ta.setWrapStyleWord(true);
        JScrollPane tasp = new JScrollPane(ta);
        tasp
            .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        tasp.setPreferredSize(new Dimension(250, 450));
    
        mainPanel.add(tasp);
        tasp.setBounds(10, 70, 570, 280);
    
        btn.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            txt = tf.getText();
            tt = ta.getText();
            String line = null;
    
            try {
              Process p = Runtime.getRuntime().exec(txt);
              BufferedReader in = new BufferedReader(
                  new InputStreamReader(p.getInputStream()));
              while ((line = in.readLine()) != null) {
                sb.append(line + "\n");
              }
            } catch (IOException e) {
              e.printStackTrace();
            }
    
            s = sb.toString();
            ta.setText(tt + "\n" + s);
    
          }
        });
    
        //frm.setVisible(true);
      }
      
      public JPanel getMainPanel() {
        return mainPanel;
      }
      
      public void setTextFieldText(String text) {
        tf.setText(text);
      }
    
    }
    There's still a lot more that I'd like to fix here but this should give you an idea of what I'm talking about.

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

    Default

    sigh, next iteration, now with a SwingWorker background thread and some simplifications:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class MenuProg {
      private static final String BUTTON1 = "Button 1";
      private static final String BUTTON2 = "Button 2";
      private static final String BUTTON3 = "Button 3";
      private JPanel mainPanel = new JPanel();
      private JFrame frame;
      private Map<String, String> btnTextFieldMap = new HashMap<String, String>();
    
      MenuProg(JFrame frame) {
        this.frame = frame;
        JButton btn1 = new JButton(BUTTON1);
        JButton btn2 = new JButton(BUTTON2);
        JButton btn3 = new JButton(BUTTON3);
    
        btnTextFieldMap.put(BUTTON1, "ls -1");
        btnTextFieldMap.put(BUTTON2, "ifconfig");
    
        ButtonListener buttonListener = new ButtonListener();
        btn1.addActionListener(buttonListener);
        btn2.addActionListener(buttonListener);
    
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10,
            30, 10, 30));
        mainPanel.setLayout(new GridLayout(0, 1, 0, 20));
        mainPanel.add(btn1);
        mainPanel.add(btn2);
        mainPanel.add(btn3);
      }
    
      public JPanel getMainPanel() {
        return mainPanel;
      }
    
      private class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent ae) {
          String actionCommand = ae.getActionCommand();
          String textString = btnTextFieldMap
              .get(actionCommand);
    
          Prog1 prog = new Prog1();
          prog.setTextFieldText(textString);
    
          JDialog dialog = new JDialog(frame, "Program", false);
          dialog.getContentPane().add(prog.getMainPanel());
          dialog.pack();
          dialog.setLocationRelativeTo(null);
          dialog.setVisible(true);
    
        }
      }
    
      public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            JFrame frame = new JFrame("Menu");
            frame
                .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(
                new MenuProg(frame).getMainPanel());
            frame.pack();
            frame.setLocation(150, 300);
            frame.setVisible(true);
          }
        });
    
      }
    
    }
    
    class Prog1 {
      private static final Dimension BUTTON_SIZE = new Dimension(100, 25);
      private JPanel mainPanel = new JPanel();
      private JTextField tf = new JTextField(10);
      private JButton goButton = new JButton("Go");
      private JButton exitBtn = new JButton("Exit");
      private JButton clearBtn = new JButton("Clear");
      private JTextArea textArea = new JTextArea("");
    
      private String txt;
      //String tt;
      //String s;
    
      Prog1() {
    
        exitBtn.setPreferredSize(BUTTON_SIZE);
        exitBtn.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            Window win = SwingUtilities
                .getWindowAncestor(mainPanel);
            win.dispose();
          }
        });
    
        clearBtn.setPreferredSize(BUTTON_SIZE);
        clearBtn.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            textArea.setText("");
            //tt = "";
          }
        });
    
        tf.setEditable(true);
    
        tf.setPreferredSize(BUTTON_SIZE);
        goButton.setPreferredSize(BUTTON_SIZE);
    
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        JScrollPane tasp = new JScrollPane(textArea);
        tasp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    
        tasp.setPreferredSize(new Dimension(570, 280));
    
        goButton.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            txt = tf.getText();
            //tt = textArea.getText();
            
            new SwingWorker<Void, String>() {
    
              @Override
              protected Void doInBackground() throws Exception {
                try {
                  Process p = Runtime.getRuntime().exec(txt);
                  BufferedReader in = new BufferedReader(
                      new InputStreamReader(p.getInputStream()));
                  String line = "";
                  while ((line = in.readLine()) != null) {
                    publish(line);
                  }
                } catch (IOException e) {
                  e.printStackTrace();
                }
                return null;
              }
              
              @Override
              protected void process(List<String> chunks) {
                for (String chunk : chunks) {
                  textArea.append("\n" + chunk);
                }
              }
              
            }.execute();
          }
        });
    
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 4));
        topPanel.add(clearBtn);
        topPanel.add(Box.createHorizontalStrut(5));
        topPanel.add(exitBtn);
    
        JPanel goPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 4));
        goPanel.add(tf);
        goPanel.add(Box.createHorizontalStrut(5));
        goPanel.add(goButton);
    
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
        mainPanel.add(topPanel);
        mainPanel.add(goPanel);
        mainPanel.add(tasp);
      }
    
      public JPanel getMainPanel() {
        return mainPanel;
      }
    
      public void setTextFieldText(String text) {
        tf.setText(text);
      }
    
    }

Page 1 of 2 12 LastLast

Similar Threads

  1. multiple program
    By mm2236 in forum Threads and Synchronization
    Replies: 2
    Last Post: 10-02-2009, 01:27 PM
  2. Multiple window
    By shoeb83 in forum New To Java
    Replies: 1
    Last Post: 08-30-2009, 11:59 PM
  3. Replies: 6
    Last Post: 02-24-2009, 03:50 PM
  4. Calling a JFrame window from a command line program.
    By new_2_java in forum New To Java
    Replies: 7
    Last Post: 11-09-2008, 04:40 AM
  5. Doubt in simultaneous 'implementation' and 'extension'
    By ajaygargnsit in forum New To Java
    Replies: 2
    Last Post: 12-20-2007, 10:33 AM

Posting Permissions

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