Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoundedRangeModel;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTextField;

public class Main {

    public static void main(String args[]) {

        final JTextField textField = new JTextField();

        JScrollBar scrollBar = new JScrollBar(JScrollBar.HORIZONTAL);

        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        BoundedRangeModel brm = textField.getHorizontalVisibility();
        scrollBar.setModel(brm);
        panel.add(textField);
        panel.add(scrollBar);

        textField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.out.println("Text: " + textField.getText());
            }
        });

        JFrame frame = new JFrame("Text Slider");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(panel, BorderLayout.NORTH);
        frame.setSize(300, 100);
        frame.setVisible(true);
    }
}