[315] in java-interest

home help back first fref pref prev next nref lref last post

Re: access to container or window

daemon@ATHENA.MIT.EDU (Jonathan Payne)
Wed Jun 14 15:35:13 1995

Date: Wed, 14 Jun 1995 12:06:01 -0700
From: jpayne@starwave.com (Jonathan Payne)
To: rizzotti.sven@ch.swissbank.com
Cc: java-interest@java.sun.com
In-Reply-To: <9506141304.AA24617@chbslu01.iis.ch.swissbank.com> (message from Sven Rizzotti on Wed, 14 Jun 95 15:04:10 +0200)
Reply-To: jpayne@starwave.com

> Mime-Version: 1.0 (NeXT Mail 3.3 v118.2)
> From: Sven Rizzotti <rizzotti.sven@ch.swissbank.com>
> Date: Wed, 14 Jun 95 15:04:10 +0200
> Sender: owner-java-interest@java.sun.com
> Precedence: bulk
> X-Info: To unsubscribe, send 'unsubscribe' to java-interest-request@java.sun.com
> Content-Type: text/plain
> Content-Length: 286
> 
> A lot of the classes like List, TextField etc. need a container which can be  
> a form or a window. where do I get this variable from inside my applet  
> subclass ?
> 
> __Sven
> -
> Note to Sun employees: this is an EXTERNAL mailing list!
> Info: send 'help' to java-interest-request@java.sun.com
> 

Here's a starting point (I just threw this together, didn't try to
compile it, left out a bunch of methods) for a scrolling list that you
can put inside an applet.

E.g.,

class ScrollingList extends AppComponent {
    String items[] = new String[5];
    int	nitems = 0;
    int rowsToDisplay = 10;
    Font font;
    int selection[] = new int[0];

    public void addItem(String item) {
	if (nitems >= items.length) {
	    String newItems[] = new String[(int) (items.length * 1.5)];

	    System.arraycopy(items, 0, newItems, 0, items.length);
	    items = newItems;
	}
	items[nitems++] = item;
    }

    public void addNotify() {
	font = getFont("Helvetica", 14);
    }

    public void setRowsToDisplay(int n) {
	rowsToDisplay = n;
    }

    public Dimension preferredSize() {
	int width = 30;	/* 30 is minimum, let's say */

	for (int i = 0; i < nitems; i++)
	    width = Math.max(width, font.stringWidth(items[i]));

	return new Dimension(width, (font.height + itemGap) * rowsToDisplay);
    }

    public void paint(Graphics g) {
	int y = 0;
	for (int i = 0; i < nitems; i++) {
	    /* do something for items that are selected */
	    g.drawString(items[i], 2, y + font.ascent);
	    y += font.height + itemGap;
	    if (y > height)
		break;
	}
    }

    public void mouseDown(int x, int y) {
	item = locateItem(x, y);

	if (item != -1)
	    selectItem(item);
    }

    public void selectItem(int item) {
	/* code to select item */
	repaint();
    }
}
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post