Archive for the 'swing' Category

16
Sep
11

useful swing border

This could be useful to have a graphical view of how much space a component is taking on the screen.


class JComponent
  def create_border
    setBorder(BorderFactory.createLineBorder(Color.black))
  end
end

11
Jun
09

how to display any type of Object in a JList


If you’re like me, and you have used JList before, you used it with objects of the String class. What do we do if we want to display other type of objects ?

I had to display some File objects in the list, and I didn’t want to resort to adding a String to the model, because I really wanted to be able to pass the File object back & forth in my code. A bit of googling revealed that the solution was to use a custom ListCellRenderer. If you open their page, you’ll notice they are showing an example there.

I wrote a class I’m using in my project. Here’s the code :


import java.awt.*;
import javax.swing.*;

public abstract class AbstractedCellRenderer extends JLabel implements ListCellRenderer {

    protected abstract String getPaintedStringFrom(Object value);
    private Color selectedForegroundColor,selectedBackgroundColor;

    public AbstractedCellRenderer()
    {
        selectedForegroundColor = Color.BLUE;
        selectedBackgroundColor = Color.RED;
        unselectedBackgroundColor = Color.WHITE;
        unselectedForegroundColor = Color.BLACK;
    }

    public Color getSelectedBackgroundColor() {
        return selectedBackgroundColor;
    }

    public void setSelectedBackgroundColor(Color selectedBackgroundColor) {
        this.selectedBackgroundColor = selectedBackgroundColor;
    }

    public Color getSelectedForegroundColor() {
        return selectedForegroundColor;
    }

    public void setSelectedForegroundColor(Color selectedForegroundColor) {
        this.selectedForegroundColor = selectedForegroundColor;
    }

    public Color getUnselectedBackgroundColor() {
        return unselectedBackgroundColor;
    }

    public void setUnselectedBackgroundColor(Color unselectedBackgroundColor) {
        this.unselectedBackgroundColor = unselectedBackgroundColor;
    }

    public Color getUnselectedForegroundColor() {
        return unselectedForegroundColor;
    }

    public void setUnselectedForegroundColor(Color unselectedForegroundColor) {
        this.unselectedForegroundColor = unselectedForegroundColor;
    }
    private Color unselectedForegroundColor,unselectedBackgroundColor;

    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        setText(getPaintedStringFrom(value));
        if(isSelected)
        {
            setForeground(selectedForegroundColor);
            setBackground(selectedBackgroundColor);
        }
        else
        {
            setForeground(unselectedForegroundColor);
            setBackground(unselectedBackgroundColor);
        }
        return this;
    }

}

To use it, extend it just like in the following example:



import java.awt.*;
import java.io.File;
import javax.swing.*;

public class FileObjectCellRenderer extends AbstractedCellRenderer
{

    @Override
    protected String getPaintedStringFrom(Object value) {
        File file = (File)value;
        return file.getName();
    }

}

The thing is, I’m displaying a String, by overriding the getPaintedStringFrom, but this way, we get to choose where that String comes from. You could show an image as well, by setting an ImageIcon on the JLabel.

Hope this helps you as well!

12
Feb
09

small Java tip


Did you know that each JComponent has a method called putClientProperty with the following signature :

void putClientProperty(Object key,Object value)

This allows you to add your own objects to the JComponent , which is helpfull sometimes , because you don’t need to create another class , when all you want is to add some variables to your buttons/labels/etc.




Blog Stats

  • 218,505 hits