Example usage for javax.swing JTable setFillsViewportHeight

List of usage examples for javax.swing JTable setFillsViewportHeight

Introduction

In this page you can find the example usage for javax.swing JTable setFillsViewportHeight.

Prototype

@BeanProperty(description = "Whether or not this table is always made large enough to fill the height of an enclosing viewport")
public void setFillsViewportHeight(boolean fillsViewportHeight) 

Source Link

Document

Sets whether or not this table is always made large enough to fill the height of an enclosing viewport.

Usage

From source file:Main.java

public Main() {
    super(new GridLayout(1, 0));

    final String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" };

    final Object[][] data = { { "Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false) },
            { "Alison", "Huml", "Rowing", new Integer(3), new Boolean(true) },
            { "Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false) },
            { "Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true) },
            { "Philip", "Milne", "Pool", new Integer(10), new Boolean(false) } };

    final JTable table = new JTable(data, columnNames);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);

    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    if (ALLOW_ROW_SELECTION) { // true by default
        ListSelectionModel rowSM = table.getSelectionModel();
        rowSM.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                // Ignore extra messages.
                if (e.getValueIsAdjusting())
                    return;

                ListSelectionModel lsm = (ListSelectionModel) e.getSource();
                if (lsm.isSelectionEmpty()) {
                    System.out.println("No rows are selected.");
                } else {
                    int selectedRow = lsm.getMinSelectionIndex();
                    System.out.println("Row " + selectedRow + " is now selected.");
                }//from   w ww  . j  a  v a2 s  .c  om
            }
        });
    } else {
        table.setRowSelectionAllowed(false);
    }

    if (ALLOW_COLUMN_SELECTION) { // false by default
        if (ALLOW_ROW_SELECTION) {
            // We allow both row and column selection, which
            // implies that we *really* want to allow individual
            // cell selection.
            table.setCellSelectionEnabled(true);
        }
        table.setColumnSelectionAllowed(true);
        ListSelectionModel colSM = table.getColumnModel().getSelectionModel();
        colSM.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                // Ignore extra messages.
                if (e.getValueIsAdjusting())
                    return;

                ListSelectionModel lsm = (ListSelectionModel) e.getSource();
                if (lsm.isSelectionEmpty()) {
                    System.out.println("No columns are selected.");
                } else {
                    int selectedCol = lsm.getMinSelectionIndex();
                    System.out.println("Column " + selectedCol + " is now selected.");
                }
            }
        });
    }

    if (DEBUG) {
        table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                printDebugData(table);
            }
        });
    }

    // Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    // Add the scroll pane to this panel.
    add(scrollPane);
}

From source file:SimpleTableSelectionDemo.java

public SimpleTableSelectionDemo() {
    super(new GridLayout(1, 0));

    final String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" };

    final Object[][] data = { { "Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false) },
            { "Alison", "Huml", "Rowing", new Integer(3), new Boolean(true) },
            { "Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false) },
            { "Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true) },
            { "Philip", "Milne", "Pool", new Integer(10), new Boolean(false) } };

    final JTable table = new JTable(data, columnNames);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);

    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    if (ALLOW_ROW_SELECTION) { // true by default
        ListSelectionModel rowSM = table.getSelectionModel();
        rowSM.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                // Ignore extra messages.
                if (e.getValueIsAdjusting())
                    return;

                ListSelectionModel lsm = (ListSelectionModel) e.getSource();
                if (lsm.isSelectionEmpty()) {
                    System.out.println("No rows are selected.");
                } else {
                    int selectedRow = lsm.getMinSelectionIndex();
                    System.out.println("Row " + selectedRow + " is now selected.");
                }// w w  w . j a  v  a2 s.  c  o  m
            }
        });
    } else {
        table.setRowSelectionAllowed(false);
    }

    if (ALLOW_COLUMN_SELECTION) { // false by default
        if (ALLOW_ROW_SELECTION) {
            // We allow both row and column selection, which
            // implies that we *really* want to allow individual
            // cell selection.
            table.setCellSelectionEnabled(true);
        }
        table.setColumnSelectionAllowed(true);
        ListSelectionModel colSM = table.getColumnModel().getSelectionModel();
        colSM.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                // Ignore extra messages.
                if (e.getValueIsAdjusting())
                    return;

                ListSelectionModel lsm = (ListSelectionModel) e.getSource();
                if (lsm.isSelectionEmpty()) {
                    System.out.println("No columns are selected.");
                } else {
                    int selectedCol = lsm.getMinSelectionIndex();
                    System.out.println("Column " + selectedCol + " is now selected.");
                }
            }
        });
    }

    if (DEBUG) {
        table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                printDebugData(table);
            }
        });
    }

    // Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    // Add the scroll pane to this panel.
    add(scrollPane);
}

From source file:components.SimpleTableSelectionDemo.java

public SimpleTableSelectionDemo() {
    super(new GridLayout(1, 0));

    final String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" };

    final Object[][] data = { { "Kathy", "Smith", "Snowboarding", new Integer(5), new Boolean(false) },
            { "John", "Doe", "Rowing", new Integer(3), new Boolean(true) },
            { "Sue", "Black", "Knitting", new Integer(2), new Boolean(false) },
            { "Jane", "White", "Speed reading", new Integer(20), new Boolean(true) },
            { "Joe", "Brown", "Pool", new Integer(10), new Boolean(false) } };

    final JTable table = new JTable(data, columnNames);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);

    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    if (ALLOW_ROW_SELECTION) { // true by default
        ListSelectionModel rowSM = table.getSelectionModel();
        rowSM.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                //Ignore extra messages.
                if (e.getValueIsAdjusting())
                    return;

                ListSelectionModel lsm = (ListSelectionModel) e.getSource();
                if (lsm.isSelectionEmpty()) {
                    System.out.println("No rows are selected.");
                } else {
                    int selectedRow = lsm.getMinSelectionIndex();
                    System.out.println("Row " + selectedRow + " is now selected.");
                }/*  ww  w  . java  2s  . com*/
            }
        });
    } else {
        table.setRowSelectionAllowed(false);
    }

    if (ALLOW_COLUMN_SELECTION) { // false by default
        if (ALLOW_ROW_SELECTION) {
            //We allow both row and column selection, which
            //implies that we *really* want to allow individual
            //cell selection.
            table.setCellSelectionEnabled(true);
        }
        table.setColumnSelectionAllowed(true);
        ListSelectionModel colSM = table.getColumnModel().getSelectionModel();
        colSM.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e) {
                //Ignore extra messages.
                if (e.getValueIsAdjusting())
                    return;

                ListSelectionModel lsm = (ListSelectionModel) e.getSource();
                if (lsm.isSelectionEmpty()) {
                    System.out.println("No columns are selected.");
                } else {
                    int selectedCol = lsm.getMinSelectionIndex();
                    System.out.println("Column " + selectedCol + " is now selected.");
                }
            }
        });
    }

    if (DEBUG) {
        table.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                printDebugData(table);
            }
        });
    }

    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    //Add the scroll pane to this panel.
    add(scrollPane);
}

From source file:integratedprogressdemo.WeatherData.java

private WeatherData(boolean displayProgress) {
    setBackground(Color.WHITE);/*  w  w w.  j  a v  a 2 s. c  om*/
    setLayout(new BorderLayout());
    JLabel lbl = new JLabel("World-Wide Weather Data");
    lbl.setFont(new Font("Serif", Font.PLAIN, 18));

    add(lbl, BorderLayout.PAGE_START);
    lbl = new JLabel("Weather information from over 50 cities");
    add(lbl, BorderLayout.LINE_START);

    if (displayProgress) {
        progressPanel = new JPanel();
        progressPanel.setBackground(Color.WHITE);
        progressPanel.setLayout(new BorderLayout(20, 20));

        String lblText = "<html>Stuck in the mud? Make progress with...<br /><font color=red><em>JDK Documentation</em></font><br/></html>";
        lbl = new JLabel(lblText);
        progressPanel.add(lbl, BorderLayout.NORTH);

        progressBar = new JProgressBar(0, 100);
        progressBar.setValue(0);
        progressBar.setStringPainted(true);
        progressPanel.add(progressBar, BorderLayout.SOUTH);

        add(progressPanel, BorderLayout.LINE_END);
    }

    String[] columnNames = { "City", "Temperature" };
    JTable table = new JTable(getData(), columnNames);

    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);

    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);
    scrollPane.setBackground(Color.WHITE);

    //Add the scroll pane to this panel.
    add(scrollPane, BorderLayout.PAGE_END);
}

