Example usage for javax.swing JScrollPane JScrollPane

List of usage examples for javax.swing JScrollPane JScrollPane

Introduction

In this page you can find the example usage for javax.swing JScrollPane JScrollPane.

Prototype

public JScrollPane(Component view) 

Source Link

Document

Creates a JScrollPane that displays the contents of the specified component, where both horizontal and vertical scrollbars appear whenever the component's contents are larger than the view.

Usage

From source file:MainClass.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("Element Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JTextArea textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(textArea);

    JButton button = new JButton("Show Elements");
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Document document = textArea.getDocument();
            ElementIterator iterator = new ElementIterator(document);
            Element element = iterator.first();
            while (element != null) {
                System.out.println(element.getStartOffset());
                element = iterator.next();
            }//from  w w w .  jav  a2  s.  c  o m
        }
    };
    button.addActionListener(actionListener);

    frame.add(scrollPane, BorderLayout.CENTER);
    frame.add(button, BorderLayout.SOUTH);

    frame.setSize(250, 250);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);

    // Create some data
    model.addColumn("Col1");
    model.addRow(new Object[] { "r1" });
    model.addRow(new Object[] { "r2" });
    model.addRow(new Object[] { "r3" });

    model.removeRow(model.getRowCount() - 1);

    JFrame f = new JFrame();
    f.setSize(300, 300);// w w  w. ja  v  a2  s  .c om
    f.add(new JScrollPane(table));
    f.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    Object rowData[][] = { { "Row1-Column1", "Row1-Column2", "Row1-Column3" },
            { "Row2-Column1", "Row2-Column2", "Row2-Column3" } };
    Object columnNames[] = { "Column 1", "Column 2", "Column 3" };

    JTable table = new JTable(rowData, columnNames);
    table.setTableHeader(null);//from  w w  w  .j  av  a2s  . c  om
    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.setColumnHeaderView(null);
    JFrame frame = new JFrame();
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
    DefaultTreeModel model = new DefaultTreeModel(root);
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(new JTree(model)));
    f.getContentPane().add(new JButton(new AbstractAction("Add thousand children") {
        @Override//from ww w .ja  v a  2  s.  com
        public void actionPerformed(ActionEvent e) {
            int offset = root.getChildCount() + 1;
            for (int i = 0; i < 1000; i++) {
                DefaultMutableTreeNode child = new DefaultMutableTreeNode("Person " + (i + offset));
                // model.insertNodeInto(child, root, root.getChildCount());
                root.add(child);
            }
            model.nodeStructureChanged(root);
        }
    }), BorderLayout.PAGE_END);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File archivo = new File("c:/Java_Dev/run.bat");
    FileReader fr = new FileReader(archivo);
    BufferedReader br = new BufferedReader(fr);

    Vector<String> lines = new Vector<String>();

    String line;//from w  ww .  j a v a  2  s .  com
    while ((line = br.readLine()) != null) {
        lines.add(line);
    }
    JOptionPane.showMessageDialog(null, new JScrollPane(new JList(lines)));
    fr.close();
}

From source file:AdjustmentTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    Icon icon = new ImageIcon("java2s.gif");
    JButton b = new JButton(icon);
    JScrollPane pane = new JScrollPane(b);
    AdjustmentListener hListener = new AdjustmentListener() {
        public void adjustmentValueChanged(AdjustmentEvent e) {
            System.out.println("Horizontal: ");
            dumpInfo(e);//from w  w w .  j a v a2 s.c  om
        }
    };
    JScrollBar hBar = pane.getHorizontalScrollBar();
    hBar.addAdjustmentListener(hListener);
    AdjustmentListener vListener = new AdjustmentListener() {
        public void adjustmentValueChanged(AdjustmentEvent e) {
            System.out.println("Vertical: ");
            dumpInfo(e);
        }
    };
    JScrollBar vBar = pane.getVerticalScrollBar();
    vBar.addAdjustmentListener(vListener);
    contentPane.add(pane, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.show();
}

From source file:TreeSelectionOption.java

public static void main(String[] argv) {
    JTree tree = new JTree();

    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.CONTIGUOUS_TREE_SELECTION);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JScrollPane(tree));
    frame.setSize(380, 320);/*from  w  w  w.j  a  v  a 2 s  . c o m*/
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    int ROW_HEIGHT = 40;
    String[] TABLE_COLUMNS = { "Foo", "Bar" };
    DefaultTableModel tableModel = new DefaultTableModel(TABLE_COLUMNS, 2);
    JTable table = new JTable(tableModel);
    table.setRowHeight(ROW_HEIGHT);/*from  w  ww  .  ja v a2  s . co  m*/
    JScrollPane scrollpane = new JScrollPane(table);
    JButton addRowBtn = new JButton(new AbstractAction("Add Row") {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            tableModel.addRow(new String[] { "", "" });
        }
    });
    JPanel btnPanel = new JPanel();
    btnPanel.add(addRowBtn);
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(scrollpane, BorderLayout.CENTER);
    f.getContentPane().add(btnPanel, BorderLayout.PAGE_END);
    f.pack();
    f.setLocationByPlatform(true);
    f.setVisible(true);
}

From source file:ElementSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Element Example");
    Container content = frame.getContentPane();

    final JTextArea textArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(textArea);

    JButton button = new JButton("Show Elements");
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Document document = textArea.getDocument();
            ElementIterator iterator = new ElementIterator(document);
            Element element = iterator.first();
            while (element != null) {
                System.out.println(element.getStartOffset());
                element = iterator.next();
            }/*  w  ww  . j  a v a 2s  . com*/
        }
    };
    button.addActionListener(actionListener);

    content.add(scrollPane, BorderLayout.CENTER);
    content.add(button, BorderLayout.SOUTH);

    frame.setSize(250, 250);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    String text = "one two three four five six seven eight nine ten ";
    JTextArea textArea = new JTextArea(text);
    textArea.setColumns(30);/*from   w ww  .j a  v  a 2  s .co m*/
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    textArea.append(text);
    textArea.append(text);
    textArea.append(text);
    textArea.append(text);
    textArea.append(text);
    textArea.setSize(textArea.getPreferredSize().width, 1);
    JOptionPane.showMessageDialog(null, new JScrollPane(textArea), "Not Truncated!",
            JOptionPane.WARNING_MESSAGE);
}