Help with java

Discussion in 'OT Technology' started by Rommel, Apr 30, 2003.

  1. Rommel

    Rommel friends forever

    Joined:
    Jul 31, 2001
    Messages:
    36,867
    Likes Received:
    0
    Location:
    /yourself now
    Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.event.ActionListener;
    
    //a very basic GUI
    //still to do 27/2/03:
    // add a display for total
    // make it work:)
    public class GUI extends JFrame  {
    
    	//instance variables
      protected ButtonPanel topPanel;
      protected TextPanel  textPanel;
      protected JTextField textName;
      protected JTextField textEmail;
      protected JTextField textNumber;
      protected  int  iteratorPosition = 0;
    	protected SortedListIterator students;
    
    
    	protected Comparable data [] = new Comparable[iteratorPosition];
    
    
    
    
    
    
    
      //creates a static window that is consistent for all frames
    
    
      public static JFrame aWindow = new JFrame("Computer Address Book");
    
      public GUI() {
    
    		Toolkit theKit = aWindow.getToolkit();
        Dimension wndSize = theKit.getScreenSize();
        int windowWidth  = 500;
        int windowHeight = 350;
        aWindow.setBounds(wndSize.width/4, wndSize.height/4,
    											windowWidth, windowHeight);
    
    
        Container content = aWindow.getContentPane();
        topPanel = new ButtonPanel();
    
        textPanel = new TextPanel();
    		content.setLayout(new BorderLayout());
    		content.add(textPanel, BorderLayout.WEST);
    
    		content.add (topPanel, BorderLayout.NORTH);
    
        content.add(textPanel, BorderLayout.WEST);
    
        //sets the default close operation
        aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //sets the JFrame as visible
    		aWindow.setVisible(true);
        aWindow.pack();
      }
    
    
    
        public static void main(String [] args) {
       
            new GUI();
       }
    
    
      class ButtonPanel extends JPanel implements ActionListener {
    
        public ButtonPanel() {
    			FlowLayout flow = new FlowLayout();
          //Get the content pane
        	this.setLayout(flow);
    
        ///////////////////////////////////////////////////////////////////////////////////////
        	String[] buttons = {"Prev","Load","Save","Add","Find",
                                    "Delete","Next"};
    
        	JButton button;
        		for(int i = 0; i< buttons.length; i++) {
    
          		this.add((button = new JButton(buttons[i])),
    																					BorderLayout.SOUTH);
            	button.addActionListener(this);
        		}
        }
    
    	public void actionPerformed(ActionEvent e) {
            String action=e.getActionCommand();
    
            if (action.equals("Next")) {
                students.hasNext();
                }
            else if (action.equals("Add")) {
                Person temp = new Person(textName.getText(),textEmail.getText(), textNumber.getText());
                students.add(temp);
    
    						}
    
    
        }
    
    }
    
    
    
    
    
    
    
    
    
    
    
     class TextPanel extends JPanel {
    
     		public TextPanel() {
    		Box right = Box.createVerticalBox();
        JPanel bottomPanel = new JPanel();
        JPanel emailPanel  = new JPanel();
        JPanel numberPanel = new JPanel();
        Border edge = LineBorder.createBlackLineBorder();
        JLabel label;
        Dimension size = new Dimension(90, 25);
    
    
    
    
    
    
    
    		//Creates a name label and text box
        bottomPanel.add(label = new JLabel("  Name"));
        label.setBorder(edge);
        label.setPreferredSize(size);
    
    [b]how the fuck can i get this working so that it initialises with the first item in the array[/b]
        JTextField textName = new JTextField("Enter Name",10);
        textName.setText(String.valueOf(students(data[iteratorPosition]).getName()));
    
        bottomPanel.add(textName);
    
    
    
    		//Creates a Email label and text box
        emailPanel.add(label = new JLabel("  Email"));
        label.setBorder(edge);
        label.setPreferredSize(size);
    
        JTextField textEmail = new JTextField("Enter Email",10);
        textEmail.setText("cool beans");
        emailPanel.add(textEmail);
    
        //textEmail.setPreferredSize(size);
    
        //Creates a Number label and text box
        numberPanel.add(label = new JLabel("  Tel Number"));
        label.setBorder(edge);
        label.setPreferredSize(size);
    
        JTextField textNumber = new JTextField("Enter Number",10);
        textNumber.setText("cool ");
        numberPanel.add(textNumber);
    
    
    
        //right.setLayout(new BorderLayout());
        right.add(Box.createVerticalStrut(5));
        right.add(bottomPanel, BorderLayout.NORTH);
    		right.add(Box.createVerticalStrut(20));
        right.add(emailPanel, BorderLayout.CENTER);
        right.add(Box.createVerticalStrut(20));
        right.add(numberPanel, BorderLayout.SOUTH);
        right.add(Box.createVerticalStrut(10));
        this.setBorder(new TitledBorder(new EtchedBorder(),""));
    
    		this.add(right,BorderLayout.WEST);
    
        }
     }
     }
    
    
    second class

    Code:
    import java.util.Iterator;
    
    public class SortedListIterator  implements Iterator{
        public static final int DEFAULT  = 50;
    //handling data    
        private int max=DEFAULT;
        private int numElements;
        protected Comparable data[];
    //dealing with iteration
        protected int iteratorPosition;
    
    //////////////////////////////////////////////////////////////
        public SortedListIterator() {
            numElements=0;
            max=DEFAULT;
            data=new Comparable[max];
        }
         
        public SortedListIterator(int newmax)  {
            numElements=0;
            max=newmax;
            data=new Comparable[max];
        }
    
        //lots of methods missed out
    
        public void add (Comparable obj) {
            int where = numElements;      //where to insert
            for (int i=0;i<numElements;i++){
                if (data[i].compareTo(obj) > 0){
                    where = i;
                    break;
                }
            }
            for (int i=numElements;i>where;i--) {
                data[i]=data[i-1];
            }
    
            data[where]=obj;
            numElements++;
        }
    
        public String toString() {
            String output="";
            for (int i=0;i<numElements;i++) {
                output=output+data[i].toString()+"\n\n";
            }
            return output;
        }
    
        ///////////////////////ITERATOR stuff
    
        public Iterator iterator() {
            iteratorPosition=0;
            return this;
        }
    
        public boolean hasNext() {
            return iteratorPosition!=numElements;
        }
    
        public Object next(){
            Object temp=data[iteratorPosition];
            iteratorPosition++;
            return temp;
        }
    
        public void remove() {}
    
    ///////////////////////test with Strings
    // notice that I try to test the start and the end of the list as
    // well as the middle
        public static void main(String args[]) {
            Person  s1 = new Person("Jim bob", "[email protected]",
                                   "4545654654", "judo, politics, games");
    
            Person  s2 = new Person("bobby robers", "[email protected]",
                                    "456456654", "judo, politics, games");
    
            Person  s3 = new Person("Calvin Jacobs", "[email protected]",
                                   "546432435", "judo, politics, games");
    
            Person  s4 = new Person("Ben Hurst", "[email protected]",
                                   "456345543", "judo, politics, games");
    
    
    
    
    
            persons.add(s1);
            persons.add(s2);
            persons.add(s3);
            persons.add(s4);
    
            System.out.println(persons);
    
            Person find = new Person("Rozhan Mallender", "[email protected]",
                                   "01782751449", "judo, politics, games");
            //persons.remove(find);
            //System.out.println(persons);
            //using toString
            ///System.out.println(persons.toString());
    //using Iterator - WHY BOTHER????????
            Iterator it = persons.iterator();
            while (it.hasNext()) {
                System.out.println(it.next() + "\n");
            }
        }       
    }
            
            
    
    its the only erro i got at the moment and its killing me, i jsut cant get it too work and i know its something really simple, any ideas people

    there is a third class but that is jsut the person class with basically sets and gets and implementing comprable
     
  2. Rommel

    Rommel friends forever

    Joined:
    Jul 31, 2001
    Messages:
    36,867
    Likes Received:
    0
    Location:
    /yourself now
    well the problem is in bold and is in the textpanel where i want to initialise the textpanel to the name part of the array
     
  3. Rommel

    Rommel friends forever

    Joined:
    Jul 31, 2001
    Messages:
    36,867
    Likes Received:
    0
    Location:
    /yourself now
  4. Leb_CRX

    Leb_CRX OT's resident terrorist

    Joined:
    Apr 22, 2001
    Messages:
    39,994
    Likes Received:
    0
    Location:
    Ottawa, Canada
    hmm nevermind, brain fart

    :/

    PEACE
     
    Last edited: May 1, 2003
  5. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    anyone got a good tutorial on java gui? i really wanna learn!
     
  6. Leb_CRX

    Leb_CRX OT's resident terrorist

    Joined:
    Apr 22, 2001
    Messages:
    39,994
    Likes Received:
    0
    Location:
    Ottawa, Canada
    cyber, try planet-source-code.com and I've seen source code listed on java.sun.com but never checked them out, but I am sure they got some
     
  7. Rommel

    Rommel friends forever

    Joined:
    Jul 31, 2001
    Messages:
    36,867
    Likes Received:
    0
    Location:
    /yourself now
    fuckin hijacker:big grin:

    someone help me
     
  8. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    yeah im gonna start learning swing tonigh :big grin: just downloaded Eclipse SDK :big grin:

    oh yeah, np for the thread hijack. ill look at ur code once i figure out awt & swt
     
  9. Rommel

    Rommel friends forever

    Joined:
    Jul 31, 2001
    Messages:
    36,867
    Likes Received:
    0
    Location:
    /yourself now
    anytime in the next week eh:big grin:
     

Share This Page