Example usage for javax.swing JViewport setView

List of usage examples for javax.swing JViewport setView

Introduction

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

Prototype

public void setView(Component view) 

Source Link

Document

Sets the JViewport's one lightweight child (view), which can be null.

Usage

From source file:MainClass.java

public static void main(final String args[]) {
    final Object rows[][] = { { "one", "1" }, { "two", "2" }, { "three", "3" } };
    final Object headers[] = { "English", "Digit" };

    JFrame frame = new JFrame("Scrollless Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTable table = new JTable(rows, headers);

    JScrollPane scrollPane = new JScrollPane(table);
    JViewport viewport = new JViewport();
    viewport.setView(table);
    scrollPane.setColumnHeaderView(new JLabel("table header here"));
    scrollPane.setRowHeaderView(viewport);

    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);//w w  w  .j a v a 2 s .c o m
    frame.setVisible(true);
}

From source file:FixedColumnModel.java

public static void main(String args[]) {

    final Object rowData[][] = { { "1", "one", "I" }, { "2", "two", "II" }, { "3", "three", "III" } };

    final String columnNames[] = { "#", "English", "Roman" };

    final TableModel fixedColumnModel = new AbstractTableModel() {
        public int getColumnCount() {
            return 1;
        }/*from   w w  w. j  a  va 2 s.  co m*/

        public String getColumnName(int column) {
            return columnNames[column];
        }

        public int getRowCount() {
            return rowData.length;
        }

        public Object getValueAt(int row, int column) {
            return rowData[row][column];
        }
    };

    final TableModel mainModel = new AbstractTableModel() {
        public int getColumnCount() {
            return columnNames.length - 1;
        }

        public String getColumnName(int column) {
            return columnNames[column + 1];
        }

        public int getRowCount() {
            return rowData.length;
        }

        public Object getValueAt(int row, int column) {
            return rowData[row][column + 1];
        }
    };

    JTable fixedTable = new JTable(fixedColumnModel);
    fixedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    JTable mainTable = new JTable(mainModel);
    mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    ListSelectionModel model = fixedTable.getSelectionModel();
    mainTable.setSelectionModel(model);

    JScrollPane scrollPane = new JScrollPane(mainTable);
    Dimension fixedSize = fixedTable.getPreferredSize();
    JViewport viewport = new JViewport();
    viewport.setView(fixedTable);
    viewport.setPreferredSize(fixedSize);
    viewport.setMaximumSize(fixedSize);
    scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixedTable.getTableHeader());
    scrollPane.setRowHeaderView(viewport);

    JFrame frame = new JFrame("Fixed Column Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 150);
    frame.setVisible(true);
}

From source file:MoveViewSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("JViewport Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Icon icon = new ImageIcon("yourFile.gif");
    JLabel dogLabel = new JLabel(icon);
    JViewport viewport = new JViewport();
    viewport.setView(dogLabel);
    InputMap inputMap = viewport.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap actionMap = viewport.getActionMap();

    Action upKeyAction = new MoveAction(viewport, DECREASE, Y_AXIS, UNIT);
    KeyStroke upKey = KeyStroke.getKeyStroke("UP");
    inputMap.put(upKey, "up");
    actionMap.put("up", upKeyAction);

    Action downKeyAction = new MoveAction(viewport, INCREASE, Y_AXIS, UNIT);
    KeyStroke downKey = KeyStroke.getKeyStroke("DOWN");
    inputMap.put(downKey, "down");
    actionMap.put("down", downKeyAction);

    Action leftKeyAction = new MoveAction(viewport, DECREASE, X_AXIS, UNIT);
    KeyStroke leftKey = KeyStroke.getKeyStroke("LEFT");
    inputMap.put(leftKey, "left");
    actionMap.put("left", leftKeyAction);

    Action rightKeyAction = new MoveAction(viewport, INCREASE, X_AXIS, UNIT);
    KeyStroke rightKey = KeyStroke.getKeyStroke("RIGHT");
    inputMap.put(rightKey, "right");
    actionMap.put("right", rightKeyAction);

    Action pgUpKeyAction = new MoveAction(viewport, DECREASE, Y_AXIS, BLOCK);
    KeyStroke pgUpKey = KeyStroke.getKeyStroke("PAGE_UP");
    inputMap.put(pgUpKey, "pgUp");
    actionMap.put("pgUp", pgUpKeyAction);

    Action pgDnKeyAction = new MoveAction(viewport, INCREASE, Y_AXIS, BLOCK);
    KeyStroke pgDnKey = KeyStroke.getKeyStroke("PAGE_DOWN");
    inputMap.put(pgDnKey, "pgDn");
    actionMap.put("pgDn", pgDnKeyAction);

    Action shiftPgUpKeyAction = new MoveAction(viewport, DECREASE, X_AXIS, BLOCK);
    KeyStroke shiftPgUpKey = KeyStroke.getKeyStroke("shift PAGE_UP");
    inputMap.put(shiftPgUpKey, "shiftPgUp");
    actionMap.put("shiftPgUp", shiftPgUpKeyAction);

    Action shiftPgDnKeyAction = new MoveAction(viewport, INCREASE, X_AXIS, BLOCK);
    KeyStroke shiftPgDnKey = KeyStroke.getKeyStroke("shift PAGE_DOWN");
    inputMap.put(shiftPgDnKey, "shiftPgDn");
    actionMap.put("shiftPgDn", shiftPgDnKeyAction);

    frame.add(viewport, BorderLayout.CENTER);
    frame.setSize(300, 200);/*  ww  w  .j ava 2s.  c  o  m*/
    frame.setVisible(true);
}

