Example usage for javax.swing JScrollPane getViewport

List of usage examples for javax.swing JScrollPane getViewport

Introduction

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

Prototype

public JViewport getViewport() 

Source Link

Document

Returns the current JViewport.

Usage

From source file:Main.java

public static void main(String[] args) {
    JScrollPane sPane = new JScrollPane();
    sPane.setPreferredSize(new Dimension(200, 150));
    JButton button = new JButton(new AbstractAction("Create Table") {
        public void actionPerformed(ActionEvent arg0) {
            DefaultTableModel model = new DefaultTableModel(new Integer[][] { { 1, 2 }, { 3, 4 } },
                    new String[] { "A", "B" });
            JTable table = new JTable(model);
            sPane.getViewport().add(table);
        }//from   w  w w  . j  a  va  2  s.  co  m
    });
    JPanel panel = new JPanel();
    panel.add(sPane);
    panel.add(button);
    JOptionPane.showMessageDialog(null, panel);

}

From source file:Main.java

public static void main(String[] args) {
    final JTextPane textPane = new JTextPane();
    final JScrollPane scrollPane = new JScrollPane(textPane);

    String text = "Lorem ipsum dolor sit amet, " + "consectetur adipiscing elit."
            + "Fusce nec sapien id diam consequat adipiscing.";
    textPane.setText(text);//from  w  w  w .j a  va 2  s.  com

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(scrollPane);

    frame.setSize(new Dimension(200, 200));
    frame.setVisible(true);

    FontMetrics metrics = textPane.getFontMetrics(textPane.getFont());
    textPane.setMargin(new Insets(scrollPane.getViewport().getHeight() - metrics.getHeight(), 0, 0, 0));
}

From source file:UIDefaultsTreeModel.java

public static void main(String[] args) {
    JFrame treeFrame1 = new JFrame();

    JScrollPane jScrollPane1 = new JScrollPane();
    JTree jTree1 = new JTree(new UIDefaultsTreeModel());

    treeFrame1.setSize(new Dimension(400, 300));
    treeFrame1.addWindowListener(new WindowAdapter() {
        public void windowClosed(WindowEvent e) {
            System.exit(0);/*from  www.  ja  v a  2s .c o m*/
        }
    });
    treeFrame1.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    treeFrame1.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
    jScrollPane1.getViewport().add(jTree1, null);

    treeFrame1.setVisible(true);
}

From source file:DocumentModel.java

public static void main(String[] args) {
    final StyledDocument doc;
    final JTextPane textpane;

    JFrame f = new JFrame();

    f.setTitle("Document Model");

    JToolBar toolbar = new JToolBar();
    JButton boldb = new JButton("bold");
    JButton italb = new JButton("italic");
    JButton strib = new JButton("strike");
    JButton undeb = new JButton("underline");

    toolbar.add(boldb);/*from w w  w  . j a  va  2s .c o  m*/
    toolbar.add(italb);
    toolbar.add(strib);
    toolbar.add(undeb);

    f.add(toolbar, BorderLayout.NORTH);

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

    JScrollPane pane = new JScrollPane();
    textpane = new JTextPane();
    textpane.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));

    doc = textpane.getStyledDocument();

    Style style = textpane.addStyle("Bold", null);
    StyleConstants.setBold(style, true);

    style = textpane.addStyle("Italic", null);
    StyleConstants.setItalic(style, true);

    style = textpane.addStyle("Underline", null);
    StyleConstants.setUnderline(style, true);

    style = textpane.addStyle("Strike", null);
    StyleConstants.setStrikeThrough(style, true);

    boldb.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            doc.setCharacterAttributes(textpane.getSelectionStart(),
                    textpane.getSelectionEnd() - textpane.getSelectionStart(), textpane.getStyle("Bold"),
                    false);
        }
    });

    italb.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            doc.setCharacterAttributes(textpane.getSelectionStart(),
                    textpane.getSelectionEnd() - textpane.getSelectionStart(), textpane.getStyle("Italic"),
                    false);
        }

    });

    strib.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            doc.setCharacterAttributes(textpane.getSelectionStart(),
                    textpane.getSelectionEnd() - textpane.getSelectionStart(), textpane.getStyle("Strike"),
                    false);
        }

    });

    undeb.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            doc.setCharacterAttributes(textpane.getSelectionStart(),
                    textpane.getSelectionEnd() - textpane.getSelectionStart(), textpane.getStyle("Underline"),
                    false);
        }
    });

    pane.getViewport().add(textpane);
    panel.add(pane);

    f.add(panel);

    f.setSize(new Dimension(380, 320));
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

}

