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(isEnabled() ? _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.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
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.Observable;
import java.util.Observer;
import java.util.Vector;
public class RankedView extends Panel
implements Observer
{
class _cls6 extends MouseAdapter
{
public void mousePressed(MouseEvent me)
{
rankDown(10);
}
_cls6()
{
}
}
class _cls5 extends MouseAdapter
{
public void mousePressed(MouseEvent me)
{
rankDown(1);
}
_cls5()
{
}
}
class _cls4 extends MouseAdapter
{
public void mousePressed(MouseEvent me)
{
rankUp(1);
}
_cls4()
{
}
}
class _cls3 extends MouseAdapter
{
public void mousePressed(MouseEvent me)
{
rankUp(10);
}
_cls3()
{
}
}
class _cls2
implements ItemListener
{
public void itemStateChanged(ItemEvent ie)
{
setButtons();
}
_cls2()
{
}
}
class _cls1 extends MouseAdapter
{
public void mousePressed(MouseEvent me)
{
unrank();
}
_cls1()
{
}
}
public RankedView(RequestModel m, Image images[])
{
_model = m;
GridBagLayout gbm = new GridBagLayout();
setLayout(gbm);
iLabel l1 = new iLabel(images[10]);
GridBagConstraints gbc1 = new GridBagConstraints();
gbc1.gridx = 0;
gbc1.gridy = 0;
gbc1.insets = new Insets(10, 42, 0, 0);
gbc1.anchor = 17;
gbm.setConstraints(l1, gbc1);
add(l1);
_heading = new Label("Rank Rating ID Status
Name");
GridBagConstraints gbc2 = new GridBagConstraints();
gbc2.gridx = 0;
gbc2.gridy = 1;
gbc2.insets = new Insets(0, 45, 0, 0);
gbc2.anchor = 17;
gbm.setConstraints(_heading, gbc2);
add(_heading);
Panel ranker = new Panel();
((FlowLayout)ranker.getLayout()).setVgap(0);
_unrankButton = new iButton(images[0]);
_unrankButton.addMouseListener(new _cls1());
ranker.add(_unrankButton);
_rankList = new SizeList(15, false);
_rankList.setSize(300, 275);
_rankList.addItemListener(new _cls2());
ranker.add(_rankList);
Panel updown = new Panel(new GridLayout(4, 1));
_bigUpButton = new iButton(images[1]);
_bigUpButton.addMouseListener(new _cls3());
updown.add(_bigUpButton);
_upButton = new iButton(images[2]);
_upButton.addMouseListener(new _cls4());
updown.add(_upButton);
_downButton = new iButton(images[3]);
_downButton.addMouseListener(new _cls5());
updown.add(_downButton);
_bigDownButton = new iButton(images[4]);
_bigDownButton.addMouseListener(new _cls6());
updown.add(_bigDownButton);
ranker.add(updown);
GridBagConstraints gbc3 = new GridBagConstraints();
gbc3.gridx = 0;
gbc3.gridy = 2;
gbc3.anchor = 10;
gbm.setConstraints(ranker, gbc3);
add(ranker);
}
public void setHeadingColour(Color c)
{
_heading.setForeground(c);
}
public void setHeadingFont(Font f)
{
_heading.setFont(f);
}
public void update(Observable obs, Object o)
{
RequestRow row = null;
Vector rlist = _model.getRanked();
int selected = _rankList.getSelectedIndex();
if(rlist.size() > _rankList.getItemCount())
selected = _rankList.getItemCount();
_rankList.removeAll();
for(int n = 0; n < rlist.size(); n++)
{
row = (RequestRow)rlist.elementAt(n);
_rankList.add(displayRow(row));
}
selected = Math.min(selected, _rankList.getItemCount() - 1);
_rankList.select(selected);
setButtons();
}
public void save()
{
_model.save();
}
public String displayRow(RequestRow 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.getItem(index).substring(16);
return name;
}
public String getSelectedName()
{
String name = null;
int index = _rankList.getSelectedIndex();
return getName(index);
}
public void unrank()
{
String name = getSelectedName();
if(name != null)
_model.setUnranked(name);
}
public void rankUp(int times)
{
int index = _rankList.getSelectedIndex();
for(int n = 0; n < times; n++)
if(index > 0 && index < _rankList.getItemCount())
{
String name = getSelectedName();
String above = getName(index - 1);
_model.swap(name, above);
index--;
}
_rankList.select(index);
_model.notifyObservers();
}
public void rankDown(int times)
{
int index = _rankList.getSelectedIndex();
for(int n = 0; n < times; n++)
if(index >= 0 && index < _rankList.getItemCount() - 1)
{
String name = getSelectedName();
String below = getName(index + 1);
_model.swap(name, below);
index++;
}
_rankList.select(index);
_model.notifyObservers();
}
protected void setButtons()
{
int count = _rankList.getItemCount();
int selected = _rankList.getSelectedIndex();
if(count > 0 && selected >= 0)
{
_unrankButton.setEnabled(true);
_bigUpButton.setEnabled(selected != 0);
_upButton.setEnabled(selected != 0);
_bigDownButton.setEnabled(selected != count - 1);
_downButton.setEnabled(selected != count - 1);
} else
{
_unrankButton.setEnabled(false);
_bigUpButton.setEnabled(false);
_upButton.setEnabled(false);
_bigDownButton.setEnabled(false);
_downButton.setEnabled(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.FilteredImageSource;
public class iButton extends Canvas
{
class _cls2 extends MouseAdapter
{
public void mousePressed(MouseEvent me)
{
_image = _pressed;
repaint();
}
public void mouseReleased(MouseEvent me)
{
_image = _normal;
repaint();
}
_cls2()
{
}
}
class _cls1 extends MouseAdapter
{
public void mouseEntered(MouseEvent me)
{
setCursor(Cursor.getPredefinedCursor(12));
}
public void mouseExited(MouseEvent me)
{
setCursor(Cursor.getDefaultCursor());
}
_cls1()
{
}
}
public iButton(Image image)
{
_normal = image;
_image = _normal;
_pressed = null;
_disabled = createImage(new
FilteredImageSource(_normal.getSource(), new
FadeFilter(Color.white)));
addMouseListener(new _cls1());
}
public iButton(Image image, Image pressed)
{
this(image);
if(pressed != null)
{
_pressed = pressed;
addMouseListener(new _cls2());
}
}
public Dimension getMinimumSize()
{
return new Dimension(_image.getWidth(this) + 2,
_image.getHeight(this) + 2);
}
public Dimension getPreferredSize()
{
return getMinimumSize();
}
public void paint(Graphics g)
{
g.drawImage(isEnabled() ? _image : _disabled, 1, 1, this);
}
private Image _image;
private Image _normal;
private Image _pressed;
private Image _disabled;
}