From source file:de.codesourcery.eve.skills.ui.components.impl.planning.ResourceStatusComponent.java

@Override
protected JPanel createPanel() {
    final JPanel result = new JPanel();

    table.setFillsViewportHeight(true);

    table.setRowSorter(model.getRowSorter());

    FixedBooleanTableCellRenderer.attach(table);

    table.setDefaultRenderer(Integer.class, new DefaultTableCellRenderer() {
        @Override// ww w.  ja va  2 s .c o m
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                boolean hasFocus, int row, int column) {
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

            if (!(value instanceof Integer)) {
                return this;
            }

            if (column == 0) {
                setHorizontalAlignment(SwingConstants.LEFT);
            } else {
                setHorizontalAlignment(SwingConstants.RIGHT);
            }

            Integer amount = (Integer) value;
            if (amount.intValue() < 0) {
                if (!isSelected) {
                    setBackground(Color.RED);
                } else {
                    setBackground(table.getSelectionBackground());
                }
            } else {
                setBackground(table.getBackground());
            }
            return this;
        }
    });

    addToShoppingListButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            final List<ItemWithQuantity> items = model.getSelectedItems();

            if (items.isEmpty()) {
                return;
            }

            final ShoppingListEditorComponent comp = new ShoppingListEditorComponent(title, "", items);

            comp.setModal(true);
            ComponentWrapper.wrapComponent(comp).setVisible(true);
            if (!comp.wasCancelled() && !comp.getShoppingList().isEmpty()) {
                shoppingListManager.addShoppingList(comp.getShoppingList());
                getComponentCallback().dispose(ResourceStatusComponent.this);
            }

        }
    });

    new GridLayoutBuilder()
            .add(new GridLayoutBuilder.VerticalGroup(new GridLayoutBuilder.Cell(new JScrollPane(table)),
                    new GridLayoutBuilder.FixedCell(addToShoppingListButton)))
            .addTo(result);
    return result;
}

From source file:de.wusel.partyplayer.gui.PartyPlayer.java

private Component createPlayListPanel() {
    final PlaylistTableModel model = new PlaylistTableModel(playerModel, this);
    final JTable playlistTable = new JTable(model);
    playlistTable.getColumn(getText("table.playlist.column.votes.label")).setMaxWidth(40);
    playlistTable.getColumn(getText("table.playlist.column.votes.label")).setResizable(false);
    JScrollPane scrollPane = new JScrollPane(playlistTable);
    playlistTable.setFillsViewportHeight(true);
    return scrollPane;
}

From source file:de.wusel.partyplayer.gui.PartyPlayer.java

private Component createSongPanel() {
    final SongsTableModel model = new SongsTableModel(playerModel, settings, this);

    table = new JXTable(model) {

        @Override//from ww  w.  java2 s. c o  m
        public String getToolTipText(MouseEvent event) {
            int viewRowIndex = rowAtPoint(event.getPoint());
            if (viewRowIndex != -1) {
                int modelIndex = convertRowIndexToModel(viewRowIndex);
                SongWrapper songFromList = playerModel.getSongFromList(modelIndex);
                return songFromList.getFileName();
            }
            return super.getToolTipText(event);
        }
    };

    table.setAutoCreateRowSorter(true);
    String numberColumnName = getText("table.songs.column.number.label");
    table.getColumn(numberColumnName).setMaxWidth(25);
    table.getColumn(numberColumnName).setResizable(false);
    TableSortController sorter = (TableSortController) table.getRowSorter();
    sorter.setComparator(2, new SongComparator());

    table.getColumn(numberColumnName).setCellRenderer(new SubstanceDefaultTableCellRenderer() {

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                boolean hasFocus, int row, int column) {
            JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row,
                    column);
            if (value != null)
                label.setText(((SongWrapper) value).getTrackNumber() + "");
            return label;
        }
    });

    table.getColumn(getText("table.songs.column.duration.label"))
            .setCellRenderer(new SubstanceDefaultTableCellRenderer() {

                @Override
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                        boolean hasFocus, int row, int column) {
                    JLabel label = (JLabel) super.getTableCellRendererComponent(table, value, isSelected,
                            hasFocus, row, column);
                    if (value != null)
                        label.setText(Util.getTimeString((Double) value));
                    return label;
                }
            });

    table.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
                addSongToPlaylist(
                        playerModel.getSongFromList(table.convertRowIndexToModel(table.getSelectedRow())));
            }
        }
    });

    table.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                addSongToPlaylist(
                        playerModel.getSongFromList(table.convertRowIndexToModel(table.getSelectedRow())));
            }
        }
    });

    JScrollPane scrollPane = new JScrollPane(table);
    table.setFillsViewportHeight(true);
    return scrollPane;
}

