How to Add Buttons to a JList In Java Netbeans
In this Java Tutorial we will see How To Create a JList With Buttons Inside It In Java Using Netbeans.
What We Are Gonna Use In This Project:
- Java Programming Language.- NetBeans Editor.
Project Source Code:
package new_tutorials;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListCellRenderer;
import javax.swing.SwingConstants;
/**
*
* @author 1BestCsharp
*/
public class ButtonsInJListFrame extends JFrame{
// Array of ButtonListElement objects to be displayed in the list
private final ButtonListElement[] elements = {
new ButtonListElement("Button 1"),
new ButtonListElement("Button 2"),
new ButtonListElement("Button 3"),
new ButtonListElement("Button 4"),
new ButtonListElement("Button 5")
};
// Currently selected element in the list
private ButtonListElement selectedElement;
// JList component to display the button list
private JList<ButtonListElement> buttonList;
// JLabel to display the selected item label
private final JLabel selectedLabel;
public ButtonsInJListFrame(){
setTitle("Buttons In JList");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// Create JList with custom cell renderer
buttonList = new JList<>(elements);
buttonList.setCellRenderer(new ButtonListRenderer());
// Set the first item as the default selection
buttonList.setSelectedIndex(0);
selectedElement = elements[0];
// Add a list selection listener to handle item state changes
buttonList.addListSelectionListener((e) -> {
selectedElement = buttonList.getSelectedValue();
updateSelectedLabel();
buttonList.repaint();
});
// Add the JList to the center of the BorderLayout
add(new JScrollPane(buttonList), BorderLayout.CENTER);
// Create and add the selected label to the bottom of the BorderLayout
selectedLabel = new JLabel("Selected Item: " + selectedElement.getLabel());
selectedLabel.setHorizontalAlignment(SwingConstants.CENTER);
add(selectedLabel, BorderLayout.SOUTH);
setSize(300,300);
setLocationRelativeTo(null);
updateSelectedLabel();
}
// Create a method to update the text of the selected label
private void updateSelectedLabel()
{
selectedLabel.setText("Selected Item: " + (selectedElement != null ? selectedElement.getLabel() : ""));
}
// Create an inner class representing an element in the button list
public static class ButtonListElement{
private final String label;
// Constructor for ButtonListElement
public ButtonListElement(String label){
this.label = label;
}
// Getter method for the label
public String getLabel(){ return label;}
}
// Create an inner class representing the custom renderer for the button list
private class ButtonListRenderer extends JButton implements ListCellRenderer<ButtonListElement>{
// Constructor for the ButtonListRenderer
public ButtonListRenderer(){
setFont(new Font("Arial", Font.PLAIN, 18));
setMargin(new Insets(5, 10, 5, 10));
}
// Method to render the list cell component
@Override
public Component getListCellRendererComponent(JList<? extends ButtonListElement> list, ButtonListElement value, int index, boolean isSelected, boolean cellHasFocus) {
setText(value.getLabel());
// Set background and foreground colors based on selection state
if(isSelected){
setBackground(Color.RED);
setForeground(Color.WHITE);
}
else{
setBackground(list.getBackground());
setForeground(list.getForeground());
}
return this;
}
}
public static void main(String[] args) {
ButtonsInJListFrame frame = new ButtonsInJListFrame();
frame.setVisible(true);
}
}
The Final Result:
More Java Projects:
Download Projects Source Code