GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes Image in List Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Image in List" Watch "Image in List" New topic
Author

Image in List

Anchit Jindal
Greenhorn

Joined: Feb 03, 2010
Posts: 21
I am making a chat server. In this I want to implement functionality so that online user is marked by Green dot, Busy by Red and Offline by Black. I have drawn these dots and saved it in file. But I am not able to implement this. I have tried the following code but it is printing something unexpected at the frame. Please help me.

Thanks a lot...

import javax.swing.*;
import java.awt.*;
class LoginS extends LoginGui
{
JList list;
DefaultListModel dlm;
JPanel p1,p2,p3;
public static void main(String[] args)
{
LoginS ls=new LoginS();
ls.drawGui();
}

/*class MyCellRenderer implements ListCellRenderer
{
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
Component com=(Component)value;
return com;
}
}*/
}
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Please UseCodeTags. You can edit your post to add them.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2191
    
    7
Maybe use a JTable. You can add the image in the first column and the user in the second column. That way you don't need to create a custom renderer.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

What happens if you remove the comment start on line 32, and actually execute it? If I do (and modify the code slightly to compile), add some icons in the same folder with the given names and run the code I see the labels with images.

You may want to replace JPanels p1 to p3 with direct JLabels:
This will make sure that the icons and text are placed on the left, not the center, and that there is no gray background. Here's the new code:
Rite Sara
Ranch Hand

Joined: Feb 13, 2010
Posts: 56
Hi Rob,
I created following inner class :
class MyCellRenderer implements TableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus,int row,int column){
Component com = (Component)value;
return com;
}
}

but how to bind it with table ? as there is no setCellRenderer() method or getCellRenderer() method .. I am attaching sample code given by you. I am trying to run on this code first before applying to my code. Could you please have a look ?

import java.awt.*;

import javax.swing.*;
import javax.swing.table.*;

public class TableIcon extends JFrame
{
public TableIcon()
{
ImageIcon aboutIcon = new ImageIcon("C:\\Documents and Settings\\saher\\Desktop\\netbeans_logo.gif");

String[] columnNames = {"Picture", "Description"};
Object[][] data =
{
{aboutIcon + "test string", "About"},
};

DefaultTableModel model = new DefaultTableModel(data, columnNames);
JTable table = new JTable( model )
{
// Returning the Class of each column will allow different
// renderers to be used based on Class
public Class getColumnClass(int column)
{
return getValueAt(0, column).getClass();
}
};


table.setPreferredScrollableViewportSize(table.getPreferredSize());

JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
}

class MyCellRenderer implements TableCellRenderer
{
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column)
{
Component com = (Component)value;
return com;
}

}

public static void main(String[] args)
{
TableIcon frame = new TableIcon();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}

}



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Please UseCodeTags - again. I'm not going to read that code like this.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4571
    
    5

Thread hijack alert. Not even the same problem too.


luck, db
There are no new questions, but there may be new answers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Ah right. I didn't notice it was a different user

Sarah, you've already created your own thread. Next time don't hijack someone else's thread like this.
 
GeeCON Prague 2014
 
subject: Image in List