aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Rotating an imageicon in JTable cells Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Rotating an imageicon in JTable cells" Watch "Rotating an imageicon in JTable cells" New topic
Author

Rotating an imageicon in JTable cells

tan kian
Ranch Hand

Joined: Apr 24, 2006
Posts: 30
i tried putting together something from what i found on the net, but i have an error in drawImage. What should i put for the parameters Image? i cant cast my imageicon to an Image nor Graphics. please point out if there are any other parts which i did not do/did wrongly. thanks.

Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Using an ImageIcon here isn't helping you that much: it seems to be used as an easy way to read from a jpeg file, but ImageIO.read is just as easy to use, and returns a BufferedImage, to boot.

That being said, have you studied ImageIcon's API to see if there are any useful methods that would enable you to successfully call drawImage?
[ May 02, 2006: Message edited by: Jeff Albertson ]

There is no emoticon for what I am feeling!
tan kian
Ranch Hand

Joined: Apr 24, 2006
Posts: 30
ok i solved it already, thks for the reply anyway =) i have another question now though. i have some JComboBoxes, and i need to make it so that when the value in the combobox is changed, the image in the cells will need to be rotated as well according to the value in the combobox. i tried doing this but well nothing happened.

public void init()
{
final Mycellrender mcr = new Mycellrender();
JComboBox l = new JComboBox();
l.addItem("a");
l.addItem("b");
l.addItem("c");
l.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if (e.toString() == "a")
{
mcr.setL(1);
}
else if (e.toString() == "b")
{
mcr.setL(2);
}
else if (e.toString() == "c")
{
mcr.setL(3);
}
}
}

class Mycellrender extends DefaultTableCellRenderer
{
Random rand = new Random(255);
int choosel;

public Component getTableCellRendererComponent(JTable jt,Object value,boolean isselected, boolean hasfocus,int row,int col)
{
super.getTableCellRendererComponent(jt,value,isselected,hasfocus,row,col);

//if a cell is selected
if (blah blah blah)
{
setBackground(Color.GRAY);
}
else
{
if (rand.nextInt(100) < 25 || choosel == 1)
{
//do stuff like rotating an image
}
else
//do other stuff
}

return this;
}

public void setL(int index)
{
choosel = index;
}
}
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
First, a couple suggestions:

1. When asking a new and potentially unrelated question, it's a good idea to start a new thread.
2. Use code tags for your code.

Back to your code: here's a hint. When trying to understand what's wrong with code, break it into smaller pieces. Your code have two parts: one half is listening to a combo box, and the other half is doing something in a table renderer. This two halfs are quite independent. Test each in isolation. For example, your item listener is doing a case analysis on e.toString() -- why not print this string out to see if it is what you are expecting it to be:

System.out.println(e.toString());
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
There are a number of ways of doing this. The idea in this one is to make up an
ImageIcon for each angle in the JComboBox.
tan kian
Ranch Hand

Joined: Apr 24, 2006
Posts: 30
thks, i will try it out and come back with any questions.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Rotating an imageicon in JTable cells