From source file:MoveViewSample.java

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

    Icon icon = new ImageIcon("dog.jpg");
    JLabel dogLabel = new JLabel(icon);
    JViewport viewport = new JViewport();
    viewport.setView(dogLabel);

    InputMap inputMap = viewport.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap actionMap = viewport.getActionMap();

    // Up key moves view up unit
    Action upKeyAction = new MoveAction(viewport, DECREASE, Y_AXIS, UNIT);
    KeyStroke upKey = KeyStroke.getKeyStroke("UP");
    inputMap.put(upKey, "up");
    actionMap.put("up", upKeyAction);

    // Down key moves view down unit
    Action downKeyAction = new MoveAction(viewport, INCREASE, Y_AXIS, UNIT);
    KeyStroke downKey = KeyStroke.getKeyStroke("DOWN");
    inputMap.put(downKey, "down");
    actionMap.put("down", downKeyAction);

    // Left key moves view left unit
    Action leftKeyAction = new MoveAction(viewport, DECREASE, X_AXIS, UNIT);
    KeyStroke leftKey = KeyStroke.getKeyStroke("LEFT");
    inputMap.put(leftKey, "left");
    actionMap.put("left", leftKeyAction);

    // Right key moves view right unit
    Action rightKeyAction = new MoveAction(viewport, INCREASE, X_AXIS, UNIT);
    KeyStroke rightKey = KeyStroke.getKeyStroke("RIGHT");
    inputMap.put(rightKey, "right");
    actionMap.put("right", rightKeyAction);

    // PgUp key moves view up block
    Action pgUpKeyAction = new MoveAction(viewport, DECREASE, Y_AXIS, BLOCK);
    KeyStroke pgUpKey = KeyStroke.getKeyStroke("PAGE_UP");
    inputMap.put(pgUpKey, "pgUp");
    actionMap.put("pgUp", pgUpKeyAction);

    // PgDn key moves view down block
    Action pgDnKeyAction = new MoveAction(viewport, INCREASE, Y_AXIS, BLOCK);
    KeyStroke pgDnKey = KeyStroke.getKeyStroke("PAGE_DOWN");
    inputMap.put(pgDnKey, "pgDn");
    actionMap.put("pgDn", pgDnKeyAction);

    // Shift-PgUp key moves view left block
    Action shiftPgUpKeyAction = new MoveAction(viewport, DECREASE, X_AXIS, BLOCK);
    KeyStroke shiftPgUpKey = KeyStroke.getKeyStroke("shift PAGE_UP");
    inputMap.put(shiftPgUpKey, "shiftPgUp");
    actionMap.put("shiftPgUp", shiftPgUpKeyAction);

    // Shift-PgDn key moves view right block
    Action shiftPgDnKeyAction = new MoveAction(viewport, INCREASE, X_AXIS, BLOCK);
    KeyStroke shiftPgDnKey = KeyStroke.getKeyStroke("shift PAGE_DOWN");
    inputMap.put(shiftPgDnKey, "shiftPgDn");
    actionMap.put("shiftPgDn", shiftPgDnKeyAction);

    Container contentPane = frame.getContentPane();
    contentPane.add(viewport, BorderLayout.CENTER);
    frame.setSize(300, 200);//from w ww .  j a v a 2 s.com
    frame.setVisible(true);
}

