Set the border in a JList in Java - java

I have a JList in Java. Whenever the user clicks on an element,
I would like to add a specific borderLine in that element.
Is that possible?
I have the following code in Java:
DefaultListModel listModel = new DefaultListModel();
listModel.addElement("element1");
listModel.addElement("element2");
listModel.addElement("element3");
list = new JList(listModel);
list.addListSelectionListener(this);
For the listener i have:
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {
if (list.getSelectedIndex() == -1) {
;
} else {
CurrentIndex = list.getSelectedIndex();
//set Current's Index border, how can i do that?
}
}
}

Create a custom renderer for the list and add a Border to the item that is selected.
Read the section from the Swing tutorial on How to Use Lists for more information and examples.

You can use a custom ListCellRender. Then in the if (isSelected) just add the border.
public class MyListCellRenderer implements ListCellRenderer{
private final JLabel jlblCell = new JLabel(" ", JLabel.LEFT);
Border lineBorder = BorderFactory.createLineBorder(Color.BLACK, 1);
Border emptyBorder = BorderFactory.createEmptyBorder(2, 2, 2, 2);
#Override
public Component getListCellRendererComponent(JList jList, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
jlblCell.setOpaque(true);
if (isSelected) {
jlblCell.setForeground(jList.getSelectionForeground());
jlblCell.setBackground(jList.getSelectionBackground());
jlblCell.setBorder(new LineBorder(Color.BLUE));
} else {
jlblCell.setForeground(jList.getForeground());
jlblCell.setBackground(jList.getBackground());
}
jlblCell.setBorder(cellHasFocus ? lineBorder : emptyBorder);
return jlblCell;
}
}
Then instantiate the render.
MyListCellRenderer renderer = new MyListCellRenderer();
JList list = new JList();
list.setCellRenderer(renderer);
The renderer will return a JLabel. So you can do anything you want to that label and that's what will appear in cell.
See ListCellRenderer javadoc

Related

Make JTable cells in specific column scrollable

I'm creating GUI in java using swing. I use JTable that looks like this.
I need to make cells of last column("Popis") to be scrollable as there will be description so String containing this description has variable lenght for each row.
This is how I created my JTable.
this.tableModel = new DefaultTableModel(
new Object[][] { },
new String[] {"ID", "Nazov", "Naplnena kapacita / kapacita", "Popis"}
);
this.table1.setModel(this.tableModel);
I add data to table using this.tablemodel.addRow();
Is there a way to make cells of last column of my table scrollable?
It would be difficult and awkward to embed a scrolling component inside a JTable. A common approach to showing long cell values is to make the cell’s tooltip (hover text) display the full value:
table1.setModel(tableModel);
table1.getColumnModel().getColumn(3).setCellRenderer(
new DefaultTableCellRenderer() {
#Override
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean selected,
boolean focused,
int row,
int column) {
Component component = super.getTableCellRendererComponent(
table, value, selected, focused, row, column);
if (component instanceof JComponent) {
((JComponent) component).setToolTipText(
Objects.toString(value, null));
}
return component;
}
});
(Objects is the java.util.Objects class.)
It can be solved by creating private class that extends AbstractCellEditor and implements TableCellEditor. Then create and customize the scrollbar. Here is example.
public class MainForm {
private JTable table1;
private JPanel panel1;
private JTextArea text = new JTextArea();
public MainForm() {
text.setEditable(false);
DefaultTableModel tableModel = new DefaultTableModel(new Object[][]{}, new String[]{"Meno", "Priezvisko", "Datum", "Popis"});
table1.setModel(tableModel);
table1.setRowHeight(40);
tableModel.addRow(new Object[]{"Jaimi", "Parker", "2022", "fdjfadufaouifasoifjasifhasiofa \nasdasdasdasdasdasdas \nasdasdasdasdasdasd\nasdasdasdasd "});
tableModel.addRow(new Object[]{"William", "Mcdonald", "2021", "fdjfadufaouasfadfdsfafifasoifjasifhasiofa"});
tableModel.addRow(new Object[]{"Matt", "Ashley", "2020", "asfasfafdsfgdafgdfgasgsdg"});
tableModel.addRow(new Object[]{"Ariana", "Burns", "2019", "fdjfadufaouifasfdsgdgagsgsdgsdgsdagsdgsdgsdgsagsdgoifjasifhasiofa"});
TableColumn column = table1.getColumn("Popis");
column.setCellEditor(new HScrollableTCE());
}
public static void main(String[] args) {
JFrame frame = new JFrame("MainForm");
frame.setContentPane(new MainForm().panel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
private class HScrollableTCE extends AbstractCellEditor implements TableCellEditor
{
JScrollPane scroll = new JScrollPane(text, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int rowIndex, int vColIndex)
{
if (value != null) {
scroll.setHorizontalScrollBar(scroll.createHorizontalScrollBar());
scroll.getHorizontalScrollBar().setPreferredSize(new Dimension(0, 5));
scroll.setBorder(new EmptyBorder(0,0,0,0));
scroll.setToolTipText(value.toString());
text.setText(value.toString());
return scroll;
}
return null;
}
public Object getCellEditorValue()
{
return text.getText();
}
}
}

The button click in one row of Jtable changes labels in all table records

I try to make the list of JPanel with buttons, edittext fields and other Swing components but cant find the examples of making listviews in swing. I tried to do this with jTable that has one column whith JPanel in cells but my label fields changes in all jPanel elements after click of button
Here is the code, that i made from different examples. This class has render and editor methods. I try to change the text of label when button is clicked. But all labels (I want to change only one that is needed) change their text... May be i set onMouseListener not in that place...
public class RssFeedCell extends AbstractCellEditor implements TableCellEditor, TableCellRenderer {
JPanel panel;
JLabel text;
JButton showButton;
JLabel label;
RssFeed feed;
public RssFeedCell() {
label = new JLabel();
text = new JLabel();
showButton = new JButton("View Articles");
showButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
label.setText("ed");
}
});
panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
panel.add(text);
panel.add(showButton);
panel.add(label);
}
private void updateData(RssFeed feed, boolean isSelected, JTable table) {
this.feed = feed;
text.setText("" + feed.name + "" + feed.url + "Articles " + feed.articles.length + "");
if (isSelected) {
panel.setBackground(table.getSelectionBackground());
} else {
panel.setBackground(table.getBackground());
}
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
RssFeed feed = (RssFeed) value;
updateData(feed, true, table);
return panel;
}
public Object getCellEditorValue() {
return null;
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
RssFeed feed = (RssFeed) value;
updateData(feed, isSelected, table);
return panel;
}
}
There is only one label. You need to incorporate the cell state into the TableModel data.

