• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Image in List

 
Anchit Jindal
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags. You can edit your post to add them.
 
Rob Camick
Ranch Hand
Posts: 2565
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags - again. I'm not going to read that code like this.
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread hijack alert. Not even the same problem too.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic