Example usage for javax.swing JViewport setPreferredSize

List of usage examples for javax.swing JViewport setPreferredSize

Introduction

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

Prototype

@BeanProperty(preferred = true, description = "The preferred size of the component.")
public void setPreferredSize(Dimension preferredSize) 

Source Link

Document

Sets the preferred size of this component.

Usage

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  ww. j  a va  2s . 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:MainClass.java

public MainClass() {
    super("Row Header Test");
    setSize(300, 200);//  ww  w  .  j a va  2 s . com
    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  ww w. j  ava 2 s .  co  m
    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.chart.TimeChartPanel.java

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

    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();
        }
    });

}

From source file:com.projity.pm.graphic.spreadsheet.common.CommonSpreadSheet.java

protected void configureScrollPaneHeaders(JScrollPane scrollPane) {
    if (scrollPane instanceof ScaledScrollPane)
        scrollPane.setColumnHeaderView(((ScaledScrollPane) scrollPane).getTimeScaleComponent());
    else/*from  ww  w  . j  ava  2  s .c  om*/
        scrollPane.setColumnHeaderView(getTableHeader());
    JViewport vp = new JViewport();
    vp.setView(rowHeader);
    vp.setPreferredSize(rowHeader.getPreferredSize());
    scrollPane.setRowHeader(vp);
    corner = new SpreadSheetCorner(this);
    scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, corner);
    //scrollPane.setCorner(JScrollPane.LOWER_LEFT_CORNER,new GradientCorner());
}

From source file:com.projity.dialog.assignment.AssignmentEntryPane.java

public void init() {
    if (project == null)
        return;/*w  w  w.  ja va  2s  .  co  m*/
    ResourcePool pool = project.getResourcePool();

    //      if (assignmentModel==null){
    pool.getResourceOutline().getHierarchy().removeHierarchyListener(this);
    pool.getResourceOutline().getHierarchy().addHierarchyListener(this);

    assignmentModel = NodeModelFactory.getInstance().replicate(pool.getResourceOutline(),
            new NodeFactoryTransformer());
    assignmentModel.getHierarchy().setNbEndVoidNodes(0); // don't allow blank lines
    //      }
    if (spreadSheet == null) {
        spreadSheet = new AssignmentSpreadSheet(resourceAssigner);
        spreadSheet.setSpreadSheetCategory(spreadsheetCategory); // for columns.  Must do first
        spreadSheet.setActions(new String[] {});

    }

    cache = NodeModelCacheFactory.getInstance().createDefaultCache(assignmentModel, pool,
            NodeModelCache.ASSIGNMENT_TYPE, "AssignmentEntry", transformerClosure);
    SpreadSheetFieldArray fields = getFields();
    spreadSheet.setCache(cache, fields, fields.getCellStyle(), fields.getActionList());

    // set widths of these columns explicitly
    if (!replace) { //TODO width should come from definition in xml, not hard coded here
        //         spreadSheet.getColumnModel().getColumn(REQUEST_DEMAND_TYPE_COLUMN).setPreferredWidth(50);
        spreadSheet.getColumnModel().getColumn(UNITS_COLUMN).setPreferredWidth(50);
    }

    JViewport viewport = createViewport();
    viewport.setView(spreadSheet);
    setViewport(viewport);

    setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

    Dimension d = spreadSheet.getPreferredSize();
    Dimension enclosing = new Dimension();

    //big awful hack to remove
    GraphicConfiguration config = GraphicConfiguration.getInstance();
    int rowHeaderWidth = config.getRowHeaderWidth() + spreadSheet.getRowMargin() * 2; //should be rowHeader's one

    //      TableColumnModel tm=spreadSheet.getColumnModel();
    //      int w=tm.getTotalColumnWidth();

    enclosing.setSize(d.getWidth()/*+rowHeaderWidth*/, d.getHeight());
    viewport.setPreferredSize(enclosing);

    //setBorder(new EmptyBorder(0,0,0,0));
    updateTable();
}

From source file:app.RunApp.java

/**
 * Fill the table with Chi and Phi coefficients
 * /*from  w  ww.  j  a  v  a2  s . c o m*/
 * @param dataset Multi-label dataset
 */
private void fillTableChiPhiCoefficients(MultiLabelInstances dataset) {
    chiPhiCoefficients = ChartUtils.getChiPhiCoefficients(dataset);
    data = new Object[chiPhiCoefficients.length][chiPhiCoefficients.length + 1];
    column = new Object[data.length + 1];

    for (int i = 0; i < chiPhiCoefficients.length; i++) {
        data[i] = Utils.getValuesByRow(i, chiPhiCoefficients, dataset.getLabelNames()[i]);
    }

    for (int i = 0; i < column.length; i++) {
        if (i == 0) {
            column[i] = "Labels";
        } else {
            column[i] = (dataset.getLabelNames()[i - 1]);
        }
    }

    AbstractTableModel1 fixedModel = new AbstractTableModel1(data, column);
    AbstractTableModel2 model = new AbstractTableModel2(data, column);

    fixedTableChiPhi.setModel(fixedModel);
    jTableChiPhi.setModel(model);

    JScrollPane scroll = new JScrollPane(jTableChiPhi);
    JViewport viewport = new JViewport();
    viewport.setView(fixedTableChiPhi);
    viewport.setPreferredSize(fixedTableChiPhi.getPreferredSize());
    scroll.setRowHeaderView(viewport);

    scroll.setBounds(20, 20, 780, 390);

    scroll.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixedTableChiPhi.getTableHeader());

    jTableChiPhi.setBorder(BorderFactory.createLineBorder(Color.black));

    if (firstTimeChi) {
        panelChiPhi.add(scroll, BorderLayout.CENTER, 0);
        firstTimeChi = false;
        return;
    }

    panelChiPhi.remove(0);
    panelChiPhi.add(scroll, BorderLayout.CENTER, 0);
}

From source file:app.RunApp.java

/**
 * Fill the table with coefficient values
 * /*from   w  w w .ja v a  2s .  co  m*/
 * @param dataset
 * @param pairs
 * @param type 
 */
private void fillTableCoefficients(MultiLabelInstances dataset, String type) {
    double[][] pairLabelValues;

    //coocurrence values table
    if (type.equals("coocurrence")) {
        pairLabelValues = ChartUtils.getCoocurrences(dataset);
        coocurrenceCoefficients = pairLabelValues;
    }
    //heatmap values table
    else {
        pairLabelValues = getHeatMapCoefficients();
        heatmapCoefficients = pairLabelValues.clone();
    }

    /**     **/

    data = new Object[pairLabelValues.length][pairLabelValues.length + 1];
    column = new Object[data.length + 1];

    if (type.equals("coocurrence")) {
        for (int i = 0; i < pairLabelValues.length; i++) {
            for (int j = 0; j < pairLabelValues.length; j++) {

                if (j == 0) {
                    data[i][j] = dataset.getLabelNames()[i];
                } else if (i == (j - 1)) {
                    data[i][j] = "";
                } else if (j > i) {
                    data[i][j] = "";
                } else {
                    if (pairLabelValues[j - 1][i] <= 0.0) {
                        data[i][j] = "";
                    } else {
                        data[i][j] = (int) pairLabelValues[j - 1][i];
                    }
                }
            }
        }
    } else {
        for (int i = 0; i < pairLabelValues.length; i++) {
            for (int j = 0; j < pairLabelValues.length + 1; j++) {

                if (j == 0) {
                    data[i][j] = dataset.getLabelNames()[i];
                } else {
                    if (pairLabelValues[j - 1][i] <= 0.0) {
                        data[i][j] = "";
                    } else {
                        NumberFormat formatter = new DecimalFormat("#0.000");
                        data[i][j] = formatter.format(pairLabelValues[j - 1][i]).replace(",", ".");
                    }
                }
            }
        }
    }

    for (int i = 0; i < column.length; i++) {
        if (i == 0) {
            column[i] = "Labels";
        } else {
            column[i] = (dataset.getLabelNames()[i - 1]);
        }
    }

    AbstractTableModel1 fixedModel = new AbstractTableModel1(data, column);
    AbstractTableModel2 model = new AbstractTableModel2(data, column);

    JTable temp, fixedTable_temp;
    JPanel jpanel_temp;

    if (type.equals("coocurrence")) {
        temp = jTableCoocurrences;
        jpanel_temp = panelCoOcurrenceValues;
        fixedTable_temp = fixedTableCoocurrences;
    } else {
        temp = jTableHeatmap;
        jpanel_temp = panelHeatmapValues;
        fixedTable_temp = fixedTableHeatmap;
    }

    fixedTable_temp.setModel(fixedModel);
    temp.setModel(model);

    JScrollPane scroll = new JScrollPane(temp);
    JViewport viewport = new JViewport();
    viewport.setView(fixedTable_temp);
    viewport.setPreferredSize(fixedTable_temp.getPreferredSize());
    scroll.setRowHeaderView(viewport);

    scroll.setBounds(20, 20, 780, 390);

    scroll.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixedTable_temp.getTableHeader());

    temp.setBorder(BorderFactory.createLineBorder(Color.black));

    jpanel_temp.remove(0);
    jpanel_temp.add(scroll, BorderLayout.CENTER, 0);
}