Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Text to other panels

Thread view: 
Mike - 13 Aug 2006 23:57 GMT
I am learning how Java works and don't quite understand the
ActionListeners and how to control other panels. I wrotes this program
that is for inventory. It has 3 main windows (GUI) open, 1 is the
status and error messages, 1 is for the buttonbs and the other has the
inventory detail. I want to be able to puch the NEXT or Previous button
and move forward or backward through the inventory and post the updated
material in the main Inventory window.  Here is the program i wrote,
doesn't work completely - I get errors when i poush NEXT or PREVIOUS,
the other buttons don't so anything yet. I would appreciate some
insight on this, TIA. Oh yea, I preloaded the arrays with some
informatin so i can test it out.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Test extends Applet implements ActionListener,
WindowListener {
   // ----  Variables Initialization   ----

   // Fields
   Button    BAdd, BChange, BDelete, BClear, BNext, BPrevious,
BSearch, BLoad, BSave;
   TextArea  TID, TTitle, TQty, TPrice, TRestock, TValue, TGValue;

   // Standard Variables
   int Current = 0; // Current position in array
   int Array = 0; //
   int counter = 0;
   static double GTotal = 0.00;
   String CMsg = ""; // Current Message
   String LMsg = ""; // Last Message
   String EMsg = ""; // Error Message
   String Tag = "";
   // Array Variables
   static double[] DVDValue = new double[50];
   static double[] DVDRestock = new double[50];

   String DVDTitle[] = {"Armageddon",
           "Boondock Saints",
           "The Replacement Killers",
           "Hardboiled",
           "Euro Trip",
           "Eureka",
           "Band of Brothers",
           "United Flight 93",
           "Silent Hill",
           "The 4400"};
   String Title = "";
   String DVDGenre[] = {"Sci-Fi",
           "Drama",
           "Action",
           "Action",
           "Comedy",
           "Sci-Fi",
           "Action",
           "Docu-Drama",
           "Horror",
           "Sci-Fi"};
   String Genre = "";
   int DVDID[] = {5125,2105,5120,1512,5126,8485,4679,7156,4156,1206};
   static int DVDQty[] = {12,15,10,9,15,12,8,21,6,11};
   static double DVDPrice[] =
{12.15,15.09,13.08,18.99,12.05,15.71,16.31,18.17,21.05,27.39};
   static int ArrayLength = 10;

   public static void main(String[] args)
   {
       for (int counter = 0;counter<ArrayLength; counter++)
       {
           DVDValue[counter]=DVDPrice[counter]*DVDQty[counter];
           DVDRestock[counter] = DVDPrice[counter] * .05;
           GTotal = GTotal + DVDValue[counter];
       }
       // -----   Frames Setup   -----

       // Create Inventory detail frame
       Frame F=new Frame("Inventory Detail: Inventory Total:
"+GTotal);
       Test BD=new Test();
       F.setSize(600, 250);
       F.addWindowListener(BD);
       F.add(BD);
       BD.init();
       BD.start();
       F.setVisible(true);

       // Create User Control Frame
       Frame F2=new Frame("User Controls");
       Test BD2=new Test();
       F2.setSize(280, 130);
       F2.addWindowListener(BD2);
       F2.add(BD2);
       BD2.init2();
       BD2.start();
       F2.setVisible(true);

       // Create Message Frame
       Frame F3=new Frame("Message Panel");
       Test BD3=new Test();
       F3.setSize(500, 215);
       F3.addWindowListener(BD3);
       F3.add(BD3);
       BD3.init3();
       BD3.start();
       F3.setVisible(true);

       // Create Company Logo Frame
       Frame F4=new Frame("Company Logo");
       Test BD4=new Test();
       F4.setSize(200, 220);
       F4.addWindowListener(BD4);
       F4.add(BD4);
       BD4.init4();
       BD4.start();
       F4.setVisible(true);

   }

   public void init()
   {
       //  --- Defining the Panels for each Frame ---
       setLayout(new FlowLayout());
       Panel p1;
       p1=new Panel();
       p1.setLayout(new GridLayout(8,2, 1,1));
       // JLabel ImageLabel=new JLabel("Inventory Detail
Screen",SwingConstants.LEFT);

       Tag = "Title: "+DVDTitle[Current];
       JLabel Title=new JLabel(Tag, Label.LEFT);
       Title.setFont(new Font("Serif",Font.BOLD,22));
       p1.add(Title);

       Tag = "ID: "+DVDID[Current];
       JLabel ID=new JLabel(Tag, Label.LEFT);
       ID.setFont(new Font("Serif",Font.BOLD,20));
       p1.add(ID);

       Tag = "Genre: "+DVDGenre[Current];
       JLabel Genre=new JLabel(Tag, Label.LEFT);
       Genre.setFont(new Font("Serif",Font.BOLD,20));
       p1.add(Genre);

       Tag = "Qty: "+DVDQty[Current]+"     Price: "+DVDPrice[Current];
       JLabel QtyPrice=new JLabel(Tag, Label.LEFT);
       QtyPrice.setFont(new Font("Serif",Font.BOLD,20));
       p1.add(QtyPrice);
       add(p1);

       Tag = "Restock Fee: "+DVDRestock[Current]+"     Value:
"+DVDValue[Current];
       JLabel RestockValue=new JLabel(Tag, Label.LEFT);
       RestockValue.setFont(new Font("Serif",Font.BOLD,20));
       p1.add(RestockValue);
       add(p1);

       Tag = " ";
       JLabel Blank=new JLabel(Tag, Label.LEFT);
       Blank.setFont(new Font("Serif",Font.BOLD,20));
       p1.add(Blank);
       add(p1);

       Tag = "Record # 1 of "+ArrayLength;
       JLabel Records=new JLabel(Tag, Label.LEFT);
       Records.setFont(new Font("Serif",Font.BOLD,20));
       p1.add(Records);
       add(p1);
   }

   // Message Panel
   public void init3()
   {
       setLayout(new FlowLayout());
       Panel p;
       p=new Panel();
       p.setLayout(new GridLayout(10,1, 1,1));

       JLabel CMsg=new JLabel("Current Message: ", JLabel.CENTER);
       CMsg.setFont(new Font("Serif",Font.BOLD,20));
       p.add(CMsg);

       JLabel CMsgHold=new JLabel("Current Message Place Marker",
JLabel.CENTER);
       CMsgHold.setFont(new Font("Serif",Font.ITALIC,20));
       p.add(CMsgHold);

       JLabel Blank1=new JLabel("", JLabel.CENTER);
       Blank1.setFont(new Font("Serif",Font.ITALIC,20));
       p.add(Blank1);

       JLabel LMsg=new JLabel("Last Message: ", JLabel.CENTER);
       LMsg.setFont(new Font("Serif",Font.BOLD,20));
       p.add(LMsg);

       JLabel LMsgHold=new JLabel("Last Message Place Marker",
JLabel.CENTER);
       LMsgHold.setFont(new Font("Serif",Font.ITALIC,20));
       p.add(LMsgHold);
       p.add(Blank1);

       JLabel EMsg=new JLabel("Error: ", JLabel.CENTER);
       EMsg.setFont(new Font("Serif",Font.BOLD,20));
       p.add(EMsg);
       add(p);

       JLabel EMsgHold=new JLabel("Error Message Place Marker",
JLabel.CENTER);
       EMsgHold.setFont(new Font("Serif",Font.ITALIC,20));
       p.add(EMsgHold);
       p.add(Blank1);
   }

   // Company Logo
   public void init4()
   {
       setLayout(new FlowLayout());
       Panel p3;
       // === labels
       p3=new Panel();
       p3.setLayout(new GridLayout(3,3, 5,5));
       JLabel la1=new JLabel("",new ImageIcon("Logo1.gif"), Current);
       p3.add(la1);
       add(p3);
   }

   // User Controls
   public void init2()
   {
       //setLayout(new FlowLayout());
       Panel p2;
       p2 = new Panel();
       p2.setLayout(new FlowLayout());
          p2.setLayout(new GridLayout(4,3, 2,2));

          JButton BAdd = new JButton("Add", new ImageIcon("Add.gif"));
          p2.add(BAdd);

          JButton BChange = new JButton("Change", new
ImageIcon("Add.gif"));
          BChange.setActionCommand("disable");
          p2.add(BChange);

          JButton BDelete = new JButton("Delete", new
ImageIcon("Add.gif"));
          p2.add(BDelete);

          JButton BClear = new JButton("Clear", new ImageIcon("Add.gif"));
          p2.add(BClear);

          JButton BNext = new JButton("Next", new ImageIcon("Add.gif"));
          p2.add(BNext);

          JButton BPrevious = new JButton("Previous", new
ImageIcon("Add.gif"));
          p2.add(BPrevious);

          JButton BSearch = new JButton("Seacrh", new
ImageIcon("Add.gif"));
          p2.add(BSearch);

          JButton BLoad = new JButton("Load", new ImageIcon("Add.gif"));
          p2.add(BLoad);

          JButton BSave = new JButton("Save", new ImageIcon("Add.gif"));
          p2.add(BSave);
          add(p2);

          // Create Button Handler
          ButtonHandler handler = new ButtonHandler();
          BNext.addActionListener(handler);
          BPrevious.addActionListener(handler);

   }
   private class ButtonHandler implements ActionListener
   {
        public void actionPerformed(ActionEvent event)
        {
            if (event.getActionCommand()=="Next")
            {
                if (Current==ArrayLength)
                {
                    Current = 0;
                }
                else
                {
                    Current = Current +1;
                }

            }
            else if(event.getActionCommand()=="Previous")
            {
                if (Current == 0)
                {
                    Current = ArrayLength;
                }
                else
                {
                    Current = Current -1;
                }
;
            }
            Panel p1 = null;
           Tag = "Title: "+DVDTitle[Current];

           (Tag, Label.LEFT);
           Title.setFont(new Font("Serif",Font.BOLD,22));
           p1.add(Title);

           Tag = "ID: "+DVDID[Current];
           JLabel ID=new JLabel(Tag, Label.LEFT);
           ID.setFont(new Font("Serif",Font.BOLD,20));
           p1.add(ID);

           Tag = "Genre: "+DVDGenre[Current];
           JLabel Genre=new JLabel(Tag, Label.LEFT);
           Genre.setFont(new Font("Serif",Font.BOLD,20));
           p1.add(Genre);

           Tag = "Qty: "+DVDQty[Current]+"     Price:
"+DVDPrice[Current];
           JLabel QtyPrice=new JLabel(Tag, Label.LEFT);
           QtyPrice.setFont(new Font("Serif",Font.BOLD,20));
           p1.add(QtyPrice);
           add(p1);

           Tag = "Restock Fee: "+DVDRestock[Current]+"     Value:
"+DVDValue[Current];
           JLabel RestockValue=new JLabel(Tag, Label.LEFT);
           RestockValue.setFont(new Font("Serif",Font.BOLD,20));
           p1.add(RestockValue);
           add(p1);

           Tag = " ";
           JLabel Blank=new JLabel(Tag, Label.LEFT);
           Blank.setFont(new Font("Serif",Font.BOLD,20));
           p1.add(Blank);
           add(p1);

           Tag = "Record # "+Current+" of "+ArrayLength;
           JLabel Records=new JLabel(Tag, Label.LEFT);
           Records.setFont(new Font("Serif",Font.BOLD,20));
           p1.add(Records);
           add(p1);

        }

   }

   public void start() { }

   public void stop() { }

// INTERFACE WindowListener -------
   public void windowClosing(WindowEvent e)      { stop();
System.exit(0); }
   public void windowClosed(WindowEvent e)       {  }
   public void windowOpened(WindowEvent e)       {  }
   public void windowIconified(WindowEvent e)    {  }
   public void windowDeiconified(WindowEvent e)  {  }
   public void windowActivated(WindowEvent e)    {  }
   public void windowDeactivated(WindowEvent e)  {  }

    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
       
    }

   

}
Andrew Thompson - 14 Aug 2006 05:29 GMT
> I am learning how Java works
...
> public class Test extends Applet ...

The combination of those two - 'learning Java' & 'applets',
not recommended.

Applets should only be attemtped once you have a good
grasp on both building and running packaged command line
examples, and have a sgnificant amount of experience in
the GUI toolkits.  (Even then, it is easier to develop
applets as a combined application/applet).

And in regard to..

> ... I get errors ..

Please copy/paste error messages, rather than simply
mentioning them.

BTW - did you have a question?  You made some
statements about what you wanted, perhaps you
could rephrase the statements into a question.

Andrew T.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.