Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

AWT Frame refresh? A CHALLENGE

161 views
Skip to first unread message

Mark Kromski

unread,
Apr 9, 1998, 3:00:00 AM4/9/98
to

Can somebody give me a quick example of:

A window with 1 button,
when the button is pressed it changes to a Choice component.


I just need to know how you can change window component layouts in an
running program.

Please help me.

--
___________
| _______ | __
| |Mark | | /__\_
| |Kromski| | . . \
| |_______| | . . . \
|________.._| . . . . \
/_______\ . . . ._ )
___|________o|_mailto:krom...@se.bel.alcatel.be __/___\__
(__________________________________________________________)
||Department VE141: S12 Design and Development Tools||
||F. Wellesplein 1, B-2018 Antwerpen,Belgium ||
||phone 32/3/2407124 ||


Serge Liberloo

unread,
Apr 9, 1998, 3:00:00 AM4/9/98
to

If you press the button.
-> Remove the button
-> Add a Choice at the same location

Tov Are Jacobsen

unread,
Apr 9, 1998, 3:00:00 AM4/9/98
to

The easiest way is java.awt.CardLayout.
There is also a programming example shipped with JDK 1.1.

Another way to do it is to remove the component from the layout, then
add a new component, then validate the layout. In my experience this
is very slow, and gives the UI a non-reassuring feel to it.

I did this once in a text editor I made, where I wanted to avoid
pop-up dialogs, and "melt" the information requests into the
user-interface as a whole. It wasn't as smooth as I would have hoped,
but the concept is a good one I think. Dialogs and Frames are a lousy
invention.

if(((CheckboxMenuItem) o).getState()){
add("North",scont);
validate();
}else{
remove(scont);
validate();
}

__________________________________________
Tov Are Jacobsen

barbre

unread,
Apr 9, 1998, 3:00:00 AM4/9/98
to

Mark Kromski wrote in message <352CD7F1...@se.bel.alcatel.be>...


>Can somebody give me a quick example of:
>
>A window with 1 button,
>when the button is pressed it changes to a Choice component.
>
>
>I just need to know how you can change window component layouts in an
>running program.
>
>Please help me.

Is this what you are looking for???

import java.awt.*;
import java.awt.event.*;


public class B2C extends Frame implements ActionListener {
Component c;

public B2C(String title) {
super(title);
c = add(new Button("Button"));
((Button) c).addActionListener(this);
pack();
}


public void actionPerformed(ActionEvent e) {
remove(c);
c = new Choice();
add(c);
doLayout();
}

public static void main(String[] args) {
new B2C("B2C").setVisible(true);
}
}

--Mike Barbre
bar...@stlnet.com


Toshihiro Horie

unread,
Apr 13, 1998, 3:00:00 AM4/13/98
to Mark Kromski

> I just need to know how you can change ... component[s]... in a
> running program.
With JDK 1.02, it does not seem to work properly with a GridBagLayout,
but it does work will a custom null layout. An example of a working
applet that does this is my Gibbs applet at
http://mc2.cchem.berkeley.edu/Java/Gibbs/Gibbs.html. Press the
[Quantitative] button, and the meter panel with switch to a panel with
textboxes, without reiniting the applet.

1. remove the component from the layout
2. add a new component and its subcomponents.
3. validate, show, and repaint the new layout/components.
Here is some source code I wrote.
//Copyright (c) 1997-1998 Regents of the University of California,
Berkeley.
public boolean handleEvent(Event evt)
{
//System.out.println("EVENT: at "+evt.target.toString()+"
id="+evt.id);
boolean ret;
String t1str=new String();
String t2str=new String();
float DSold;
float DHold;
if ((evt.id==Event.ACTION_EVENT)&&
(evt.target==bQualitative)&&
(QUANT==true))
{ //MAGIC! replacement of a removed component with another
repaint();
QUANT=false;
bQuantitative.enable(); // only allow switch into opposite state
bQualitative.disable();
p2.hide();
p2.removeAll();
p2.setLayout(new GridLayout(1,1));
p2.reshape(xmax,0,appletxmax-xmax,ymid);
p2.add(p4); // put MeterPanel in p2
p4.validate();
p4.show();
p2.validate();
p2.show();
p4.drawBackground();
repaint();

} else if ((evt.id==Event.ACTION_EVENT)&&
(evt.target==bQuantitative)&&
(QUANT==false)) {
QUANT=true;
bQuantitative.disable(); // only allow switch into opposite state
bQualitative.enable();
p4.copyBack(x0,y0,x2,y2,noRecalcDeltas,Gscale,Tscale, DS,DH);
p2.removeAll();
p2_addComponents();
repaint();

0 new messages