When a JComboBox is just made and added and all the background of the selected item is just normal and white:
(ignore the enormous spacing after the text)
When I then open the list and hover my cursor over an item, that item get's highlighted, all normal, nothing wrong with it.
But the problem now is that the highlighting stays once I've clicked on an item:
So my question is:
How can I make the highlighting go away?
Preferably without doing difficult with packages from the community or overloading or whatever.
If I'm right it has to be in the 'root' of the action listener of the combo box?
So:
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == comboBox)
{
// code to delete the highlighting
}
}
The highlighting is done by the default renderer for the combo box.
See the section from the Swing tutorial on Providing Custom Renderers for an example of providing your own custom renderer. You will just want a renderer that doesn't change the background/foreground depending on the selected value.
To make it easier for people with a similar problem, here is the code for the renderer I wrote:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class ComboBoxRenderer extends JLabel implements ListCellRenderer
{
private boolean colorSet;
private Color selectionBackgroundColor;
public ComboBoxRenderer()
{
setOpaque(true);
setHorizontalAlignment(LEFT);
setVerticalAlignment(CENTER);
colorSet = false;
selectionBackgroundColor = Color.red; // Have to set a color, else a compiler error will occur
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
// Check if color is set (only runs the first time)
if(!colorSet)
{
// Set the list' background color to original selection background of the list
selectionBackgroundColor = list.getSelectionBackground();
// Do this only one time since the color will change later
colorSet = true;
}
// Set the list' background color to white (white will show once selection is made)
list.setSelectionBackground(Color.white);
// Check which item is selected
if(isSelected)
{
// Set background color of the item your cursor is hovering over to the original background color
setBackground(selectionBackgroundColor);
}
else
{
// Set background color of all other items to white
setBackground(Color.white);
}
// Do nothing about the text and font to be displayed
setText((String)value);
setFont(list.getFont());
return this;
}
}
Edit: Previous code didn't seem to work as properly, code updated and should work all fine now
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.
This may end up being noted as a formerly asked question, however the other questions have yet to answer the question that I have asked properly, to what I need them to do.
I want it to be that whenever I click the ToggleButton it will set the color of the ToggleButton to black (in this case), and then when I unclick the button it will return to the default color.
Here is the code for the individual button, its possible Ill have to create a new class (I am using Eclipse) but if anyone could help id be extremely thankful. (I also have Jigloo)
jToggleButton4 = new JToggleButton();
getContentPane().add(jToggleButton4);
FlowLayout jToggleButton4Layout = new FlowLayout();
jToggleButton4.setLayout(jToggleButton4Layout);
jToggleButton4.setText("Black");
jToggleButton4.setPreferredSize(new java.awt.Dimension(133, 249));
You can take the default color/ the color you keep initially and can toggle the background color with an ActionEvent,
JToggleButton jtb = new JToggleButton("My Button");
Color defaultColor=jtb.getBackground();
jtb.addActionListener(new ActionListener( ) {
public void actionPerformed(ActionEvent ev) {
if(jtb.getBackground()==defaultColor)
jtb.setBackground(Color.BLACK);
else
jtb.setBackground(defaultColor);
repaint();//repaint your frame
System.out.println("BackGround color changed!");
}
});
I spent the last few hours looking for a solution or at least a decent guide about this issue, but found nothing.
I'm implementing a custom Swing Look and Feel for a small GUI of mine. Up until now I've been using the UIManager.put("key", values); method to good success, but I got stuck when it came to properly modify JComboBoxes.
Using this list I managed to get my jComboBoxes really close to what I want them to look like:
I have two issues with this, a major and a minor one:
Major
I want the blue shown border gone.
Minor
I'd really like the black shown border gone.
Apparently no key in the UIDefaults has anything to do with either two borders: they seem somehow hardcoded in the Look and Feel I'm modifying (which should be Metal). I resorted to manually extending the ComboBoxRenderer and managed to come up with this:
package exec.laf.theme;
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicComboBoxRenderer;
public class ComboBoxRenderer extends BasicComboBoxRenderer {
private Color background;
private Color selectionBackground;
public ComboBoxRenderer() {
super();
background = UIManager.getColor("ComboBox.background");
selectionBackground = UIManager.getColor("ComboBox.selectionBackground");
}
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
setText((String) value);
if (isSelected) setBackground(selectionBackground);
else setBackground(background);
return this;
}
}
Where I specify this Renderer every time I create a JComboBox like so:
aComboBox.setRenderer(new ComboBoxRenderer());
obtaining the same look as the non-extended JComboBox.
The problem is that with this extension I can't find a way of touching those borders. Adding setBorder(new EmptyBorder(0, 0, 0, 0)); accomplishes nothing, since it simply adds a border to the listed items.
I checked the source code for javax.swing.plaf.basic.BasicComboBoxRenderer to see if any borders were applied there, but found nothing (the only border there is the one applied to the listed items, that I can override as shown above.
What am I supposed to do? Am I extending the wrong class, or am I missing something else?
The solution I found is:
UIManager.put("ComboBox.borderPaintsFocus", Boolean.TRUE)
This sets a boolean inside the ComboBoxUI that prevents rendering of the focus border, which is the border painted around all buttons when they are focused. Its style is dependent on your look and feel.
To remove the black border of comboBox PopUp,
Object child = comboBox.getAccessibleContext().getAccessibleChild(0);
BasicComboPopup popup = (BasicComboPopup)child;
popup.setBorder(BorderFactory.createEmptyBorder());
If i understoud, your problem is in generally how to use extended class from BasicComboBoxRenderer. So here a simple code to explain you how to use it:
public class RenderComboBox extends BasicComboBoxRenderer {
Color selectedBackground;
Color selectedForground;
Color background;
Color forground;
public RenderComboBox() {
setOpaque(true);
background = new Color(37, 37, 37);
selectedBackground = new Color(93, 93, 93);
forground = Color.WHITE;
selectedForground = forground;
}
#Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
if (isSelected) {
setBackground(selectedBackground);
setForeground(selectedForground);
} else {
setBackground(background);
setForeground(forground);
}
setFont(list.getFont());
if (value == null) {
setText("");
} else {
setText(value.toString());
}
return this;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setLayout(null);
frame.setPreferredSize(new Dimension(200, 170));
JComboBox<String> combobox = new JComboBox<>();
combobox.setRenderer(new RenderComboBox());
combobox.setBounds(50, 50, 100, 20);
combobox.addItem("TEST");
combobox.addItem("REVERT");
frame.add(combobox);
frame.pack();
frame.setVisible(true);
}
}
I have a JTable that just contains one item which is a JButtton that has an icon which is set to the JFreeChart generated chart.
If you click the button it launches a new window with the JFreeChart chart.
I have a cell renderer for the button which simply returns the JButton object that contains the chart item.
If I single click on the button it will launch the new window.
But if I double click on the button the new window launches, but the icon on the button, which used to be the JFreeChart, changes to just a text string with the class name path of the JButton with the class field values.
They other way I can get this behavior to happen is if I remove the cell renderer then the JButton just diplays the class name. So I'm not sure what is going on and have played around with it a ton and haven't made any progress. I was going to post some code but there is just too much of it.
If anybody has any ideas, I can post certain sections of code to help explain.
Here is code:
Class init {
...
ItModel itModel = new ItModel ();
JTable table = new JTable(itModel );
TableRendrend rend = new TableRend();
table.getColumn("it").setCellRenderer(rend);
}
class TableRend implements TableCellRenderer {
JButton itButton;
...
getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int columns){
if(itButton ==null){
itButton = new JButton (JButton)value.getIcon();
}
return itButton;
}
}
Class itModel extends AbstractTableModel{
JButton button;
...
public Object getValueAt(int rowIndex, intColumnIndex){
if(button==null){
button = new JButton( new ImageIcon(JFreeChartImage.getImage()));
}
return button
}
}
This all works except when double clicking on the JButton and it displays TEXT ex.
javax.Swing.JButton(0,0,...)
all field values instead of the the actual chart image that should be displayed
There are several issues with the code you posted, and too long to include them all in a comment.
You should not store Swing components in the model. The creation of the components is the task for the renderer
I do not believe you can click the button which is returned by the renderer. That button is not contained in the JTable which is displayed. Only an image/screenshot/stamp of the button is drawn on-screen, but it does not behave like a button. You would need an editor for that. Consult the JTable tutorial about the editors and renderers for more information
I made a Setting panel, in which I am setting color for Main table cell by pressing set button in setting window. There are two combo box button greater and less. The value greater than the value in combo box will be colored in a particular color and less than in same way. But the colors are not rendered after pressing SET, but it is rendered after minimizing and then maximizing the main window.
Below is actionPerformed() method:
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnCancel)
{
MainUI1.isShowModel=true;
//mf.setVisible(true);
this.dispose();
}
if(e.getSource()==btnset)
{
// MainUI1.isShowModel=true;
ColorSettings.setColor();
validate();
}
Try to use Component.repaint method. Also see this question Java Swing revalidate() vs repaint()