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:Main.java

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

    StyledDocument document = new DefaultStyledDocument();

    SimpleAttributeSet attributes = new SimpleAttributeSet();
    attributes.addAttribute(StyleConstants.CharacterConstants.Bold, Boolean.FALSE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Italic, Boolean.FALSE);
    attributes.addAttribute(StyleConstants.CharacterConstants.Foreground, Color.LIGHT_GRAY);

    try {//  w  w  w.j ava2s . c  o m
        document.insertString(document.getLength(), " Bold, Italic and light gray color", attributes);
    } catch (BadLocationException badLocationException) {
        System.err.println("Bad insert");
    }

    JTextPane textPane = new JTextPane(document);
    textPane.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textPane);

    frame.add(scrollPane, BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    String HTMLTEXT = "<html><head><style>.foot{color:red} .head{color:black}</style></head>"
            + "<span style='font-family:consolas'>java2s.com</span><br/>"
            + "<span style='font-family:tahoma'>java2s.com</span>";
    JTextPane textPane1 = new JTextPane();

    textPane1.setContentType("text/html");
    textPane1.setFont(new Font("courier new", Font.PLAIN, 32));
    textPane1.setDocument(new HTMLDocument() {
        @Override/* ww w  .  jav  a  2s . com*/
        public Font getFont(AttributeSet attr) {
            StyleContext styles = (StyleContext) getAttributeContext();
            Font f = styles.getFont(attr);
            String ff = f.getFamily();
            System.out.println(ff);
            return textPane1.getFont();
        }
    });
    textPane1.setText(HTMLTEXT);

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new JScrollPane(textPane1));
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:JTextPaneWithIcon.java

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

    StyleContext context = new StyleContext();
    StyledDocument document = new DefaultStyledDocument(context);

    Style labelStyle = context.getStyle(StyleContext.DEFAULT_STYLE);

    Icon icon = new ImageIcon("yourFile.gif");
    JLabel label = new JLabel(icon);
    StyleConstants.setComponent(labelStyle, label);

    try {//w  ww . j ava2s  .  c om
        document.insertString(document.getLength(), "Ignored", labelStyle);
    } catch (BadLocationException badLocationException) {
        System.err.println("Oops");
    }

    JTextPane textPane = new JTextPane(document);
    textPane.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textPane);
    frame.add(scrollPane, BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) {
    JTree tree = new JTree();
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
    int treeSelectedRows[] = { 3, 1 };
    tree.setSelectionRows(treeSelectedRows);
    TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {

        @Override//from w ww  .j  a  va 2  s .  c o m
        public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
            JTree treeSource = (JTree) treeSelectionEvent.getSource();
            System.out.println("Min: " + treeSource.getMinSelectionRow());
            System.out.println("Max: " + treeSource.getMaxSelectionRow());
            System.out.println("Lead: " + treeSource.getLeadSelectionRow());
            System.out.println("Row: " + treeSource.getSelectionRows()[0]);
        }
    };
    tree.addTreeSelectionListener(treeSelectionListener);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JScrollPane(tree));
    frame.setPreferredSize(new Dimension(380, 320));
    frame.setLocation(150, 150);
    frame.pack();
    frame.setVisible(true);
}

From source file:MyListDataListener.java

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

    // Fill model
    final DefaultListModel model = new DefaultListModel();

    model.addListDataListener(new MyListDataListener());
    for (int i = 0, n = labels.length; i < n; i++) {
        model.addElement(labels[i]);/*from   w  w  w.  j  ava 2  s .  c  om*/
    }
    JList jlist = new JList(model);

    JScrollPane scrollPane1 = new JScrollPane(jlist);
    frame.add(scrollPane1, BorderLayout.CENTER);

    frame.setSize(640, 300);
    frame.setVisible(true);
}