Preferred height of JPanel is lower then combined height of its children in table renderer

I have a JTable for which the renderer returns a JPanel composed of multiple JLabel instances. One of those JLabels can contain HTML used among other things to split the output over multiple lines using <br/> tags.
To show the multiple lines in the table, the renderer calls in the getTableCellRendererComponent method
table.setRowHeight(row, componentToReturn.getPreferredSize().height);
to dynamically update the row height, based on the contents. This only works correctly if componentToReturn indicates a correct preferred size.
It looks however that the getPreferredSize returns bogus values. The preferred height of the returned component is smaller than the sum of the heights of the labels inside the component.
Here is a little program illustrating this behaviour (without using a JTable)
import java.awt.*;
import javax.swing.*;
public class SwingLabelTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
LabelPanel renderer = new LabelPanel();
Component component = renderer.getComponent(false);
//asking for a bigger component will not
//update the preferred size of the returned component
component = renderer.getComponent(true);
}
});
}
private static class LabelPanel {
private final JPanel compositePanel;
private final JLabel titleLabel = new JLabel();
private final JLabel propertyLabel = new JLabel();
public LabelPanel() {
JPanel labelPanel = new JPanel();
labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.PAGE_AXIS));
labelPanel.add(titleLabel);
labelPanel.add(propertyLabel);
compositePanel = new JPanel(new BorderLayout());
//normally it contains more components,
//but that is not needed to illustrate the problem
compositePanel.add(labelPanel, BorderLayout.CENTER);
}
public Component getComponent( boolean aMultiLineProperty ) {
titleLabel.setText("Title");
if ( aMultiLineProperty ){
propertyLabel.setText("<html>First line<br/>Property: value</html>");
} else {
propertyLabel.setText("Property: value");
}
int titleLabelHeight = titleLabel.getPreferredSize().height;
int propertyLabelHeight = propertyLabel.getPreferredSize().height;
int compositePanelHeight = compositePanel.getPreferredSize().height;
if ( compositePanelHeight < titleLabelHeight + propertyLabelHeight){
throw new RuntimeException("Preferred size of the component returned "
+ "by the renderer is incorrect");
}
return compositePanel;
}
}
}
As I am aware that the previous example is a bit far-fetched, here an example which includes a JTable
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
public class SwingTableTest {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
DefaultTableModel tableModel = new DefaultTableModel(0, 1);
JTable table = new JTable(tableModel);
table.setDefaultRenderer(Object.class, new DataResultRenderer());
tableModel.addRow(new Object[]{new Object()});
tableModel.addRow(new Object[]{new Object()});
tableModel.addRow(new Object[]{new Object()});
JFrame testFrame = new JFrame("TestFrame");
testFrame.getContentPane().add(new JScrollPane(table));
testFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
testFrame.setSize(new Dimension(300, testFrame.getPreferredSize().height));
testFrame.setVisible(true);
}
});
}
private static class DataResultRenderer implements TableCellRenderer {
private final JPanel compositePanel;
private final JLabel titleLabel = new JLabel();
private final JLabel propertyLabel = new JLabel();
public DataResultRenderer() {
JPanel labelPanel = new JPanel();
labelPanel.setOpaque(false);
labelPanel.setLayout(new BoxLayout(labelPanel, BoxLayout.PAGE_AXIS));
labelPanel.add(titleLabel);
labelPanel.add(propertyLabel);
compositePanel = new JPanel(new BorderLayout());
//normally it contains more components,
//but that is not needed to illustrate the problem
compositePanel.add(labelPanel, BorderLayout.CENTER);
}
#Override
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column) {
titleLabel.setText("Title");
if ( row == 2 ){
propertyLabel.setText("<html>Single property: value</html>");
} else {
String text = "<html>";
text += "First property<br/>";
text += "Second property<br/>";
text += "Third property:value";
text += "</html>";
propertyLabel.setText(text);
}
int titleLabelHeight = titleLabel.getPreferredSize().height;
int propertyLabelHeight = propertyLabel.getPreferredSize().height;
int compositePanelHeight = compositePanel.getPreferredSize().height;
if ( compositePanelHeight < titleLabelHeight + propertyLabelHeight){
throw new RuntimeException("Preferred size of the component returned "
+ "by the renderer is incorrect");
}
table.setRowHeight(row, compositePanel.getPreferredSize().height);
return compositePanel;
}
}
}
I am looking for a way to update the row height of the table to ensure that the multi-line content is completely visible, without knowing up front how many lines each row will contain.
So either I need a solution to retrieve the correct preferred size, or my approach is completely wrong and then I need a better one.
Note that the above examples are simplified. In the real code, the "renderer" (the code responsible for creating the component) is decorated a few times. This means that the outer renderer is the only with access to the JTable, and it has no knowledge about what kind of Component the inner code returns.
Because setRowHeight() "Sets the height, in pixels, of all cells to rowHeight, revalidates, and repaints," the approach is unsound. Absent throwing an exception, profiling shows 100% CPU usage as an endless cascade of repaints tries to change the row height repeatedly. Moreover, row selection becomes unreliable.
Some alternatives include these:
Use TablePopupEditor to display multi-line content on request from a TableCellEditor.
Update an adjacent multi-line panel from a TableModelListener, as shown here.