From source file:Main.java

public static JTextArea getTextAreaFromJScrollPane(JScrollPane scrollBar) {
    if (scrollBar != null)
        return ((JTextArea) scrollBar.getViewport().getView());
    else {// w w  w .ja va 2 s . c  om
        try {
            throw new Exception("JTextArea received was equal to null!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

From source file:Main.java

public static JTextField getTextFieldFromJScrollPane(JScrollPane scrollBar) {
    if (scrollBar != null)
        return ((JTextField) scrollBar.getViewport().getView());
    else {/*from   w  ww  .j  a  va  2s .  c o  m*/
        try {
            throw new Exception("JTextField received was equal to null!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

From source file:Main.java

public static void makeTransparant(JTable table, JScrollPane scrollPane) {
    table.setOpaque(false);/*from  w  ww  .  ja  va2s .c  o m*/
    ((DefaultTableCellRenderer) table.getDefaultRenderer(Object.class)).setOpaque(false);
    ((DefaultTableCellRenderer) table.getDefaultRenderer(String.class)).setOpaque(false);
    ((JComponent) table.getDefaultRenderer(Boolean.class)).setOpaque(false);

    scrollPane.setOpaque(false);
    scrollPane.getViewport().setOpaque(false);
}

From source file:edu.ku.brc.specify.config.init.secwiz.DatabasePanel.java

/**
 * @param bgColor//from www  .  j a  v a 2s . c  o m
 * @param htmlFileName
 * @return
 */
public static JComponent createHelpPanel(final Color bgColor, final String htmlFileName) {
    Locale currLocale = Locale.getDefault();

    String helpMasterPath = (new File(".")).getAbsolutePath() + File.separator + "../" + "help/securitywiz/"
            + htmlFileName;
    String fullHelpMasterPath = UIHelper.createLocaleName(currLocale, helpMasterPath, "html");

    JEditorPane htmlPane = null;
    try {
        File file = new File(fullHelpMasterPath);
        if (!file.exists()) // for testing
        {
            helpMasterPath = (new File(".")).getAbsolutePath() + File.separator + "help/securitywiz/"
                    + htmlFileName;
            fullHelpMasterPath = UIHelper.createLocaleName(currLocale, helpMasterPath, "html");
            file = new File(fullHelpMasterPath);
            System.out.println(file.getCanonicalPath());
        }
        URI url = file.toURI();

        htmlPane = new JEditorPane(url.toURL()); //$NON-NLS-1$
        htmlPane.setEditable(false);
        htmlPane.setBackground(bgColor);

    } catch (IOException ex) {
        File file = new File(fullHelpMasterPath);
        String htmlDesc = "";
        try {
            htmlDesc = "Error loading help: " + file.getCanonicalPath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        htmlPane = new JEditorPane("text/plain", htmlDesc); //$NON-NLS-1$
    }

    JScrollPane scrollPane = UIHelper.createScrollPane(htmlPane, true);
    scrollPane.setBorder(BorderFactory.createEmptyBorder());
    scrollPane.getViewport().setPreferredSize(new Dimension(400, 400));

    return scrollPane;
}

From source file:Main.java

public Main() throws Exception {
    setSize(400, 240);/*from   www. j a  va2 s . com*/
    JPanel topPanel = new JPanel();
    topPanel.setLayout(new BorderLayout());
    getContentPane().add(topPanel, BorderLayout.CENTER);

    RTFEditorKit rtf = new RTFEditorKit();
    JEditorPane editor = new JEditorPane();
    editor.setEditorKit(rtf);

    JScrollPane scroller = new JScrollPane();
    scroller.getViewport().add(editor);
    topPanel.add(scroller, BorderLayout.CENTER);

    FileInputStream fi = new FileInputStream("test.rtf");
    rtf.read(fi, editor.getDocument(), 0);
}

From source file:SimpleList.java

public SimpleList() {
    super("Simple Swing List");
    setSize(500, 240);/*from  ww w.j a v a 2  s. co  m*/

    String[] item = { "First", "Second", "Third" };

    list = new JList(item);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.getViewport().add(list);
    getContentPane().add(scrollPane, BorderLayout.CENTER);

    WindowListener exit = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(exit);

    setVisible(true);
}