I am writing a custom jlist cell renderer for some kind of filechooser. My problem is that when I read my ImageIcon, it seems it has the dimension (-1,-1), so I can't resize it properly. The picture is a simple texture (wood, metal, etc.).
Then I thought that if I added a JPanel instead of an image, and then adding the image to the panel, I wouldnt even have to resize the picture.
I have 2 possibilities:
Read the ImageIcon from the hard drive so that they dont have dimension -1,-1
Insert a JPanel inside the JLabel.
Here is a preview of my list cells.
Here is my custom renderer, which adds icons to the cells.
class IconListRenderer extends DefaultListCellRenderer {
private Map<Object, Icon> icons = null;
public IconListRenderer(Map<Object, Icon> icons) {
this.icons = icons;
}
#Override
public Component getListCellRendererComponent(
JList list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
// Get the renderer component from parent class
JLabel label =
(JLabel) super.getListCellRendererComponent(list,
value, index, isSelected, cellHasFocus);
ImageIcon icon = (ImageIcon)icons.get(value);
// Set icon to display for value
label.setIcon(icon);
label.setText(value.toString());
return label;
}
}
Just replace the label with the panel.
You can use a JPanel as the rendercomponent instead of the JLabel.
Related
I have this code and MyComboBoxRenderer() seems to not work with it. It has an error in the line with comment written below.
This code is made for autosuggest. So it shows suggestion in a combobox while user types on the textfield.
public test2() {
initComponents();
jComboBox1.setRenderer(new MyComboBoxRenderer1());
jComboBox1.setBackground(new Color(0,0,0,0));
final JTextField textfield = (JTextField) jComboBox1.getEditor().getEditorComponent(); //it has error in this line
textfield.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent ke) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
comboFilter(textfield.getText());
}
});
}
});
}
Maybe it has something to do with the textfield. My problem is that I wanted to edit the appearance or design of combobox. I want it to inherit the background of the frame. Like transparent. Example are in the pictures.
Here are the pictures. Please click the links below to see it.
It should be something like this
Rather than this one. This is the output of the codes above.
And here is the code I have in my combobox renderer.
public MyComboBoxRenderer1(){
setOpaque(true);
setFont(new Font ("Segoe UI Semibold", Font.PLAIN ,14));
setForeground(Color.WHITE);
}
#Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
setText(value.toString());
if (isSelected)
{
setBackground(Color.WHITE);
setForeground(Color.BLACK);
}
else {
setBackground(Color.GRAY);
setForeground(Color.WHITE);
}
return this;
}
}
Why is it that the renderer doesn't work with this? And what should I do to make it work? Can anyone help me please? Thank you in advance. :)
EDITED...
I've already set the background transparent. I just need to declare the background of the texfield. XD Yey.
textfield.setBackground(new Color(0,0,0,0));
textfield.setForeground(new Color(255,255,255));
But it left small portion that is still not transparent.
I tried doing an additional comboBox on my frame. But it is without the textfield. And it works just fine!
The upper is the comboBox with textfield, the one I have problem with. The lower is the one w/o textfield, I just tried if the code will work with a normal comboBox. I need to make it look like the lower one.
jComboBox1.setRenderer(new MyComboBoxRenderer1());
jComboBox1.setBackground(new Color(0,0,0,0));
jComboBox2.setRenderer(new MyComboBoxRenderer1());
jComboBox2.setBackground(new Color(0,0,0,0));
It has the same code. But it doesn't work with the other one. Maybe it's because of the textfield again?? :(((
jComboBox1.setBackground(new Color(0,0,0,0));
Don't try to use transparent colors. Swing does not know how to paint transparent colors properly. See: Background With Transparency for more information.
Instead you change the opaqueness of the component:
component.setOpaque( false );
In the case of the combo box you need to worry about the component and the renderer, so you might use:
comboBox.setOpaque(false);
((JLabel)comboBox.getRenderer()).setOpaque(false);
However, this will now cause a problem with the dropdown list. Since the rendering is now transparent you won't see the row selections.
Haven't tested this but a possible solution if to alter the opaqueness of the renderer based on the item being rendered.
The code might be:
public Component getListCellRendererComponent(
JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
setOpaque(index == -1 ? false : true);
// add custom painting here
return this;
}
The -1 indicates the item in the combo box is being rendered as oppose to an item in the list.
I am stuck with setting a tooltip to one of my JPanel added to the node in a JTree. This question could be similar to JTree node's changable tooltip but not entirely.
I am also using JTree populated with some (custom) nodes. Each node contains a checkbox, a color box (JPanel) and node path. I am implementing TreeCellRenderer. I have not posted below code for what is being added to node as I think it is not necessary.
Below is part of my code:
public class CheckTreeCellRenderer extends JPanel implements TreeCellRenderer {
private static final long serialVersionUID = 4025435851260573240L;
CheckTreeSelectionModel selectionModel;
private TreeCellRenderer delegate;
TristateCheckBox checkBox = new TristateCheckBox();
JPanel panel = new JPanel();
public CheckTreeCellRenderer(TreeCellRenderer delegate, CheckTreeSelectionModel selectionModel){
this.delegate = delegate;
this.selectionModel = selectionModel;
setLayout(new BorderLayout());
setOpaque(false);
checkBox.setOpaque(false);
}
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus){
Component renderer = delegate.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
panel.setToolTipText("Hello");
removeAll();
add(checkBox, BorderLayout.WEST);
add(panel, BorderLayout.CENTER);
add(renderer, BorderLayout.EAST);
return this;
}
}
How to set a tooltip for JPanel added to a node?
Have a look at the docs of JTree.getToolTipText:
NOTE: For JTree to properly display tooltips of its renderers, JTree must be a registered component with the ToolTipManager. This can be done by invoking ToolTipManager.sharedInstance().registerComponent(tree). This is not done automatically!
This will fix it.
Good morning, please,could you mind helping me in determining why this ListCellRenderer class not setting the image icon at combobox cells:
here's the ListCellRenderer class:
class MyComboRendere implements ListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = new JLabel();
label.setOpaque(true);
label.setText(value.toString());
label.setIcon(new ImageIcon("/pics/Color-icon.png"));
if (isSelected)
if (index == 0)
label.setBackground(Color.RED);
else if (index == 1)
label.setBackground(Color.GREEN);
else
label.setBackground(Color.BLUE);
return label;
}
}
and this is a method to setup the combobox:
public void setComboColor(){
Vector<String> colors=new Vector<>();
comboPanel=new JPanel(new BorderLayout());
colors.add("RED");
colors.add("GREEN");
colors.add("BLUE");
colorCombo=new JComboBox(colors);
colorCombo.setRenderer(new MyComboRendere());
comboPanel.add(colorCombo,BorderLayout.BEFORE_FIRST_LINE);
}
It seems that label.setIcon(new ImageIcon("/pics/Color-icon.png")); doesn't get the actual path of the icon as it always returns null, but it doesn't throw an exception. So I tried to use this:
java.net.URL imgURL = getClass().getResource("/pics/Color-icon.png");
label.setIcon(icon);
and it works properly
"/pics/Color-icon.png"
Does this exist? ImageIcon won't throw any exceptions if it fails to load the image, but will return null.
don't provide FileIO inside XxxRenderer, load all Icons to local variable, test for null value
XxxRenderer firing a lots of event (mouse, keys and internally implemented in API), then you recreated Icon on fly
read Oracle tutorial about JComboBox, try code example about similair issue
I am using this JTable method to create a Cell with a JComboBox as their rendered appearance.
public void addComboBoxToColumn(String[] options, int column_index){
ComboTableCellRenderer renderer = new ComboTableCellRenderer();
JComboBox<String> combo = new JComboBox<String>(options);
TableCellEditor combo_editor = new DefaultCellEditor(combo);
TableColumn column = getColumnModel().getColumn(column_index);
column.setCellRenderer(renderer);
column.setCellEditor(combo_editor);
}
...
public class ComboTableCellRenderer implements ListCellRenderer, TableCellRenderer
{
DefaultListCellRenderer listRenderer = new DefaultListCellRenderer();
DefaultTableCellRenderer tableRenderer = new DefaultTableCellRenderer();
private void configureRenderer(JLabel renderer, Object value)
{
if (value != null)
renderer.setText((String)value);
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
listRenderer = (DefaultListCellRenderer)listRenderer.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
configureRenderer(listRenderer, value);
return listRenderer;
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
tableRenderer = (DefaultTableCellRenderer)tableRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
configureRenderer(tableRenderer, value);
return tableRenderer;
}
}
My problem is that the combobox is always the size of the cell. I do not want that. Is it possible to make the combo box bigger? Some options in the combobox are too big and are half-hidden.
My problem is that the combobox is always the size of the cell. I do
not want that. Is it possible to make the combo box bigger? Some
options in the combobox are too big and are half-hidden.
not possible without jumping of cell Dimmension on the Screen, don't to confuse the user
to avoiding possible side effects, I'd be
create popup undecorated JDialog (for editable JComboBox), JWindow, put there JComboBox
add ListSelectionListener (have to change ListSelectionMode to SINGLE)
change built in KeyBinding in JTable for TableCellEditor (double_click or F2) to showing JDialog/JWindow have to center to the desired Point on the scren, setVisible must be wrapped in invokeLater
add ItemListener, test for SELECTED, on selected to store value to (setValueAt()) XxxTableModel, then to hide JDialog/JWindow
use only one JDialog (reuse by removeAll from content pane for another action from GUI) for whole JVM instance, only one for JTable
Override the JTable.editCellAt. This is the method that positions the table cell editor by calling setBounds on it. Just set the bounds differently in your preferred way, maybe should span over several columns.
How does one change the background color of a Java AWT List item? By that I mean a single item in a AWT List, not the whole thing.
You'll need a custom renderer. That is, if you're using Swing. It's better to stick with the Swing components and not the awt gui components.
JList
...
setCellRenderer(new MyCellRenderer());
...
class MyCellRenderer extends DefaultListCellRenderer
{
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
Color bg = <calculated color based on value>;
setBackground(bg);
setOpaque(true); // otherwise, it's transparent
return this; // DefaultListCellRenderer derived from JLabel, DefaultListCellRenderer.getListCellRendererComponent returns this as well.
}
}
Since Java AWT List inherits from Component, use Component's setBackground(Color c) method.
List coloredList = new List(4, false);
Color c = new Color(Color.green);
coloredList.add("Hello World")
coloredList.setBackground(c);
List now has a color green.
Been a while since I have worked with AWT, but can't you just use setBackground(Color)? List is a subclass of java.awt.Component.