JList display error with custom ListCellRenderer

I am having trouble with one a custom ListCellRenderer on a JList. When there is only 1 item in the List the cell is displayed correctly, but when there is more than 1 item, each cell seams to be painted with the content of all cells overlapping each other, like this:
My CellRenderer looks like this:
public class SendungsCellRenderer extends JPanel implements ListCellRenderer {
private EmptyBorder eb = new EmptyBorder(5, 2, 5, 2);
private LineBorder lb = new LineBorder(new Color(255,255,255), 5);
#Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
uiSendungsbutton p = (uiSendungsbutton) value;
if(isSelected){
this.setBackground(new Color(200,200,250));
this.setBorder(new CompoundBorder(lb, new StrokeBorder(new BasicStroke())));
}else{
this.setBackground(new Color(252,252,252));
this.setBorder(lb);
}
this.setLayout(p.getLayout());
this.add(p.getNamePnl(),BorderLayout.NORTH);
this.add(p.getKdnrPnl(), BorderLayout.CENTER);
return this;
}
}
and it is set using
list_Sendung = new JList(getSendungen().toArray());
list_Sendung.setVisibleRowCount(1);
list_Sendung.setValueIsAdjusting(true);
list_Sendung.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list_Sendung.setCellRenderer(new SendungsCellRenderer());
The method getSendungen() returns an ArrayList of uiSendungsbutton.
How do I get the JList to display each item in a cell of its own correctly?
The problem is that the same cell renderer is being used for all cells, and for each new cell, you add the components to this again. To fix this problem, remove all components from this each time, using removeAll. Your code would look like this after it was fixed:
public class SendungsCellRenderer extends JPanel implements ListCellRenderer {
private EmptyBorder eb = new EmptyBorder(5, 2, 5, 2);
private LineBorder lb = new LineBorder(new Color(255,255,255), 5);
#Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
this.removeAll();
uiSendungsbutton p = (uiSendungsbutton) value;
if(isSelected){
this.setBackground(new Color(200,200,250));
this.setBorder(new CompoundBorder(lb, new StrokeBorder(new BasicStroke())));
}else{
this.setBackground(new Color(252,252,252));
this.setBorder(lb);
}
this.setLayout(p.getLayout());
this.add(p.getNamePnl(),BorderLayout.NORTH);
this.add(p.getKdnrPnl(), BorderLayout.CENTER);
return this;
}
}

