designing application


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: designing application

  1. #1
    Join Date
    Sep 2003
    Posts
    21

    designing application

    I finally found some helpful code to start my application with a JTabbedPane. It looks and feels like I want, but the next big question I have is knowing what is the best way to deal with each "panel", the tabs you click on each will be set up differently, with a logical title and different controls with events that work, I hope.

    Would it be wise to make each panel [tab] a separate class based with it's own main method? This is the part of application design I do not get.

    It seems I could theorhetically put everything in one method and have subclasses inside the main, but that seems like it uses a lot of resources.

    Here's what I have so far, and hopefully I can get some direction to begin work on each panel [tab].

    Example, tab 1 would be the main tab at start up of application. Here is where I want the user to be able to add new tools to an inventory list [a sequential file]. Since I have not learned about databases in this Java class, and only know about sequential text files, the only thing I know how to do is read and write and access records, using String Tokenizers and such.
    I want this to be a usable application for a user with no understanding of files, databases, etc.

    Tab 2 would be where user could view and print the file
    Tab 3 would be access to a simple browser in case to look up tools at a specifically coded URL and the option to type in a new URL [address line on IE or Netscape browsers]
    Tab4 would be where user could sort the records based on type of tool, cost, etc. And view all tools of certain type, price range or whatever
    Tab5 would be where the user could get the total of all tools and input a date range of when added to file, to keep track of all the items bought in a single calendar year and how much $$ for tax purposes.

    Here's my simple code that shows the tabs, and I just coded some different controls to make sure I was doing things correctly. The code per panel[tab] will of course be different, but I need direction about design of application before I go any further.

    Code:
    import javax.swing.*;
    import java.awt.event.*;
    
    public class startUp 
    {
         public static void main(String[] args)
        {
           JFrame frame = new JFrame("Tool Inventory");
           
           //handle window close
           
           frame.addWindowListener(new WindowAdapter()
           {
             public void windowClosing(WindowEvent e)
             {
                System.exit(0);   
             }
           });
        
        //set up panels for tabbed pages
        
        JPanel tab1 = new JPanel();
        JPanel tab2 = new JPanel();
        JPanel tab3 = new JPanel();
        JPanel tab4 = new JPanel();
        JPanel tab5 = new JPanel();
        
        tab1.add(new JButton("Button 1"));
        tab2.add(new JPanel());
        tab3.add(new JRadioButton("Radio Button 3"));
        tab4.add(new JList());
        tab5.add(new JComboBox());
        
        //set up JTabbedPane object and add panels
        JTabbedPane jtp = new JTabbedPane();
        
        jtp.add("Tab 1", tab1);
        jtp.add("Tab 2", tab2);
        jtp.add("Tab 3", tab3);
        jtp.add("Tab 4", tab4);
        jtp.add("Tab 5", tab5);
        
        //display
        frame.getContentPane().add(jtp);
        frame.setSize(500,500);
        frame.setLocation(20,20);
        frame.setVisible(true);
        
        }
    Sue

  2. #2
    Join Date
    Sep 2003
    Posts
    21
    I know I wrote a lot in my previous post, but
    I wanted someone to get the overall picture of
    how I wanted my app to look.

    I just want to know if I should
    a) put all code in one class [named startUp]
    with subclasses and many methods and a main
    and what is the logical way to write the code for each panel?
    b) put each panel of the JTabbedPane into a separate
    class unto itself and instantiate it in the superclass [named startUP]
    c) do another way that isn't mentioned here.
    Sue

  3. #3
    Join Date
    Mar 2003
    Posts
    834
    (b) sounds better to me - modularized, self-contained code.
    ArchAngel.
    O:-)

  4. #4
    Join Date
    Sep 2003
    Posts
    21
    Ok, but I am not understanding how to do that in a JTabbedPane.

    I have been trying to put together in one big class
    and that has it's pitfalls, and since I am not good at breaking down code into modules [a problem I need to fix to be proficient at object oriented programming] I am a little stuck. All the programs we have done might have had separate classes but the frame/form that popped up was one form, not a Tabbed Pane that changes from one panel/form to another.

    I get lost in what class to create that has the main method and then when to put the instantiation for each panel/form/tab of the Tabbed Pane. Is tab1 part of the class that has the main method, for instance?

    Look at this code and see what I mean about being confused here. I cannot even take the program we wrote to create a browser because there was one class and inside that class were methods...I cannot write all that code inside a single class where the code for the other tabs on the Tabbed Pane are, it just doesn't work the way I have it. I get lost in how to access different variables and their methods.

    Code:
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.io.*;
    import java.net.*;
    
    public class startUPex extends JFrame implements ActionListener /*, HyperlinkListener  */
    
                                               
    {
        public static void main(String[] args)
        {
           JFrame frame = new JFrame("Tool Inventory");
           JTextField text = new JTextField(30);
           JEditorPane edit = new JEditorPane();
           
           //handle window close
           
           frame.addWindowListener(new WindowAdapter()
           {
             public void windowClosing(WindowEvent e)
             {
                System.exit(0);   
             }
           });
        
        //set up panels for tabbed pages
        
        JPanel tab1 = new JPanel();
        JPanel tab2 = new JPanel();
        JPanel tab3 = new JPanel();
        JPanel tab4 = new JPanel();
        JPanel tab5 = new JPanel();
        
        //variables 
        String input1 = "";  //for inputing on tab1
        
        //tab1 - Inputing records
        tab1.add(new JButton("Enter"));
        tab1.add(new JLabel("Tool name"));
        tab1.add(new JTextField(20));
        tab1.add(new JLabel("Tool cost"));
        tab1.add(new JTextField(20));
        tab1.add(new JLabel("Quantity"));
        tab1.add(new JTextField(10));
        
    this part does not seem to be right either, I need to 
    write a record to the file and later be able to print file or search file for specific records--which I have not learned yet
        writeIt(input1, "F://project examples/tool_inventory.txt");
        
        
        
        //tab2 - Viewing Files and their records
         
        tab2.add(new JLabel("File Name:"));
        tab2.add(new JTextField(20));
        tab2.add(new JTextArea());
        
        
        //tab3 - Sorting and searching records
        tab3.add(new JLabel("Search"));
        tab3.add(new JTextField(20));
        
        
        //tab4 - Web Browser
        URL url;
        tab4.add(text);
        tab4.add(edit);
        edit.setEditable(false);
       // text.addActionListener(frame);
       // edit.addHyperlinkListener(frame);
        
        //tab5 - Tax Form
        tab5.add(new JComboBox());
        
        //set up JTabbedPane object and add panels
        JTabbedPane jtp = new JTabbedPane();
        
        jtp.add("Input Tool", tab1);
        jtp.add("Files", tab2);
        jtp.add("Find Tool", tab3);
        jtp.add("Search Web", tab4);
        jtp.add("Tax Reporting", tab5);
        
        //display
        frame.getContentPane().add(jtp);
        frame.setSize(500,500);
        frame.setLocation(20,20);
        frame.setVisible(true);
        
        }
        
        //used on tab1
        public static void writeIt (String toWrite, String theFile)
        {   
            PrintWriter pw = null;   //initialize file
            try
            
            {
                //pass the file path from the main to this class
                pw = new PrintWriter(new BufferedWriter(new FileWriter(theFile,true)));
                //pass the string from the main to this class
                pw.println(toWrite);
                         
            }
            
            catch(Exception e)
            {
                
            }
            
            finally
            {
              //always input a finally block to close the file ALWAYS  
              pw.close();    
            }
        }
        
        //used on tab2
         public static void readIt (String theFile)
        {
            //first way to read a file
            File someFile = new File(theFile);   //creates a reference to a file 
            FileReader readFile = null;
            if (!someFile.exists())   //if someFile does not exist, get out of the method readIt
            {
                System.out.println("file does not exist!");
                return;
            }
            
            //read one character at a time, is fast because characters are data types not classes
            //which can eat up memory and time
            
            
            try 
            {
                readFile = new FileReader(theFile);
                int ch = readFile.read();                          //reading a character as an ASCII number
                while (ch != -1)
                   {
                      System.out.print((char)ch +"");               //casts int into a character and prints the character
                                                                    //and concatenate into a string
                      ch = readFile.read();
                      
                   }
            }
            
            catch (Exception e)
            {
                
            }
                
            finally
            { 
                try
                {
                   readFile.close();                                     //close file ALWAYS
           
                }
                
                catch(Exception e){}
            }
        }
        
         public void actionPerformed(ActionEvent e) {
         }
    
    
         
      /*   public void hyperlinkUpdate(HyperlinkEvent e)
         {
             HyperlinkEvent.EventType type = e.getEventType();
             try
             {
               if (type == HyperlinkEvent.EventType.ACTIVATED)
                   edit.setPage(e.getURL)):
               else if (type == HyperlinkEvent.EventType.ENTERED)
                   text.setText(e.getURL()+"");
               else if (type == HyperlinkEvent.EventType.EXITED)
                   text.setText("");
             }
             
             catch(Exception ex){}
         }*/
    }
    
    Sue

  5. #5
    Join Date
    Sep 2002
    Posts
    124
    Sue, ... I may call you Sue, mayn't I?

    Just reading the posts you've been sending to this site has been more than just eye-opening. Please keep experimenting. I imagine you're reading up "behind the scenes", so to speak.

    To be honest I reckon you're at the interesting stage. It's that weird bit where one has to experiment, but one almost feels like one has to ask permission or gain approval from someone.

    By all means, try starting by creating a startpoint of two ( or more ) JTabbedPane instances... you can add the individual code for each one which differentiates between the two... one template which is extended to tailor make an endless number ( or just two ) tabs. The MAIN just launches everything and gets things rolling.

    What's you're favourite hobby? What occupation ( and don't say programming, just for argument's sake ) Try to turn this / these into ....

    1. The task
    2. Split the task into items that need to be performed
    3. EXPERTS who can do each task
    4. a GENERAL who can MANAGE the task performers.

    You're the SUPREME COMMANDER who can PROMOTE other PEOPLE to do things for you.

    Sorry if it's all very vague, but I hate to see someone persevere so admirably yet not get a response. Actually, keeping within this sort of realm, translate what I've suggested into what you're endeavoring, and then step back and THEN ask specific questions.

    Best regards,
    Joyous monkey

  6. #6
    Join Date
    Sep 2003
    Posts
    21
    Yo JoyousMonkey, dude, you are helpful at least with helping me get my mind thinking about things differently than I have.

    My favorite hobbies are
    1) writing
    2) learning about different programming languages
    3) creating web pages
    4) whining about life
    Sue

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center