Results 1 to 20 of 25
- 08-30-2009, 07:52 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 17
- Rep Power
- 0
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.
- 08-30-2009, 08:51 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
How are you opening the other window then? You'll have to show the code that opens the window while closing the first one.
- 08-30-2009, 11:00 PM #3
Suggest that this be moved to the Swing/AWT forum.
Thanks, db
- 08-31-2009, 08:26 PM #4
Member
- Join Date
- Aug 2009
- Posts
- 17
- Rep Power
- 0
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");
- 08-31-2009, 08:41 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
For starters put the code that runs the Runtime.getRuntime().exec on their own threads so they don't run on the EDT.
-
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.
- 09-01-2009, 12:33 PM #7
Member
- Join Date
- Aug 2009
- Posts
- 17
- Rep Power
- 0
If someone explain in detail it will be very helpfull for me
- 09-01-2009, 12:36 PM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Which part do you wish explained in detail?
- 09-01-2009, 08:18 PM #9
Member
- Join Date
- Aug 2009
- Posts
- 17
- Rep Power
- 0
- 09-01-2009, 08:24 PM #10
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Perhaps let me ask a simple question first.
Why do you have two separate programs?
- 09-01-2009, 08:36 PM #11
Member
- Join Date
- Aug 2009
- Posts
- 17
- Rep Power
- 0
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.
- 09-01-2009, 08:39 PM #12
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You just need one program for that.
You can open many Frames and dialogs in one Java program.
- 09-01-2009, 08:53 PM #13
Member
- Join Date
- Aug 2009
- Posts
- 17
- Rep Power
- 0
are u talking about JInternalframe? if not pls explain
- 09-01-2009, 09:01 PM #14
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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); } } }
-
- 09-01-2009, 10:54 PM #16
Member
- Join Date
- Aug 2009
- Posts
- 17
- Rep Power
- 0
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(); } }); } }
- 09-01-2009, 11:10 PM #17
Member
- Join Date
- Aug 2009
- Posts
- 17
- Rep Power
- 0
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.
-
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.
-
For instance, compare your code with this:
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.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); } }
-
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); } }
Similar Threads
-
multiple program
By mm2236 in forum Threads and SynchronizationReplies: 2Last Post: 10-02-2009, 12:27 PM -
Multiple window
By shoeb83 in forum New To JavaReplies: 1Last Post: 08-30-2009, 10:59 PM -
Error message in executing a simple program from DOS window
By betelgeuse in forum New To JavaReplies: 6Last Post: 02-24-2009, 02:50 PM -
Calling a JFrame window from a command line program.
By new_2_java in forum New To JavaReplies: 7Last Post: 11-09-2008, 03:40 AM -
Doubt in simultaneous 'implementation' and 'extension'
By ajaygargnsit in forum New To JavaReplies: 2Last Post: 12-20-2007, 09:33 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks