Example usage for javax.swing JScrollPane setRowHeaderView

List of usage examples for javax.swing JScrollPane setRowHeaderView

Introduction

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

Prototype

public void setRowHeaderView(Component view) 

Source Link

Document

Creates a row-header viewport if necessary, sets its view and then adds the row-header viewport to the scrollpane.

Usage

From source file:Main.java

public Main() {
    super("JScrollPane Demo");
    JScrollPane scrollPane = new JScrollPane(label);

    JLabel[] corners = new JLabel[4];
    for (int i = 0; i < 4; i++) {
        corners[i] = new JLabel();
        corners[i].setBackground(Color.white);
        corners[i].setOpaque(true);//from   w w w .  ja v a2  s  . c  o  m
    }

    JLabel rowheader = new JLabel() {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Rectangle rect = g.getClipBounds();
            for (int i = 50 - (rect.y % 50); i < rect.height; i += 50) {
                g.drawLine(0, rect.y + i, 3, rect.y + i);
                g.drawString("" + (rect.y + i), 6, rect.y + i + 3);
            }
        }

        public Dimension getPreferredSize() {
            return new Dimension(25, (int) label.getPreferredSize().getHeight());
        }
    };
    rowheader.setBackground(Color.white);
    rowheader.setOpaque(true);

    JLabel columnheader = new JLabel() {

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Rectangle r = g.getClipBounds();
            for (int i = 50 - (r.x % 50); i < r.width; i += 50) {
                g.drawLine(r.x + i, 0, r.x + i, 3);
                g.drawString("" + (r.x + i), r.x + i - 10, 16);
            }
        }

        public Dimension getPreferredSize() {
            return new Dimension((int) label.getPreferredSize().getWidth(), 25);
        }
    };
    columnheader.setBackground(Color.white);
    columnheader.setOpaque(true);

    scrollPane.setRowHeaderView(rowheader);
    scrollPane.setColumnHeaderView(columnheader);
    scrollPane.setCorner(JScrollPane.LOWER_LEFT_CORNER, corners[0]);
    scrollPane.setCorner(JScrollPane.LOWER_RIGHT_CORNER, corners[1]);
    scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, corners[2]);
    scrollPane.setCorner(JScrollPane.UPPER_RIGHT_CORNER, corners[3]);

    getContentPane().add(scrollPane);
    setSize(400, 300);
    setVisible(true);
}

From source file:HeaderDemo.java

public HeaderDemo() {
    super("JScrollPane Demo");
    JScrollPane scrollPane = new JScrollPane(label);

    JLabel[] corners = new JLabel[4];
    for (int i = 0; i < 4; i++) {
        corners[i] = new JLabel();
        corners[i].setBackground(Color.white);
        corners[i].setOpaque(true);//w ww.ja v a2  s  . c o m
    }

    JLabel rowheader = new JLabel() {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Rectangle rect = g.getClipBounds();
            for (int i = 50 - (rect.y % 50); i < rect.height; i += 50) {
                g.drawLine(0, rect.y + i, 3, rect.y + i);
                g.drawString("" + (rect.y + i), 6, rect.y + i + 3);
            }
        }

        public Dimension getPreferredSize() {
            return new Dimension(25, (int) label.getPreferredSize().getHeight());
        }
    };
    rowheader.setBackground(Color.white);
    rowheader.setOpaque(true);

    JLabel columnheader = new JLabel() {

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Rectangle r = g.getClipBounds();
            for (int i = 50 - (r.x % 50); i < r.width; i += 50) {
                g.drawLine(r.x + i, 0, r.x + i, 3);
                g.drawString("" + (r.x + i), r.x + i - 10, 16);
            }
        }

        public Dimension getPreferredSize() {
            return new Dimension((int) label.getPreferredSize().getWidth(), 25);
        }
    };
    columnheader.setBackground(Color.white);
    columnheader.setOpaque(true);

    scrollPane.setRowHeaderView(rowheader);
    scrollPane.setColumnHeaderView(columnheader);
    scrollPane.setCorner(JScrollPane.LOWER_LEFT_CORNER, corners[0]);
    scrollPane.setCorner(JScrollPane.LOWER_RIGHT_CORNER, corners[1]);
    scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, corners[2]);
    scrollPane.setCorner(JScrollPane.UPPER_RIGHT_CORNER, corners[3]);

    getContentPane().add(scrollPane);
    setSize(400, 300);
    setVisible(true);
}

From source file:edu.ucla.stat.SOCR.chart.Chart.java

protected void setTablePane() {

    dataPanel.removeAll();//from   www . ja  v  a2 s  .co  m

    JScrollPane tablePanel = new JScrollPane(dataTable);
    tablePanel.setRowHeaderView(headerTable);
    dataPanel.add(tablePanel);

    JScrollPane st = new JScrollPane(summaryPanel);
    st.setPreferredSize(new Dimension(CHART_SIZE_X, CHART_SIZE_Y / 3));

    dataPanel.add(new JScrollPane(st));
    dataPanel.validate();
}

From source file:edu.ucla.stat.SOCR.chart.Chart.java

public void keyPressed(KeyEvent e) {
    //System.out.println("Chart: keyPressed="+e.getKeyCode()+"dataTable.getSelectedRow()="+dataTable.getSelectedRow());
    if (e.getKeyCode() == 127 || e.getKeyCode() == 8) {
        if ((dataTable.getSelectedRow() >= 0) && (dataTable.getSelectedColumn() >= 0)) {
            int[] rows = dataTable.getSelectedRows();
            int[] cols = dataTable.getSelectedColumns();
            for (int i = rows[0]; i <= rows[rows.length - 1]; i++)
                for (int j = cols[0]; j <= cols[cols.length - 1]; j++) {
                    dataTable.setValueAt(null, i, j);
                }//  w ww .ja v  a2s . c o  m
        }
        dataPanel.removeAll();
        JScrollPane tablePanel = new JScrollPane(dataTable);
        tablePanel.setRowHeaderView(headerTable);
        dataPanel.add(tablePanel);
        dataPanel.add(new JScrollPane(summaryPanel));
        dataTable.setGridColor(Color.gray);
        dataTable.setShowGrid(true);
        dataPanel.validate();
    }
}

From source file:edu.ucla.stat.SOCR.chart.Chart.java

public void updateEditableHeader(TableColumnModel aColumnModel) {
    dTableHeader = new EditableHeader(aColumnModel);
    /*for (int i=0; i<aColumnModel.getColumnCount(); i++){
       System.out.println("H "+aColumnModel.getColumn(i).getHeaderValue());
    }*///from   ww w  .  ja  v a2  s.  c  o m
    dTableHeader.setEditingColumn(-1);
    dataTable.setTableHeader(dTableHeader);
    dataTable.validate();

    dataPanel.removeAll();

    JTableHeader corner = headerTable.getTableHeader();
    corner.setReorderingAllowed(false);
    corner.setResizingAllowed(false);
    JScrollPane tablePanel = new JScrollPane(dataTable);
    tablePanel.setRowHeaderView(headerTable);
    tablePanel.setCorner(JScrollPane.UPPER_LEFT_CORNER, corner);
    new JScrollPaneAdjuster(tablePanel);
    new JTableRowHeaderResizer(tablePanel).setEnabled(true);
    dataPanel.add(tablePanel);

    JScrollPane st = new JScrollPane(summaryPanel);
    st.setPreferredSize(new Dimension(CHART_SIZE_X, CHART_SIZE_Y / 3));
    dataPanel.add(new JScrollPane(st));
    dataPanel.validate();
    //System.out.println("table Header updated");
}

From source file:edu.ucla.stat.SOCR.chart.Chart.java

/**
 * make the show_all panel//from  w w w  . j a v a  2s  . c o  m
 */
protected void setMixPanel() {
    dataPanel2.removeAll();
    graphPanel2.removeAll();

    graphPanel2.setPreferredSize(new Dimension(CHART_SIZE_X * 2 / 3, CHART_SIZE_Y * 2 / 3));
    graphPanel2.setLayout(new BoxLayout(graphPanel2, BoxLayout.Y_AXIS));
    if (chartPanel != null)
        graphPanel2.add(chartPanel);
    if (legendPanelOn) {
        if (legendPanel != null)
            legendPanel.setPreferredSize(new Dimension(CHART_SIZE_X * 2 / 3, CHART_SIZE_Y * 2 / 5));
        JScrollPane legendPane = new JScrollPane(legendPanel);

        legendPane.setPreferredSize(new Dimension(CHART_SIZE_X * 2 / 3, CHART_SIZE_Y * 2 / 5));
        graphPanel2.add(legendPane);
    }
    graphPanel2.validate();

    dataPanel2.add(new JLabel(" "));
    dataPanel2.add(new JLabel("Data"));
    JScrollPane dt = new JScrollPane(dataTable);
    dt.setRowHeaderView(headerTable);
    dt.setPreferredSize(new Dimension(CHART_SIZE_X / 3, CHART_SIZE_Y * 3 / 8));

    dataPanel2.add(dt);
    JScrollPane st = new JScrollPane(summaryPanel);
    st.setPreferredSize(new Dimension(CHART_SIZE_X / 3, CHART_SIZE_Y / 6));
    dataPanel2.add(st);
    st.validate();

    dataPanel2.add(new JLabel(" "));
    dataPanel2.add(new JLabel("Mapping"));
    JScrollPane st2 = new JScrollPane(mapPanel);
    st2.setPreferredSize(new Dimension(CHART_SIZE_X / 3, CHART_SIZE_Y / 2));
    dataPanel2.add(st2);

    dataPanel2.validate();

    mixPanel.removeAll();
    mixPanel.add(graphPanel2, BorderLayout.WEST);
    mixPanel.add(new JScrollPane(dataPanel2), BorderLayout.CENTER);
    mixPanel.validate();
}

From source file:edu.ucla.stat.SOCR.chart.Chart.java

public void updateExample(ChartExampleData example) {
    //  reset();/*from   w  w w.j  a  v  a  2  s.  c o m*/

    hasExample = true;
    JTable tempDataTable = example.getExample();

    /* if(tempDataTable.getRowCount()>dataTable.getRowCount())
    appendTableRows(tempDataTable.getRowCount()-dataTable.getRowCount());
       else   */
    resetTableRows(tempDataTable.getRowCount());

    for (int i = 0; i < tempDataTable.getColumnCount(); i++) {
        columnModel.getColumn(i).setHeaderValue(tempDataTable.getColumnName(i));
        //  System.out.println("updateExample tempDataTable["+i+"] = " +tempDataTable.getColumnName(i));
    }
    dTableHeader = new EditableHeader(columnModel);
    dataTable.setTableHeader(dTableHeader);

    for (int i = 0; i < tempDataTable.getRowCount(); i++)
        for (int j = 0; j < tempDataTable.getColumnCount(); j++) {
            dataTable.setValueAt(tempDataTable.getValueAt(i, j), i, j);
        }
    dataPanel.removeAll();
    JScrollPane tablePanel = new JScrollPane(dataTable);
    tablePanel.setRowHeaderView(headerTable);
    dataPanel.add(tablePanel);
    dataPanel.add(new JScrollPane(summaryPanel));
    dataTable.setGridColor(Color.gray);
    dataTable.setShowGrid(true);

    // this is a fix for the BAD SGI Java VM - not up to date as of dec. 22, 2003
    try {
        dataTable.setDragEnabled(true);
    } catch (Exception e) {
    }

    dataPanel.validate();
    //updateStatus("Example updated, please redo the mapping and update the chart");
}

From source file:edu.ucla.stat.SOCR.chart.Chart.java

/**
 * clear data panel//  w  w w  .  j  av  a2  s  .  c o  m
 *
 */
public void resetTable() {

    //   System.out.println("resetTable get called");
    for (int i = 0; i < columnNumber; i++)
        columnNames[i] = new String(DEFAULT_HEADER + (i + 1));

    tModel = new javax.swing.table.DefaultTableModel(dataObject, columnNames);
    hModel = new DefaultTableModel(0, 1);

    for (int i = 0; i < tModel.getRowCount(); i++)
        hModel.addRow(new Object[] { (i + 1) + ":" });

    dataTable = new JTable(tModel);
    /*   System.out.println("here2");
       dataTable.getTableHeader().addMouseListener(new MouseAdapter(){  
           public void mouseClicked(MouseEvent e) {  
     TableColumnModel colModel = dataTable.getColumnModel();
       int columnModelIndex = colModel.getColumnIndexAtX(e.getX());
       selectedHeaderColumnIndex = colModel.getColumn(columnModelIndex)
           .getModelIndex();
               
        System.out.println("mouseClicked haeder "+selectedHeaderColumnIndex);
     }  
       });*/
    /*final JTableHeader heading = dataTable.getTableHeader();
    heading.addMouseListener(new MouseAdapter(){  
        public void mouseClicked(MouseEvent e) {  
           selectedHeaderColumnIndex = heading.columnAtPoint(e.getPoint());  
           System.out.println("mouseClicked haeder "+selectedHeaderColumnIndex);
        }  
     });  */
    dataTable.setGridColor(Color.gray);
    dataTable.setShowGrid(true);
    dataTable.doLayout();
    dataTable.setCellSelectionEnabled(true);
    dataTable.setColumnSelectionAllowed(true);
    dataTable.setRowSelectionAllowed(true);

    columnModel = dataTable.getColumnModel();
    dTableHeader = new EditableHeader(columnModel);
    dataTable.setTableHeader(dTableHeader);

    listIndex = new int[dataTable.getColumnCount()];
    for (int j = 0; j < listIndex.length; j++)
        listIndex[j] = 1;

    hookTableAction();

    headerTable = new JTable(hModel);
    headerTable.setCellSelectionEnabled(false);
    LookAndFeel.installColorsAndFont(headerTable, "TableHeader.background", "TableHeader.foreground",
            "TableHeader.font");
    headerTable.setIntercellSpacing(new Dimension(0, 0));
    Dimension d = headerTable.getPreferredScrollableViewportSize();
    d.width = headerTable.getPreferredSize().width;
    headerTable.setPreferredScrollableViewportSize(d);
    headerTable.setRowHeight(dataTable.getRowHeight());
    headerTable.setDefaultRenderer(Object.class, new RowHeaderRenderer());
    JTableHeader corner = headerTable.getTableHeader();
    corner.setReorderingAllowed(false);
    corner.setResizingAllowed(false);

    //      summaryPanel.setPreferredSize(new Dimension(CHART_SIZE_X,CHART_SIZE_Y/2));
    summaryPanel.removeAll();

    dataPanel.removeAll();
    JScrollPane tablePanel = new JScrollPane(dataTable);
    tablePanel.setRowHeaderView(headerTable);
    tablePanel.setCorner(JScrollPane.UPPER_LEFT_CORNER, corner);
    new JScrollPaneAdjuster(tablePanel);
    new JTableRowHeaderResizer(tablePanel).setEnabled(true);
    dataPanel.add(tablePanel);

    JScrollPane st = new JScrollPane(summaryPanel);
    st.setPreferredSize(new Dimension(CHART_SIZE_X, CHART_SIZE_Y / 3));
    dataPanel.add(new JScrollPane(st));
    dataPanel.validate();

}

From source file:edu.ucla.stat.SOCR.chart.Chart.java

/**
 * setup GUI layout for the data panel including datatable and summary textArea
 *
 *///  ww  w .j a v  a  2s .c  om
protected void initTable() {

    tModel = new DefaultTableModel(dataObject, columnNames);
    hModel = new DefaultTableModel(0, 1);

    dataTable = new JTable(tModel);
    /*   System.out.println("here");
       dataTable.getTableHeader().addMouseListener(new MouseAdapter(){  
           public void mouseClicked(MouseEvent e) {  
     TableColumnModel colModel = dataTable.getColumnModel();
       int columnModelIndex = colModel.getColumnIndexAtX(e.getX());
       selectedHeaderColumnIndex = colModel.getColumn(columnModelIndex)
           .getModelIndex();
               
        System.out.println("mouseClicked haeder "+selectedHeaderColumnIndex);
     }  
       });*/

    /*final JTableHeader heading = dataTable.getTableHeader();
    dataTable.addMouseListener(new MouseAdapter(){  
        public void mouseClicked(MouseEvent e) {  
           selectedHeaderColumnIndex = heading.columnAtPoint(e.getPoint());  
           System.out.println("mouseClicked haeder "+selectedHeaderColumnIndex);
        }  
     });  */
    dataTable.addKeyListener(this);
    dataTable.setGridColor(Color.gray);
    dataTable.setShowGrid(true);

    for (int i = 0; i < tModel.getRowCount(); i++)
        hModel.addRow(new Object[] { (i + 1) + ":" });

    headerTable = new JTable(hModel);

    headerTable.setCellSelectionEnabled(false);
    headerTable.setEnabled(false);
    headerTable.setGridColor(Color.gray);
    headerTable.setShowGrid(true);
    LookAndFeel.installColorsAndFont(headerTable, "TableHeader.background", "TableHeader.foreground",
            "TableHeader.font");
    headerTable.setIntercellSpacing(new Dimension(0, 0));
    Dimension d = headerTable.getPreferredScrollableViewportSize();
    d.width = headerTable.getPreferredSize().width;
    headerTable.setPreferredScrollableViewportSize(d);
    headerTable.setRowHeight(dataTable.getRowHeight());
    headerTable.setDefaultRenderer(Object.class, new RowHeaderRenderer());

    JTableHeader corner = headerTable.getTableHeader();
    corner.setReorderingAllowed(false);
    corner.setResizingAllowed(false);

    // this is a fix for the BAD SGI Java VM - not up to date as of dec. 22, 2003
    try {
        dataTable.setDragEnabled(true);
    } catch (Exception e) {
    }
    columnModel = dataTable.getColumnModel();
    dTableHeader = new EditableHeader(columnModel);
    dataTable.setTableHeader(dTableHeader);
    dataTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    dataTable.setCellSelectionEnabled(true);
    dataTable.setColumnSelectionAllowed(true);
    dataTable.setRowSelectionAllowed(true);

    hookTableAction();

    dataPanel = new JPanel();

    summaryPanel = new JTextArea();
    //      summaryPanel.setPreferredSize(new Dimension(CHART_SIZE_X,CHART_SIZE_Y/2));
    Color bg = dataPanel.getBackground();
    summaryPanel.setBackground(bg);
    summaryPanel.setEditable(false);
    summaryPanel.setForeground(Color.BLACK);

    dataPanel.setLayout(new BoxLayout(dataPanel, BoxLayout.Y_AXIS));
    JScrollPane tablePanel = new JScrollPane(dataTable);

    tablePanel.setRowHeaderView(headerTable);
    tablePanel.setCorner(JScrollPane.UPPER_LEFT_CORNER, corner);
    new JScrollPaneAdjuster(tablePanel);

    new JTableRowHeaderResizer(tablePanel).setEnabled(true);

    dataPanel.add(tablePanel);
    JScrollPane st = new JScrollPane(summaryPanel);
    st.setPreferredSize(new Dimension(CHART_SIZE_X, CHART_SIZE_Y / 3));
    dataPanel.add(new JScrollPane(st));

    dataPanel.validate();
}

From source file:fs.MainWindow.java

public void GenerateTable(Object[][] data, String title, String subtitle, final String[] titles) {
    JFrame window = new JFrame(title);
    window.setLayout(new java.awt.BorderLayout(5, 5));
    JLabel label = new JLabel(subtitle);
    label.setFont(new java.awt.Font("Tahoma", 1, 12));
    label.setForeground(new java.awt.Color(0, 0, 255));
    label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    label.setOpaque(true);/*from  w  ww .  j av  a2  s.  c o m*/
    label.setPreferredSize(new java.awt.Dimension(50, 30));

    ListModel lm = new AbstractListModel() {

        String headers[] = titles;

        @Override
        public int getSize() {
            return headers.length;
        }

        @Override
        public Object getElementAt(int index) {
            return headers[index];
        }
    };

    //        String[] titles = getClasses();
    JList rowHeader = new JList(lm);
    rowHeader.setFixedCellWidth(50);
    JTable jTable = new JTable(data, titles);
    rowHeader.setFixedCellHeight(jTable.getRowHeight());
    rowHeader.setCellRenderer(new RowHeaderRenderer(jTable));
    JScrollPane scroll = new JScrollPane(jTable);
    scroll.setRowHeaderView(rowHeader);
    getContentPane().add(scroll, BorderLayout.CENTER);

    jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    jTable.setAlignmentX(JTable.CENTER_ALIGNMENT);

    window.add(label, java.awt.BorderLayout.NORTH);
    window.add(scroll, java.awt.BorderLayout.CENTER);

    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    window.setBounds((screenSize.width - 800) / 2, (screenSize.height - 270) / 2, 800, 270);
    window.setVisible(true);
}