Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #661

Re: silly newbie GUI ques

From "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this>
Subject Re: silly newbie GUI ques
Message-ID <XKZph.176133$gl2.75614@newsfe16.lga> (permalink)
Newsgroups comp.lang.java.gui
References <1168655443.129951.23220@m58g2000cwm.googlegroups.com>
Date 2011-04-27 15:28 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.gui
printdude1968@gmail.com wrote:
> Knute Johnson wrote:
>> printdude1968@gmail.com wrote:
>>> I must be missing something.  None of the books I have or web sites
>>> I've visited talk about calling one java program from another.  I have
>>> a "main" program which is a GUI.  I have 3 buttons which when pushed,
>>> should call subsequent programs (classes) which have buttons on them
>>> and so on and so forth.  But what is the command that I need to make my
>>> button action listener do to call the "child" program.  I've searched
>>> the API and other resources, and now I have no other choice but to pose
>>> my question here.  I "suspect" that I may need to make inner classes,
>>> but I would really like to have my main program call and invoke 3
>>> others and then when they are done, come back.
>>>
>>> Here's a pseudo code program
>>>
>>> if add-button.pressed
>>>   terminate the current program
>>>   call the add program
>>>      return to calling program
>>> fi
>>>
>>> if change-button.pressed
>>>   terminate the current program
>>>   call the change program
>>>      return to calling program
>>> fi
>>>
>>>
>>> Maybe I'm looking at this the wrong way.  I think I can write the code
>>> for each program, but getting control to pass from one to the other is
>>> a bottle-neck for me.
>>>
>> Do these buttons really start new programs?  Does pressing button 1
>> invoke a text editor and pressing button 2 invoke a spread sheet
>> program?  Or are you just looking to get input for your existing GUI
>> program?
>>
>> What exactly are you trying to do?
>>
>> --
>>
>> Knute Johnson
>> email s/nospam/knute/
> 
> Each button has a function... Add/Change/Delete.  I want to use one
> program /class file for each of these because I think my program may be
> a bit larger than the ideal (one program, one function) if I use the
> same class file for all 3 in addition to the main function.
> 
> Pressing the ADD button would, in my mind, launch another program which
> presents a GUI as would DELETE and CHANGE.... does this make sense?  Do
> java programmers code their apps this way... with a main program which
> calls subprograms?
> 

RedGrittyBrick gave you some good suggestions.  The decision of when to 
make a class or method can be very complicated even to experienced Java 
programmers.  It depends a lot on what you are trying to achieve.  I can 
tell you though that code reuse is a very very complicated process and 
probably not worth the effort especially in the beginning.  You will end 
up rewriting most of your code many times before you ever start reusing 
any quantity of it.

If your GUI is going to present some object that you are then going to 
modify by way of the buttons, that code should probably be contained in 
the GUI main class.  You can create new Frames or Dialogs to display it 
while it is being modified and those may be in separate classes if they 
are complex enough.

When I write a GUI program I first decide what I want it to look like, 
then how I want to control it (menus, buttons etc).  I will usually 
write the GUI shell with all of the bits but no data handling yet.  Then 
I go back and create all of the I/O and modification code that responds 
to the menus and buttons.  Everybody probably has a different routine 
and those that learned to program in an object oriented environment are 
probably not as linear thinking as I am.

Anyway you still didn't tell us exactly what your program is going to 
do.  Except that you have three things you want to do with your data, 
Add/Change/Delete.  So knowing very little about the task at hand here 
is how I would start.

1)  Create a GUI by extending either a J/Frame or J/Panel
2)  create a component to display your data
3)  add the J/Buttons for the add/change/delete functions
4)  if the functions are complicated enough make components to handle 
the changes or else just put the change code in the button's action listener

So here is a very simplified example:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JPanel implements ActionListener {
     JTextArea ta;
     JButton add,change,del;

     public test() {
         setLayout(new BorderLayout());

         ta = new JTextArea("Initial Data",10,30);
         JScrollPane sp = new JScrollPane(ta);
         add(sp,BorderLayout.CENTER);

         JPanel buttonPanel = new JPanel();
         add = new JButton("Add");
         add.addActionListener(this);
         buttonPanel.add(add);
         change = new JButton("Change");
         change.addActionListener(this);
         buttonPanel.add(change);
         del = new JButton("Delete");
         del.addActionListener(this);
         buttonPanel.add(del);
         add(buttonPanel,BorderLayout.SOUTH);
     }

     public void actionPerformed(ActionEvent ae) {
         String ac = ae.getActionCommand();
         if (ac.equals("Change")) {
             String str = ta.getText();
             str = str.toLowerCase();
             ta.setText(str);
         }
     }

     public static void main(String[] args) {
         Runnable r = new Runnable() {
             public void run() {
                 JFrame f = new JFrame();
                 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                 test t = new test();
                 f.add(t,BorderLayout.CENTER);
                 f.pack();
                 f.setVisible(true);
             }
         };
         EventQueue.invokeLater(r);
     }
}

knute...

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

silly newbie GUI question printdude1968@gmail.com.remove-dii-this - 2011-04-27 15:28 +0000
  Re: silly newbie GUI ques "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
    Re: silly newbie GUI ques printdude1968@gmail.com.remove-dii-this - 2011-04-27 15:28 +0000
      Re: silly newbie GUI ques "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
  Re: silly newbie GUI ques "RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
    Re: silly newbie GUI ques printdude1968@gmail.com.remove-dii-this - 2011-04-27 15:28 +0000
      Re: silly newbie GUI ques "RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
        Re: silly newbie GUI ques "RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
          Re: silly newbie GUI ques printdude1968@gmail.com.remove-dii-this - 2011-04-27 15:28 +0000
            Re: silly newbie GUI ques "RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
              Re: silly newbie GUI ques printdude1968@gmail.com.remove-dii-this - 2011-04-27 15:28 +0000
                Re: silly newbie GUI ques "RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
              Re: silly newbie GUI ques printdude1968@gmail.com.remove-dii-this - 2011-04-27 15:28 +0000
                Re: silly newbie GUI ques "RedGrittyBrick" <redgrittybrick@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000
  Re: silly newbie GUI ques "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:28 +0000

csiph-web