Hi All,
I am a relative newbie to Java / Applets, but am despirately after
some help !
I have got the following code, which is basically a listing with
button items along the sides, allowing the items in the list to be
moved up/down etc.
Obviously, for the top and bottom items, the appropriate move up /
move down buttons should be disabled.
The bit I am interested in is setButtons() method, which when
conditions are true, sets the _unrankedButton enabled property to
true, which in turn calls the code I have written for iButton for
which _unrankedButton has been declared as.
This then performs the appropriate tasks, and repaints the screen to
show any button updates in the void paint method.
Unfortunately, when I added some debug into the code, it is clear to
see that although Netscape 4.79 works fine, calling the setButtons
method, which in turn initiated the line :
g.drawImage(isE nabled() ? _image : _disabled, 1, 1, this);
which updates and repainting the buttons, but I cannot get this to
work in IE 6 or Netscape 7.1 both browsers I need to cater for ! The
logic behind the button is changed to disable / enable it, but the
buttons are bot repainted.
Is this something glaringly obvious that I am missing, or is there a
reason why buttons are not getting repainted browsers other than
Netscape 4.79 ?
If anyone can help me out or offer any insight into what I am doing
wrong, or how I can fix this, I would be really grateful !!!
Thanks in advance, and apologise in advance if my terminology is not
the best !
Martin
P.S. Sorry for the length of this post. I was unsure how much info. /
code would be useful
import java.awt.Color;
import java.awt.Compon ent;
import java.awt.Contai ner;
import java.awt.FlowLa yout;
import java.awt.Font;
import java.awt.GridBa gConstraints;
import java.awt.GridBa gLayout;
import java.awt.GridLa yout;
import java.awt.Image;
import java.awt.Insets ;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event. ItemEvent;
import java.awt.event. ItemListener;
import java.awt.event. MouseAdapter;
import java.awt.event. MouseEvent;
import java.util.Obser vable;
import java.util.Obser ver;
import java.util.Vecto r;
public class RankedView extends Panel
implements Observer
{
class _cls6 extends MouseAdapter
{
public void mousePressed(Mo useEvent me)
{
rankDown(10);
}
_cls6()
{
}
}
class _cls5 extends MouseAdapter
{
public void mousePressed(Mo useEvent me)
{
rankDown(1);
}
_cls5()
{
}
}
class _cls4 extends MouseAdapter
{
public void mousePressed(Mo useEvent me)
{
rankUp(1);
}
_cls4()
{
}
}
class _cls3 extends MouseAdapter
{
public void mousePressed(Mo useEvent me)
{
rankUp(10);
}
_cls3()
{
}
}
class _cls2
implements ItemListener
{
public void itemStateChange d(ItemEvent ie)
{
setButtons();
}
_cls2()
{
}
}
class _cls1 extends MouseAdapter
{
public void mousePressed(Mo useEvent me)
{
unrank();
}
_cls1()
{
}
}
public RankedView(Requ estModel m, Image images[])
{
_model = m;
GridBagLayout gbm = new GridBagLayout() ;
setLayout(gbm);
iLabel l1 = new iLabel(images[10]);
GridBagConstrai nts gbc1 = new GridBagConstrai nts();
gbc1.gridx = 0;
gbc1.gridy = 0;
gbc1.insets = new Insets(10, 42, 0, 0);
gbc1.anchor = 17;
gbm.setConstrai nts(l1, gbc1);
add(l1);
_heading = new Label("Rank Rating ID Status
Name");
GridBagConstrai nts gbc2 = new GridBagConstrai nts();
gbc2.gridx = 0;
gbc2.gridy = 1;
gbc2.insets = new Insets(0, 45, 0, 0);
gbc2.anchor = 17;
gbm.setConstrai nts(_heading, gbc2);
add(_heading);
Panel ranker = new Panel();
((FlowLayout)ra nker.getLayout( )).setVgap(0);
_unrankButton = new iButton(images[0]);
_unrankButton.a ddMouseListener (new _cls1());
ranker.add(_unr ankButton);
_rankList = new SizeList(15, false);
_rankList.setSi ze(300, 275);
_rankList.addIt emListener(new _cls2());
ranker.add(_ran kList);
Panel updown = new Panel(new GridLayout(4, 1));
_bigUpButton = new iButton(images[1]);
_bigUpButton.ad dMouseListener( new _cls3());
updown.add(_big UpButton);
_upButton = new iButton(images[2]);
_upButton.addMo useListener(new _cls4());
updown.add(_upB utton);
_downButton = new iButton(images[3]);
_downButton.add MouseListener(n ew _cls5());
updown.add(_dow nButton);
_bigDownButton = new iButton(images[4]);
_bigDownButton. addMouseListene r(new _cls6());
updown.add(_big DownButton);
ranker.add(updo wn);
GridBagConstrai nts gbc3 = new GridBagConstrai nts();
gbc3.gridx = 0;
gbc3.gridy = 2;
gbc3.anchor = 10;
gbm.setConstrai nts(ranker, gbc3);
add(ranker);
}
public void setHeadingColou r(Color c)
{
_heading.setFor eground(c);
}
public void setHeadingFont( Font f)
{
_heading.setFon t(f);
}
public void update(Observab le obs, Object o)
{
RequestRow row = null;
Vector rlist = _model.getRanke d();
int selected = _rankList.getSe lectedIndex();
if(rlist.size() > _rankList.getIt emCount())
selected = _rankList.getIt emCount();
_rankList.remov eAll();
for(int n = 0; n < rlist.size(); n++)
{
row = (RequestRow)rli st.elementAt(n) ;
_rankList.add(d isplayRow(row)) ;
}
selected = Math.min(select ed, _rankList.getIt emCount() - 1);
_rankList.selec t(selected);
setButtons();
}
public void save()
{
_model.save();
}
public String displayRow(Requ estRow row)
{
String spaces = " ";
String gap = " ";
String id = null;
String rank = null;
rank = spaces + row.getRanking( );
rank = rank.substring( rank.length() - 2);
id = spaces + row.getId();
id = id.substring(id .length() - 6);
return rank + gap + row.getRating() + gap + id + gap +
row.getStatus() + gap + row.getName();
}
public String getName(int index)
{
String name = null;
if(index >= 0)
name = _rankList.getIt em(index).subst ring(16);
return name;
}
public String getSelectedName ()
{
String name = null;
int index = _rankList.getSe lectedIndex();
return getName(index);
}
public void unrank()
{
String name = getSelectedName ();
if(name != null)
_model.setUnran ked(name);
}
public void rankUp(int times)
{
int index = _rankList.getSe lectedIndex();
for(int n = 0; n < times; n++)
if(index > 0 && index < _rankList.getIt emCount())
{
String name = getSelectedName ();
String above = getName(index - 1);
_model.swap(nam e, above);
index--;
}
_rankList.selec t(index);
_model.notifyOb servers();
}
public void rankDown(int times)
{
int index = _rankList.getSe lectedIndex();
for(int n = 0; n < times; n++)
if(index >= 0 && index < _rankList.getIt emCount() - 1)
{
String name = getSelectedName ();
String below = getName(index + 1);
_model.swap(nam e, below);
index++;
}
_rankList.selec t(index);
_model.notifyOb servers();
}
protected void setButtons()
{
int count = _rankList.getIt emCount();
int selected = _rankList.getSe lectedIndex();
if(count > 0 && selected >= 0)
{
_unrankButton.s etEnabled(true) ;
_bigUpButton.se tEnabled(select ed != 0);
_upButton.setEn abled(selected != 0);
_bigDownButton. setEnabled(sele cted != count - 1);
_downButton.set Enabled(selecte d != count - 1);
} else
{
_unrankButton.s etEnabled(false );
_bigUpButton.se tEnabled(false) ;
_upButton.setEn abled(false);
_bigDownButton. setEnabled(fals e);
_downButton.set Enabled(false);
}
}
protected RequestModel _model;
protected SizeList _rankList;
protected iButton _unrankButton;
protected iButton _bigUpButton;
protected iButton _upButton;
protected iButton _bigDownButton;
protected iButton _downButton;
protected Label _heading;
}
*************** *************** *************** *************** ********
import java.awt.*;
import java.awt.event. MouseAdapter;
import java.awt.event. MouseEvent;
import java.awt.image. FilteredImageSo urce;
public class iButton extends Canvas
{
class _cls2 extends MouseAdapter
{
public void mousePressed(Mo useEvent me)
{
_image = _pressed;
repaint();
}
public void mouseReleased(M ouseEvent me)
{
_image = _normal;
repaint();
}
_cls2()
{
}
}
class _cls1 extends MouseAdapter
{
public void mouseEntered(Mo useEvent me)
{
setCursor(Curso r.getPredefined Cursor(12));
}
public void mouseExited(Mou seEvent me)
{
setCursor(Curso r.getDefaultCur sor());
}
_cls1()
{
}
}
public iButton(Image image)
{
_normal = image;
_image = _normal;
_pressed = null;
_disabled = createImage(new
FilteredImageSo urce(_normal.ge tSource(), new
FadeFilter(Colo r.white)));
addMouseListene r(new _cls1());
}
public iButton(Image image, Image pressed)
{
this(image);
if(pressed != null)
{
_pressed = pressed;
addMouseListene r(new _cls2());
}
}
public Dimension getMinimumSize( )
{
return new Dimension(_imag e.getWidth(this ) + 2,
_image.getHeigh t(this) + 2);
}
public Dimension getPreferredSiz e()
{
return getMinimumSize( );
}
public void paint(Graphics g)
{
g.drawImage(isE nabled() ? _image : _disabled, 1, 1, this);
}
private Image _image;
private Image _normal;
private Image _pressed;
private Image _disabled;
}