ListCellRendererComponent() method updates all the objects(elements) of JList similar to last added element

hi there i implemented a JList which contains JLabels as elements. My aim is implementing a contact list for server/client chat application. So, when a client connects to a server, JList will build to show his/her contact list.I selected to use JLabels cause they can have icons and text as well. However, i'm getting some trouble about overridden cellrenderer method. Whenever, a client gets online/offline JList updates its state and set all the items similar to the last added item. It's something like this,
this is the first time adding an offline state client into the friend list of a person;
afterwards, this is the second time adding a different client
and finally third time...
Furthermore, i remember that in tutorial it mention about JList overrides pain method and draw whole elements again and again when there is a change in the list. Well actually i'm a newbie about this rendering subject and this thing gets very annoying. Here you can see my renderer class;
RendererSample
and add an element into the model like this way in my main class
labelSetText = tempon.getNickName();
onlineStatus = tempon.isIsOnline();
model.addElement(createPanel());
and also createPanel() returns a JLabel which is like;
public static JLabel createPanel() {
JLabel panel = new JLabel();
return panel;
}
i hope that i have been clear about my problem. I have to achieve that when a contact changes his/her state or when a contact is added this action shouldn't affect the other elements. I will be very appreciate for every answers (and also if you can add brief explanation that what and why you did i will be grateful.) well thanks anyway
You should not put components such as JLabel into JList. Instead, use a model to hold the data and add a renderer to customize the presentation. See How to Use Lists for some examples.
Here is a very basic example of a renderer that adds icon:
import java.awt.BorderLayout;
import java.awt.Component;
import javax.swing.*;
public class TestUserList {
public static class UserRenderer extends DefaultListCellRenderer {
#Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean hasFocus) {
if (value instanceof UserEntry) {
UserEntry user = (UserEntry) value;
JLabel label = (JLabel) super.getListCellRendererComponent(
list, user.getName(), index, isSelected, hasFocus);
if (user.isOnline())
label.setIcon(UIManager
.getIcon("OptionPane.informationIcon"));
else
label.setIcon(UIManager.getIcon("OptionPane.errorIcon"));
return label;
}
return super.getListCellRendererComponent(list, value, index,
isSelected, hasFocus);
}
}
public TestUserList() {
JFrame frame = new JFrame("User List");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JList list = new JList();
JScrollPane scrollPane = new JScrollPane(list);
JPanel content = new JPanel(new BorderLayout());
content.add(scrollPane, BorderLayout.CENTER);
final DefaultListModel model = new DefaultListModel();
model.addElement(new UserEntry("John", true));
model.addElement(new UserEntry("Jesse", false));
list.setModel(model);
list.setCellRenderer(new UserRenderer());
frame.add(content);
frame.setLocationByPlatform(true);
frame.pack();
frame.setVisible(true);
}
public class UserEntry {
private String name;
private boolean online;
public UserEntry(String name, boolean online) {
super();
this.name = name;
this.online = online;
}
public String getName() {
return name;
}
public Boolean isOnline() {
return online;
}
}
public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TestUserList();
}
});
}
}

Categories