From source file:RowSorterDemo.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Sort Table Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Object rows[][] = { { "J", 23 }, { "R", 24, }, { "E", 21, }, { "B", 27, }, { "A", 25, }, { "S", 22, }, };
    /* Specify column names */
    String columns[] = { "Name", "Age" };
    /* Create a TableModel */
    TableModel model = new DefaultTableModel(rows, columns) {
        public Class getColumnClass(int column) {
            Class returnValue;/*w ww. j av  a  2s .  c  o  m*/
            if ((column >= 0) && (column < getColumnCount())) {
                returnValue = getValueAt(0, column).getClass();
            } else {
                returnValue = Object.class;
            }
            return returnValue;
        }
    };

    JTable table = new JTable(model);

    RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);

    table.setRowSorter(sorter);

    JScrollPane pane = new JScrollPane(table);

    frame.add(pane, BorderLayout.CENTER);

    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:TreeNodeVector.java

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

    Vector<String> v1 = new TreeNodeVector<String>("Two", new String[] { "Mercury", "Venus", "Mars" });
    Vector<Object> v2 = new TreeNodeVector<Object>("Three");
    v2.add(System.getProperties());
    v2.add(v1);//from w  w w  .  j a  v  a  2  s . c o m
    Object rootNodes[] = { v1, v2 };
    Vector<Object> rootVector = new TreeNodeVector<Object>("Root", rootNodes);
    JTree tree = new JTree(rootVector);

    UIManager.put("Tree.line", Color.GREEN);
    tree.putClientProperty("JTree.lineStyle", "Horizontal");

    frame.add(new JScrollPane(tree), BorderLayout.CENTER);

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

}

From source file:TreeChangedRenderer.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Changed Renderer");
    JTree tree = new JTree();
    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();

    renderer.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 32));
    int rowHeight = tree.getRowHeight();
    if (rowHeight <= 0) {
        tree.setRowHeight(rowHeight - 1);
    }// ww w .  ja va 2s . c  o  m

    // Swap background colors
    Color backgroundSelection = renderer.getBackgroundSelectionColor();
    renderer.setBackgroundSelectionColor(renderer.getBackgroundNonSelectionColor());
    renderer.setBackgroundNonSelectionColor(backgroundSelection);

    // Swap text colors
    Color textSelection = renderer.getTextSelectionColor();
    renderer.setTextSelectionColor(renderer.getTextNonSelectionColor());
    renderer.setTextNonSelectionColor(textSelection);

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:LabelHeaderSample.java

public static void main(String args[]) {
    Object rows[][] = { { "one", "ichi - \u4E00" }, { "two", "ni - \u4E8C" }, { "three", "san - \u4E09" },
            { "four", "shi - \u56DB" }, { "five", "go - \u4E94" }, { "six", "roku - \u516D" },
            { "seven", "shichi - \u4E03" }, { "eight", "hachi - \u516B" }, { "nine", "kyu - \u4E5D" },
            { "ten", "ju - \u5341" } };
    JFrame frame = new JFrame("Label Header");
    String headers[] = { "English", "Japanese" };
    JTable table = new JTable(rows, headers);
    JScrollPane scrollPane = new JScrollPane(table);

    Icon redIcon = new DiamondIcon(Color.red);
    Icon blueIcon = new DiamondIcon(Color.blue);

    Border headerBorder = UIManager.getBorder("TableHeader.cellBorder");

    JLabel blueLabel = new JLabel(headers[0], blueIcon, JLabel.CENTER);
    blueLabel.setBorder(headerBorder);/*  w  w  w .  j  a  va2s .c om*/
    JLabel redLabel = new JLabel(headers[1], redIcon, JLabel.CENTER);
    redLabel.setBorder(headerBorder);

    TableCellRenderer renderer = new JComponentTableCellRenderer();

    TableColumnModel columnModel = table.getColumnModel();

    TableColumn column0 = columnModel.getColumn(0);
    TableColumn column1 = columnModel.getColumn(1);

    column0.setHeaderRenderer(renderer);
    column0.setHeaderValue(blueLabel);

    column1.setHeaderRenderer(renderer);
    column1.setHeaderValue(redLabel);

    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) throws BadLocationException {
    JFrame jf = new JFrame("StyledText");
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container cp = jf.getContentPane();

    JTextPane pane = new JTextPane();
    SimpleAttributeSet set = new SimpleAttributeSet();
    StyleConstants.setBold(set, true);

    // Set the attributes before adding text
    pane.setCharacterAttributes(set, true);
    pane.setText("java2s.com ");

    set = new SimpleAttributeSet();
    StyleConstants.setItalic(set, true);
    StyleConstants.setForeground(set, Color.red);
    StyleConstants.setBackground(set, Color.blue);

    Document doc = pane.getStyledDocument();
    doc.insertString(doc.getLength(), "Swing ", set);

    set = new SimpleAttributeSet();
    StyleConstants.setFontSize(set, 24);

    doc.insertString(doc.getLength(), "Tutorial", set);

    JScrollPane scrollPane = new JScrollPane(pane);
    cp.add(scrollPane, BorderLayout.CENTER);

    jf.setSize(400, 300);//from   w  ww  .  ja  v a 2  s . co m
    jf.setVisible(true);
}