Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;

public class Main extends JFrame {
    public Main() {
        DefaultListModel<String> model = new DefaultListModel<>();
        model.addElement("one");
        model.addElement("two");
        model.addElement("three");
        model.addElement("four");
        model.addElement("five");
        model.addElement("six");
        model.addElement("seven");
        model.addElement("eight");
        model.addElement("nine");
        model.addElement("ten");

        JList<String> list = new JList(model) {
            public String getToolTipText(MouseEvent e) {
                int row = locationToIndex(e.getPoint());
                Object o = getModel().getElementAt(row);
                return o.toString();
            }

            public Point getToolTipLocation(MouseEvent e) {
                int row = locationToIndex(e.getPoint());
                Rectangle r = getCellBounds(row, row);
                return new Point(r.width, r.y);
            }
        };

        JScrollPane scrollPane = new JScrollPane(list);
        getContentPane().add(scrollPane);
    }

    public static void main(String[] args) {
        Main frame = new Main();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setSize(400, 100);
        frame.setVisible(true);
    }
}