-
Change image with JList
Im not good with java and it seems that i cant this working so i give this a try..
What i want is to be able to change image when clicked on the JList i treit it with setIcon() but it didnt work 
The
this is the code:
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
public class CardList extends JFrame
implements ListSelectionListener {
public JLabel l = new JLabel();
public String[] creature = {"AnabaShaman", "BalduvianBarbarians",};
////
//private image[] col = {code here to change image?};
////
public JList list = new JList(creature);
public JScrollPane sp = new JScrollPane(list);
public JLabel image2 = new JLabel(new ImageIcon("c:\\image.jpg"));
public CardList() {
Container c = getContentPane();
c.setLayout (new FlowLayout());
c.add(sp);
c.add(l);
c.add(image2);
l.setForeground(Color.blue);
l.setText("Show card");
l.setPreferredSize(new Dimension(100,75));
list.setVisibleRowCount(3);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.addListSelectionListener(this);
pack();
setSize(300,420);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void valueChanged(ListSelectionEvent e) {
// l.setForeground(col[list.getSelectedIndex()]);
}
public static void main (String[] arg) {
CardList CL = new CardList ();
}
}
example:
http://arkserver.servehttp.com/stuff/dev/mtgdev1.jpg
Last edited by Arkie_student; 05-12-2005 at 10:46 AM.
-
you need to setup an ActionListener for the JList so you know when a listed item is selected.
-
 Originally Posted by Phaelax
you need to setup an ActionListener for the JList so you know when a listed item is selected.
thnx, any idea how? ill take a look in the sun doc
-
cant help much, but all i can say is, ooo, anaba shamans own, i love mtg ^_^
-
 Originally Posted by Gaia2000
cant help much, but all i can say is, ooo, anaba shamans own, i love mtg ^_^
haha yeah i play mtg battles on my forums :P
http://arkserver.servehttp.com/forum/viewforum.php?f=48
thnx anyways for the reply
-
oops, i told you the wrong thing, sry.
easiest way probably would be like this:
implement the ListSelectionListener class into your JFrame class then create the following method:
Code:
public void valueChanged(ListSelectionEvent e)
{
String name = e.toString();
if (e == "item 1")
{}
}
list.addListSelectionListener(this);
Take a look at the ListSelectionEvent class to see the different methods you can use to determine which item you clicked.
-
part of code:
public String[] creature = {"AnabaShaman", "BalduvianBarbarians",};
//your code
public void valueChanged(ListSelectionEvent e) {
//String name = e.toString();
if (creature.equals ("BalduvianBarbarians")) {
System.out.println("working");
}
else {
System.out.println("not working");
}
}
//Does the event work with array? because i cant figure out why it doesnt //work.
-
So you wanna use a JList huh ?
Then this might be of interest....
Code:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
public class CardList
extends JFrame
implements ListSelectionListener {
public JLabel l = new JLabel();
public String[] creature = {
"AnabaShaman", "BalduvianBarbarians", };
public MyList list = new MyList(creature);
public JScrollPane sp = new JScrollPane();
public JLabel image2 = new JLabel(new ImageIcon("c:\\image.jpg"));
public CardList() {
jbInit();
}
public void jbInit() {
getContentPane().setLayout(new FlowLayout());
sp.getViewport().add(list);
getContentPane().add(sp);
getContentPane().add(l);
getContentPane().add(image2);
l.setForeground(Color.blue);
l.setText("Show card");
l.setPreferredSize(new Dimension(100, 75));
list.setVisibleRowCount(3);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.addListSelectionListener(this);
pack();
setSize(300, 420);
setVisible(true);
}
public static void main(String[] arg) {
CardList cl = new CardList();
// ensure proper termination
cl.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
cl.setBounds(20,20, 400,300);
cl.setVisible(true);
}
public void valueChanged(ListSelectionEvent e) {
if (e.getSource()!=list) return; // reassurance of the nervous
if (e.getValueIsAdjusting()) return; // avoid "echo"
System.out.println(e.toString());
int ix = list.getSelectedIndex();
String selected = (String) list.getSelectedValue();
System.out.println("selected (1):" + selected);
// many ways to skin the cat...
selected = (String) list.getModel().getElementAt(ix);
System.out.println("selected (2):" + selected);
}
}
/******************************************************
* A well behaved and fancy list
*/
class MyList extends JList {
/**
* Constructors
*/
public MyList () {
setCellRenderer(new FancyStuff());
}
public MyList (Object [] values) {
super(values);
setCellRenderer(new FancyStuff());
}
/**
* Get all values in list as an object array
*/
public Object [] getListArray () {
Object [] o=new Object[this.getModel().getSize()];
for (int i=0; i<this.getModel().getSize(); i++) {
o[i]=this.getModel().getElementAt(i);
}
return o;
}
/**
* Get all values in list as an arrayList
*/
public ArrayList getArrayList() {
ArrayList aList=new ArrayList();
for (int i=0; i<this.getModel().getSize(); i++) {
aList.add((String)this.getModel().getElementAt(i));
}
return aList;
}
/**
* Single element removal
*/
public void removeSelection() {
int ix=this.getSelectedIndex();
if (ix < 0) return;
ArrayList oldList=getArrayList();
ArrayList newList=new ArrayList();
for (int i=0; i<oldList.size(); i++) {
if (i==ix) continue;
newList.add(oldList.get(i));
}
}
/**
* Single element add
* @param element
*/
public void addElement (Object element) {
ArrayList oldList=getArrayList();
oldList.add(element);
this.setListData(oldList.toArray());
}
}
/*******************************************************
* Color and font for a JList
*/
class FancyStuff implements ListCellRenderer {
Font f=new Font("Times New Roman",Font.BOLD,17);
private JLabel theLabel=new JLabel();
public Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
theLabel.setOpaque(true);
theLabel.setFont(f);
String displayVal=(String) value;
if (displayVal.startsWith("B")) {
theLabel.setBackground(Color.green);
} else if (displayVal.startsWith("A")) {
theLabel.setBackground(Color.cyan);
} else {
theLabel.setBackground(Color.yellow);
}
if (isSelected || cellHasFocus) {
theLabel.setForeground(Color.magenta);
} else {
theLabel.setForeground(Color.black);
}
theLabel.setText(displayVal);
return theLabel;
}
}
eschew obfuscation
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|