From source file:Main.java

public Main() {
    super(new GridLayout(1, 0));

    JTable table = new JTable(new MyTableModel()) {

        //Implement table cell tool tips.
        public String getToolTipText(MouseEvent e) {
            String tip = null;/*from w w w.  j a  v  a  2s.co  m*/
            java.awt.Point p = e.getPoint();
            int rowIndex = rowAtPoint(p);
            int colIndex = columnAtPoint(p);
            int realColumnIndex = convertColumnIndexToModel(colIndex);

            if (realColumnIndex == 2) { //Sport column
                tip = "This person's favorite sport to " + "participate in is: "
                        + getValueAt(rowIndex, colIndex);
            } else if (realColumnIndex == 4) { //Veggie column
                TableModel model = getModel();
                String firstName = (String) model.getValueAt(rowIndex, 0);
                String lastName = (String) model.getValueAt(rowIndex, 1);
                Boolean veggie = (Boolean) model.getValueAt(rowIndex, 4);
                if (Boolean.TRUE.equals(veggie)) {
                    tip = firstName + " " + lastName + " is a vegetarian";
                } else {
                    tip = firstName + " " + lastName + " is not a vegetarian";
                }
            } else {
                //You can omit this part if you know you don't 
                //have any renderers that supply their own tool 
                //tips.
                tip = super.getToolTipText(e);
            }
            return tip;
        }

        //Implement table header tool tips. 
        protected JTableHeader createDefaultTableHeader() {
            return new JTableHeader(columnModel) {
                public String getToolTipText(MouseEvent e) {
                    String tip = null;
                    java.awt.Point p = e.getPoint();
                    int index = columnModel.getColumnIndexAtX(p.x);
                    int realIndex = columnModel.getColumn(index).getModelIndex();
                    return columnToolTips[realIndex];
                }
            };
        }
    };

    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);

    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    //Add the scroll pane to this panel.
    add(scrollPane);
}

From source file:components.TableToolTipsDemo.java

public TableToolTipsDemo() {
    super(new GridLayout(1, 0));

    JTable table = new JTable(new MyTableModel()) {

        //Implement table cell tool tips.
        public String getToolTipText(MouseEvent e) {
            String tip = null;/*from  w  w w .  j  a v  a2  s .  c om*/
            java.awt.Point p = e.getPoint();
            int rowIndex = rowAtPoint(p);
            int colIndex = columnAtPoint(p);
            int realColumnIndex = convertColumnIndexToModel(colIndex);

            if (realColumnIndex == 2) { //Sport column
                tip = "This person's favorite sport to " + "participate in is: "
                        + getValueAt(rowIndex, colIndex);
            } else if (realColumnIndex == 4) { //Veggie column
                TableModel model = getModel();
                String firstName = (String) model.getValueAt(rowIndex, 0);
                String lastName = (String) model.getValueAt(rowIndex, 1);
                Boolean veggie = (Boolean) model.getValueAt(rowIndex, 4);
                if (Boolean.TRUE.equals(veggie)) {
                    tip = firstName + " " + lastName + " is a vegetarian";
                } else {
                    tip = firstName + " " + lastName + " is not a vegetarian";
                }
            } else {
                //You can omit this part if you know you don't 
                //have any renderers that supply their own tool 
                //tips.
                tip = super.getToolTipText(e);
            }
            return tip;
        }

        //Implement table header tool tips. 
        protected JTableHeader createDefaultTableHeader() {
            return new JTableHeader(columnModel) {
                public String getToolTipText(MouseEvent e) {
                    String tip = null;
                    java.awt.Point p = e.getPoint();
                    int index = columnModel.getColumnIndexAtX(p.x);
                    int realIndex = columnModel.getColumn(index).getModelIndex();
                    return columnToolTips[realIndex];
                }
            };
        }
    };

    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);

    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    //Add the scroll pane to this panel.
    add(scrollPane);
}