From source file:Main.java

public Main() {
    JViewport viewport = new MyViewport();
    viewport.setView(new MyPanel());
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setViewport(viewport);/*from  ww  w.j a  va 2 s .  c  om*/
    add(scrollPane);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack();
    setVisible(true);
}

From source file:ScrollDemo2.java

public void init() {
    JRadioButton form[][] = new JRadioButton[12][5];
    String counts[] = { "", "0-1", "2-5", "6-10", "11-100", "101+" };
    String categories[] = { "Household", "Office", "Extended Family", "Company (US)", "Company (World)", "Team",
            "Will", "Birthday Card List", "High School", "Country", "Continent", "Planet" };
    JPanel p = new JPanel();
    p.setSize(600, 400);//from ww  w  .  ja v  a  2  s  .com
    p.setLayout(new GridLayout(13, 6, 10, 0));
    for (int row = 0; row < 13; row++) {
        ButtonGroup bg = new ButtonGroup();
        for (int col = 0; col < 6; col++) {
            if (row == 0) {
                p.add(new JLabel(counts[col]));
            } else {
                if (col == 0) {
                    p.add(new JLabel(categories[row - 1]));
                } else {
                    form[row - 1][col - 1] = new JRadioButton();
                    bg.add(form[row - 1][col - 1]);
                    p.add(form[row - 1][col - 1]);
                }
            }
        }
    }
    scrollpane = new JScrollPane(p);

    // Add in some JViewports for the column and row headers
    JViewport jv1 = new JViewport();
    jv1.setView(new JLabel(new ImageIcon("columnlabel.gif")));
    scrollpane.setColumnHeader(jv1);
    JViewport jv2 = new JViewport();
    jv2.setView(new JLabel(new ImageIcon("rowlabel.gif")));
    scrollpane.setRowHeader(jv2);

    // And throw in an information button
    JButton jb1 = new JButton(new ImageIcon("question.gif"));
    jb1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JOptionPane.showMessageDialog(null, "This is an Active Corner!", "Information",
                    JOptionPane.INFORMATION_MESSAGE);
        }
    });
    scrollpane.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, jb1);
    getContentPane().add(scrollpane, BorderLayout.CENTER);
}

From source file:MainClass.java

public MainClass() {
    super("Row Header Test");
    setSize(300, 200);//from  ww  w.j  av a  2  s  .c  om
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    TableModel tm = new AbstractTableModel() {
        String data[] = { "", "a", "b", "c", "d", "e" };

        String headers[] = { "Row #", "Column 1", "Column 2", "Column 3", "Column 4", "Column 5" };

        public int getColumnCount() {
            return data.length;
        }

        public int getRowCount() {
            return 1000;
        }

        public String getColumnName(int col) {
            return headers[col];
        }

        public Object getValueAt(int row, int col) {
            return data[col] + row;
        }
    };

    TableColumnModel cm = new DefaultTableColumnModel() {
        boolean first = true;

        public void addColumn(TableColumn tc) {
            if (first) {
                first = false;
                return;
            }
            tc.setMinWidth(150);
            super.addColumn(tc);
        }
    };

    TableColumnModel rowHeaderModel = new DefaultTableColumnModel() {
        boolean first = true;

        public void addColumn(TableColumn tc) {
            if (first) {
                tc.setMaxWidth(tc.getPreferredWidth());
                super.addColumn(tc);
                first = false;
            }
        }
    };

    JTable jt = new JTable(tm, cm);
    JTable headerColumn = new JTable(tm, rowHeaderModel);
    jt.createDefaultColumnsFromModel();
    headerColumn.createDefaultColumnsFromModel();

    jt.setSelectionModel(headerColumn.getSelectionModel());

    headerColumn.setBackground(Color.lightGray);
    headerColumn.setColumnSelectionAllowed(false);
    headerColumn.setCellSelectionEnabled(false);

    JViewport jv = new JViewport();
    jv.setView(headerColumn);
    jv.setPreferredSize(headerColumn.getMaximumSize());

    jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    JScrollPane jsp = new JScrollPane(jt);
    jsp.setRowHeader(jv);
    jsp.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, headerColumn.getTableHeader());
    getContentPane().add(jsp, BorderLayout.CENTER);
}

From source file:RowHeaderTable.java

