Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.Color;
import java.awt.Component;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListCellRenderer;

public class Main extends JPanel {
    final String[] DATA = { "One\n1", "Two\n2", "Three\n3" };
    DefaultListModel<String> listModel = new DefaultListModel<>();
    JList<String> list = new JList<>(listModel);

    public Main() {
        list.setCellRenderer(new Listm(3, 30));
        add(new JScrollPane(list));
        for (String datum : DATA) {
            listModel.addElement(datum);
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.getContentPane().add(new Main());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

class Listm extends JTextArea implements ListCellRenderer {
    protected Listm(int rows, int cols) {
        super(rows, cols);
        setBorder(BorderFactory.createLineBorder(Color.blue));
    }

    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
            boolean cellHasFocus) {
        setText(value.toString());
        if (cellHasFocus) {
            setBackground(Color.RED);
        } else if (isSelected) {
            setBackground(Color.BLUE);
        } else {
            setBackground(null);
        }
        return this;
    }
}