1

Hi I'm a final year engineering student, and though I have learnt java, my knowledge in its application is rudimentary. However, I have to use java for my project. This is the front end code which I've written:

    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.util.*;


    public class ex extends Applet 
    {
       static Frame f;
       DrawCanvas can;
       SetControls cont;

    public void init() 
    {
           can=new DrawCanvas();
           setLayout(new BorderLayout());
           add("Center", can);
           add("North", cont = new SetControls(can, f));
    }

            public void destroy() 
    {
           remove(cont);    
           remove(can);
        }

        public void start() 
    {
           cont.setEnabled(true);
        }

            public void stop() 
    {
           cont.setEnabled(false);
        }

            public void processWindowEvent(AWTEvent e) 
    {
           if (e.getID() == Event.WINDOW_DESTROY) 
        {
                        System.exit(0);
        }
        }

        public static void main(String args[]) 
    {
            f = new Frame("Title");
            ex fe = new ex();

            fe.init();
            fe.start();

           f.add("Center", fe);
           f.setSize(1500,1500);

           f.show();
        }
}

    class DrawCanvas extends Canvas
{
    //declaration of variables
    public DrawCanvas()
    {
    }

    public void paint(Graphics g)
    {
        //various flags are set for functions to be called and what shud 
                //be drawn when those functions are called

    }   
}


    class SetControls extends Panel implements ActionListener
{
       TextArea text1;
       TextField txt;
       Button load, init1, cluster,exit;
       Label head, vig;

           //declaration of variables used

           FileDialog fc;
       String fname;
       Frame f;
       DrawCanvas canvas;

       public SetControls(DrawCanvas can, Frame f) 
    {
             //initialization of variables used

             canvas = can;
            this.f = f;

           head = new Label("Clustering of Genes:");
           cluster = new Button("Cluster Now");
           load = new Button("Load");
           init1 = new Button("Filter");
           exit = new Button("Exit");
           txt = new TextField(2);
           text1 = new TextArea(5,80);
           vig = new Label("Parameter");


           load.addActionListener(this);
           init1.addActionListener(this);
               txt.addActionListener(this);
               cluster.addActionListener(this);
           exit.addActionListener(this);


           add(head);  
           add(load);
           add(init1);
           add(text1);
           add(vig);
           add(txt);
           add(cluster);
           add(exit);

    }

public void actionPerformed(ActionEvent ae) 
    {
    String str = ae.getActionCommand();
    System.out.println("Command: " + str);

    if(str.equals("Load"))
        {
        text1.setText("Loading microarray data file...");

        fc = new FileDialog(f, "Choose a file", FileDialog.LOAD);
        fc.setDirectory(".");
        fc.setVisible(true);
        fname = fc.getFile();
        //function call to read the file    
        text1.setText("Input Data File Loaded: " + fname);      
        }
    else if((str.equals("Filter")||str.equals("Cluster Now")))  
        {
        if(str.equals("Filter"))
            {
            //function calls and setting of text are
            }   
        else
            {
            //function calls and setting of text area
                }   

        } 


    else{
        //call close function when exit is pressed 
        }           
    }
}

I know you're thinking why did I use AWT instead of swing but at the moment I am more comfortable with AWT. So far, the code is working great. But the problem is, when I press one of the buttons, the information that displays is a lot and goes beyond the frame; so I want to add a scrollbar or a scrollpane so that I can, well, scroll down to see the rest of the info. I hav tried a lot of things like f.add(new ScrollPane()) etc but none of them are working. Please help me!

ppra
  • 31
  • 2
  • 4
  • check this out http://www.coderanch.com/t/533808/GUI/java/add-Scrollbar-Frame-AWT – Rachel Gallen May 12 '13 at 16:17
  • This is how you add something to the center or the south, not with a string: http://docs.oracle.com/javase/7/docs/api/java/awt/Container.html#add%28java.awt.Component,%20java.lang.Object%29 – Leo Izen May 12 '13 at 16:19
  • 1) Please add an upper case letter at the start of sentences. Also use a capital for the word I, and abreviations and acronyms like JEE or WAR. This makes it easier for people to understand and help. 2) Why code an applet? If it is due due to spec. by teacher, please refer them to [Why CS teachers should stop teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 3) Use Swing not AWT. See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson May 12 '13 at 16:25
  • *"at the moment i am more comfortable with awt."* Since your code is broken and/or you do not know how to proceed (I could not wade through that wall of mumbling to get to the basis of your inquiry), it seems you need help from us. Well her's the rub. Very few people have used AWT, and those that did have largely forgotten the details! Use Swing.. – Andrew Thompson May 12 '13 at 16:28
  • 2
    If you cannot help me, please do not insult me. Like I said, I'm new. – ppra May 12 '13 at 16:39

1 Answers1

2
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Scrollbar;

public class AWTScrollbar {

public static void main(String[] args) {

    Frame frame = new Frame("Scrollbar");

    FlowLayout layout = new FlowLayout();

    frame.setLayout(layout);

    Label label = new Label("Scrollbar");

    frame.add(label);

    Scrollbar scrollbar = new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 255);

    frame.add(scrollbar);

    frame.setVisible(true);

    frame.pack();

}

}

May be this will be useful for you :)

Akash Shinde
  • 925
  • 3
  • 14
  • 31