public RowHeaderTable() {
    super("Row Header Test");
    setSize(300, 200);/*from  w w w  .  j a  v  a 2  s.c  om*/
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    TableModel tm = new AbstractTableModel() {
        String data[] = { "", "a", "b", "c", "d", "e" };

        String headers[] = { "Row #", "Column 1", "Column 2", "Column 3", "Column 4", "Column 5" };

        public int getColumnCount() {
            return data.length;
        }

        public int getRowCount() {
            return 1000;
        }

        public String getColumnName(int col) {
            return headers[col];
        }

        // Synthesize some entries using the data values & the row #
        public Object getValueAt(int row, int col) {
            return data[col] + row;
        }
    };

    // Create a column model for the main table. This model ignores the
    // first
    // column added, and sets a minimum width of 150 pixels for all others.
    TableColumnModel cm = new DefaultTableColumnModel() {
        boolean first = true;

        public void addColumn(TableColumn tc) {
            // Drop the first column . . . that'll be the row header
            if (first) {
                first = false;
                return;
            }
            tc.setMinWidth(150); // just for looks, really...
            super.addColumn(tc);
        }
    };

    // Create a column model that will serve as our row header table. This
    // model picks a maximum width and only stores the first column.
    TableColumnModel rowHeaderModel = new DefaultTableColumnModel() {
        boolean first = true;

        public void addColumn(TableColumn tc) {
            if (first) {
                tc.setMaxWidth(tc.getPreferredWidth());
                super.addColumn(tc);
                first = false;
            }
            // Drop the rest of the columns . . . this is the header column
            // only
        }
    };

    JTable jt = new JTable(tm, cm);

    // Set up the header column and get it hooked up to everything
    JTable headerColumn = new JTable(tm, rowHeaderModel);
    jt.createDefaultColumnsFromModel();
    headerColumn.createDefaultColumnsFromModel();

    // Make sure that selections between the main table and the header stay
    // in sync (by sharing the same model)
    jt.setSelectionModel(headerColumn.getSelectionModel());

    // Make the header column look pretty
    //headerColumn.setBorder(BorderFactory.createEtchedBorder());
    headerColumn.setBackground(Color.lightGray);
    headerColumn.setColumnSelectionAllowed(false);
    headerColumn.setCellSelectionEnabled(false);

    // Put it in a viewport that we can control a bit
    JViewport jv = new JViewport();
    jv.setView(headerColumn);
    jv.setPreferredSize(headerColumn.getMaximumSize());

    // With out shutting off autoResizeMode, our tables won't scroll
    // correctly (horizontally, anyway)
    jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    // We have to manually attach the row headers, but after that, the
    // scroll
    // pane keeps them in sync
    JScrollPane jsp = new JScrollPane(jt);
    jsp.setRowHeader(jv);
    jsp.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, headerColumn.getTableHeader());
    getContentPane().add(jsp, BorderLayout.CENTER);
}

From source file:com.projity.pm.graphic.views.TreeView.java

public void init(ReferenceNodeModelCache cache, NodeModel model, String viewName, Closure transformerClosure) {
    tree = new Xbs(project, viewName);
    this.viewName = viewName;
    this.cache = NodeModelCacheFactory.getInstance()
            .createAntiAssignmentFilteredCache((ReferenceNodeModelCache) cache, viewName, transformerClosure);
    tree.setCache(this.cache);
    tree.setBarStyles((BarStyles) Dictionary.get(BarStyles.category, viewName));

    JViewport viewport = createViewport();
    viewport.setView(tree);
    setViewport(viewport);/*from w  ww  . ja  v a 2  s . co m*/
    cache.update(); //this is not required by certain views 
    HelpUtil.addDocHelp(this, viewName == MenuActionConstants.ACTION_RBS ? "RBS_Chart" : "WBS_Chart");
    //tree.insertCacheData();
}

From source file:com.projity.pm.graphic.chart.TimeChartPanel.java

public void configureScrollPaneHeaders(JScrollPane scrollPane, JComponent rowHeader) {
    viewport = scrollPane.getViewport();
    if (viewport == null || viewport.getView() != this)
        return;/*from  w  w w . j  a v  a 2 s.c om*/

    JViewport vp = new JViewport();
    vp.setView(rowHeader);
    vp.setPreferredSize(rowHeader.getPreferredSize());
    scrollPane.setRowHeader(vp);

    scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, new ChartCorner(this));

    Border border = scrollPane.getBorder();
    if (border == null || border instanceof UIResource) {
        scrollPane.setBorder(UIManager.getBorder("Table.scrollPaneBorder"));
    }

    // left scale synchro
    viewport.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            updateTimeScaleComponentSize();
        }
    });

}