Example usage for javax.swing JTable JTable

List of usage examples for javax.swing JTable JTable

Introduction

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

Prototype

public JTable(TableModel dm) 

Source Link

Document

Constructs a JTable that is initialized with dm as the data model, a default column model, and a default selection model.

Usage

From source file:gui.accessories.BattleSimFx.java

@Override
public void init() {
    tableModel = new SampleTableModel();
    // create javafx panel for charts
    chartFxPanel = new JFXPanel();
    chartFxPanel.setPreferredSize(new Dimension(PANEL_WIDTH_INT, PANEL_HEIGHT_INT));

    //JTable/*from www  . jav a 2 s  . co m*/
    JTable table = new JTable(tableModel);
    table.setAutoCreateRowSorter(true);
    table.setGridColor(Color.DARK_GRAY);
    BattleSimFx.DecimalFormatRenderer renderer = new BattleSimFx.DecimalFormatRenderer();
    renderer.setHorizontalAlignment(JLabel.RIGHT);
    for (int i = 0; i < table.getColumnCount(); i++) {
        table.getColumnModel().getColumn(i).setCellRenderer(renderer);
    }
    JScrollPane tablePanel = new JScrollPane(table);
    tablePanel.setPreferredSize(new Dimension(PANEL_WIDTH_INT, TABLE_PANEL_HEIGHT_INT));

    JPanel chartTablePanel = new JPanel();
    chartTablePanel.setLayout(new BorderLayout());

    //Split pane that holds both chart and table
    JSplitPane jsplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    jsplitPane.setTopComponent(chartTablePanel);
    jsplitPane.setBottomComponent(tablePanel);
    jsplitPane.setDividerLocation(410);
    chartTablePanel.add(chartFxPanel, BorderLayout.CENTER);

    //          add(tablePanel, BorderLayout.CENTER);
    add(jsplitPane, BorderLayout.CENTER);

    // create JavaFX scene
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            createScene();
        }
    });
}

From source file:net.sf.vfsjfilechooser.accessories.bookmarks.BookmarksManagerPanel.java

public BookmarksManagerPanel(BookmarksDialog parentDialog, VFSJFileChooser chooser) {
    this.parentDialog = parentDialog;
    this.chooser = chooser;

    model = new Bookmarks();

    table = new JTable(model);
    scrollPane = new JScrollPane(table);

    table.setPreferredScrollableViewportSize(tableSize);
    table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

    bCancel = new JButton(VFSResources.getMessage("VFSJFileChooser.closeButtonText"));

    bOpen = new JButton(VFSResources.getMessage("VFSJFileChooser.openButtonText"));
    bOpen.setIcon(/*from w w  w.  j  a  va  2  s.  co  m*/
            new ImageIcon(getClass().getResource("/net/sf/vfsjfilechooser/plaf/icons/document-open.png")));
    bOpen.setHorizontalAlignment(SwingConstants.LEFT);

    bAdd = new JButton(VFSResources.getMessage("VFSJFileChooser.addButtonText"));
    bAdd.setIcon(new ImageIcon(getClass().getResource("/net/sf/vfsjfilechooser/plaf/icons/list-add.png")));
    bAdd.setHorizontalAlignment(SwingConstants.LEFT);

    bEdit = new JButton(VFSResources.getMessage("VFSJFileChooser.editButtonText"));
    bEdit.setIcon(new ImageIcon(getClass().getResource("/net/sf/vfsjfilechooser/plaf/icons/book_edit.png")));
    bEdit.setHorizontalAlignment(SwingConstants.LEFT);

    bDelete = new JButton(VFSResources.getMessage("VFSJFileChooser.deleteButtonText"));
    bDelete.setIcon(
            new ImageIcon(getClass().getResource("/net/sf/vfsjfilechooser/plaf/icons/list-remove.png")));
    bDelete.setHorizontalAlignment(SwingConstants.LEFT);

    bMoveUp = new JButton(VFSResources.getMessage("VFSJFileChooser.moveUpButtonText"));
    bMoveUp.setIcon(new ImageIcon(getClass().getResource("/net/sf/vfsjfilechooser/plaf/icons/go-up.png")));
    bMoveUp.setHorizontalAlignment(SwingConstants.LEFT);

    bMoveDown = new JButton(VFSResources.getMessage("VFSJFileChooser.moveDownButtonText"));
    bMoveDown.setIcon(new ImageIcon(getClass().getResource("/net/sf/vfsjfilechooser/plaf/icons/go-down.png")));
    bMoveDown.setHorizontalAlignment(SwingConstants.LEFT);

    final ActionHandler ah = new ActionHandler();

    bOpen.addActionListener(ah);
    bCancel.addActionListener(ah);
    bEdit.addActionListener(ah);
    bAdd.addActionListener(ah);
    bDelete.addActionListener(ah);
    bMoveUp.addActionListener(ah);
    bMoveDown.addActionListener(ah);

    final Box south = Box.createHorizontalBox();
    south.add(Box.createHorizontalGlue());
    south.add(bCancel);
    south.add(Box.createHorizontalGlue());

    final JPanel buttons = new JPanel(new GridLayout(0, 1, 5, 5));

    buttons.add(bAdd);
    buttons.add(bEdit);
    buttons.add(bDelete);
    buttons.add(bOpen);
    buttons.add(Box.createVerticalStrut(10));
    buttons.add(bMoveUp);
    buttons.add(bMoveDown);

    JPanel east = new JPanel();
    east.add(buttons, BorderLayout.NORTH);
    east.add(new JPanel(), BorderLayout.CENTER); // don't ask

    setLayout(new BorderLayout(10, 10));

    add(scrollPane, BorderLayout.CENTER);
    add(south, BorderLayout.SOUTH);
    add(east, BorderLayout.EAST);

    setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, UIManager.getColor("Panel.background")));
}

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;/*www .j  a  v  a2 s  . c o 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:edu.harvard.mcz.imagecapture.VerbatimListDialog.java

protected void init() {
    setTitle("Verbatim Transcriptions to parse into fields");
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setBounds(100, 100, 1000, 600);/* w ww  . j  a v a2  s.  c om*/
    getContentPane().setLayout(new BorderLayout());
    contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(contentPanel, BorderLayout.CENTER);
    contentPanel.setLayout(new BorderLayout(0, 0));
    SpecimenLifeCycle sls = new SpecimenLifeCycle();
    table = new JTable(new VerbatimCountTableModel(sls.countDistinctVerbatimValues()));
    table.setDefaultRenderer(VerbatimCount.class, new ButtonRenderer("Edit"));
    table.setDefaultEditor(VerbatimCount.class, new ButtonEditor(ButtonEditor.OPEN_VERBATIM_CLASSIFY, this));
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setViewportView(table);
    contentPanel.add(scrollPane, BorderLayout.CENTER);
    {
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
        getContentPane().add(buttonPane, BorderLayout.SOUTH);
        {
            JButton cancelButton = new JButton("Close");
            cancelButton.setActionCommand("Close");
            cancelButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    setVisible(false);
                }

            });
            buttonPane.add(cancelButton);
        }
    }
}

From source file:TableRowColumnTest.java

public PlanetTableFrame() {
    setTitle("TableRowColumnTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    TableModel model = new DefaultTableModel(cells, columnNames) {
        public Class<?> getColumnClass(int c) {
            return cells[0][c].getClass();
        }/*from  ww w  .  j  a  v a 2 s  . c  o m*/
    };

    table = new JTable(model);

    table.setRowHeight(100);
    table.getColumnModel().getColumn(COLOR_COLUMN).setMinWidth(250);
    table.getColumnModel().getColumn(IMAGE_COLUMN).setMinWidth(100);

    final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
    table.setRowSorter(sorter);
    sorter.setComparator(COLOR_COLUMN, new Comparator<Color>() {
        public int compare(Color c1, Color c2) {
            int d = c1.getBlue() - c2.getBlue();
            if (d != 0)
                return d;
            d = c1.getGreen() - c2.getGreen();
            if (d != 0)
                return d;
            return c1.getRed() - c2.getRed();
        }
    });
    sorter.setSortable(IMAGE_COLUMN, false);
    add(new JScrollPane(table), BorderLayout.CENTER);

    removedRowIndices = new HashSet<Integer>();
    removedColumns = new ArrayList<TableColumn>();

    final RowFilter<TableModel, Integer> filter = new RowFilter<TableModel, Integer>() {
        public boolean include(Entry<? extends TableModel, ? extends Integer> entry) {
            return !removedRowIndices.contains(entry.getIdentifier());
        }
    };

    // create menu

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    JMenu selectionMenu = new JMenu("Selection");
    menuBar.add(selectionMenu);

    rowsItem = new JCheckBoxMenuItem("Rows");
    columnsItem = new JCheckBoxMenuItem("Columns");
    cellsItem = new JCheckBoxMenuItem("Cells");

    rowsItem.setSelected(table.getRowSelectionAllowed());
    columnsItem.setSelected(table.getColumnSelectionAllowed());
    cellsItem.setSelected(table.getCellSelectionEnabled());

    rowsItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            table.clearSelection();
            table.setRowSelectionAllowed(rowsItem.isSelected());
            updateCheckboxMenuItems();
        }
    });
    selectionMenu.add(rowsItem);

    columnsItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            table.clearSelection();
            table.setColumnSelectionAllowed(columnsItem.isSelected());
            updateCheckboxMenuItems();
        }
    });
    selectionMenu.add(columnsItem);

    cellsItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            table.clearSelection();
            table.setCellSelectionEnabled(cellsItem.isSelected());
            updateCheckboxMenuItems();
        }
    });
    selectionMenu.add(cellsItem);

    JMenu tableMenu = new JMenu("Edit");
    menuBar.add(tableMenu);

    JMenuItem hideColumnsItem = new JMenuItem("Hide Columns");
    hideColumnsItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            int[] selected = table.getSelectedColumns();
            TableColumnModel columnModel = table.getColumnModel();

            // remove columns from view, starting at the last
            // index so that column numbers aren't affected

            for (int i = selected.length - 1; i >= 0; i--) {
                TableColumn column = columnModel.getColumn(selected[i]);
                table.removeColumn(column);

                // store removed columns for "show columns" command

                removedColumns.add(column);
            }
        }
    });
    tableMenu.add(hideColumnsItem);

    JMenuItem showColumnsItem = new JMenuItem("Show Columns");
    showColumnsItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            // restore all removed columns
            for (TableColumn tc : removedColumns)
                table.addColumn(tc);
            removedColumns.clear();
        }
    });
    tableMenu.add(showColumnsItem);

    JMenuItem hideRowsItem = new JMenuItem("Hide Rows");
    hideRowsItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            int[] selected = table.getSelectedRows();
            for (int i : selected)
                removedRowIndices.add(table.convertRowIndexToModel(i));
            sorter.setRowFilter(filter);
        }
    });
    tableMenu.add(hideRowsItem);

    JMenuItem showRowsItem = new JMenuItem("Show Rows");
    showRowsItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            removedRowIndices.clear();
            sorter.setRowFilter(filter);
        }
    });
    tableMenu.add(showRowsItem);

    JMenuItem printSelectionItem = new JMenuItem("Print Selection");
    printSelectionItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            int[] selected = table.getSelectedRows();
            System.out.println("Selected rows: " + Arrays.toString(selected));
            selected = table.getSelectedColumns();
            System.out.println("Selected columns: " + Arrays.toString(selected));
        }
    });
    tableMenu.add(printSelectionItem);
}

From source file:gate.termraider.gui.HyponymyDebugger.java

protected void generateTable() {
    JTextField tempField = new JTextField("generating...");
    placeholder.add(tempField);/*from  w  w w  .j a v a 2  s  . c  o  m*/

    TableModel tableModel = new HDTableModel(termbank);
    table = new JTable(tableModel);
    table.setDefaultRenderer(String.class, new MultilineCellRenderer());
    table.setAutoCreateRowSorter(true);
    scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    this.remove(placeholder);
    this.add(scrollPane, BorderLayout.CENTER);
}

From source file:com.googlecode.vfsjfilechooser2.accessories.bookmarks.BookmarksManagerPanel.java

public BookmarksManagerPanel(BookmarksDialog parentDialog, VFSJFileChooser chooser) {
    this.parentDialog = parentDialog;
    this.chooser = chooser;

    model = new Bookmarks();

    table = new JTable(model);
    scrollPane = new JScrollPane(table);

    table.setPreferredScrollableViewportSize(tableSize);
    table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

    bCancel = new JButton(VFSResources.getMessage("VFSJFileChooser.closeButtonText"));

    bOpen = new JButton(VFSResources.getMessage("VFSJFileChooser.openButtonText"));
    bOpen.setIcon(new ImageIcon(
            getClass().getResource("/com/googlecode/vfsjfilechooser2/plaf/icons/document-open.png")));
    bOpen.setHorizontalAlignment(SwingConstants.LEFT);

    bAdd = new JButton(VFSResources.getMessage("VFSJFileChooser.addButtonText"));
    bAdd.setIcon(// w w  w .  java  2s.  c  o m
            new ImageIcon(getClass().getResource("/com/googlecode/vfsjfilechooser2/plaf/icons/list-add.png")));
    bAdd.setHorizontalAlignment(SwingConstants.LEFT);

    bEdit = new JButton(VFSResources.getMessage("VFSJFileChooser.editButtonText"));
    bEdit.setIcon(
            new ImageIcon(getClass().getResource("/com/googlecode/vfsjfilechooser2/plaf/icons/book_edit.png")));
    bEdit.setHorizontalAlignment(SwingConstants.LEFT);

    bDelete = new JButton(VFSResources.getMessage("VFSJFileChooser.deleteButtonText"));
    bDelete.setIcon(new ImageIcon(
            getClass().getResource("/com/googlecode/vfsjfilechooser2/plaf/icons/list-remove.png")));
    bDelete.setHorizontalAlignment(SwingConstants.LEFT);

    bMoveUp = new JButton(VFSResources.getMessage("VFSJFileChooser.moveUpButtonText"));
    bMoveUp.setIcon(
            new ImageIcon(getClass().getResource("/com/googlecode/vfsjfilechooser2/plaf/icons/go-up.png")));
    bMoveUp.setHorizontalAlignment(SwingConstants.LEFT);

    bMoveDown = new JButton(VFSResources.getMessage("VFSJFileChooser.moveDownButtonText"));
    bMoveDown.setIcon(
            new ImageIcon(getClass().getResource("/com/googlecode/vfsjfilechooser2/plaf/icons/go-down.png")));
    bMoveDown.setHorizontalAlignment(SwingConstants.LEFT);

    final ActionHandler ah = new ActionHandler();

    bOpen.addActionListener(ah);
    bCancel.addActionListener(ah);
    bEdit.addActionListener(ah);
    bAdd.addActionListener(ah);
    bDelete.addActionListener(ah);
    bMoveUp.addActionListener(ah);
    bMoveDown.addActionListener(ah);

    final Box south = Box.createHorizontalBox();
    south.add(Box.createHorizontalGlue());
    south.add(bCancel);
    south.add(Box.createHorizontalGlue());

    final JPanel buttons = new JPanel(new GridLayout(0, 1, 5, 5));

    buttons.add(bAdd);
    buttons.add(bEdit);
    buttons.add(bDelete);
    buttons.add(bOpen);
    buttons.add(Box.createVerticalStrut(10));
    buttons.add(bMoveUp);
    buttons.add(bMoveDown);

    JPanel east = new JPanel();
    east.add(buttons, BorderLayout.NORTH);
    east.add(new JPanel(), BorderLayout.CENTER); // don't ask

    setLayout(new BorderLayout(10, 10));

    add(scrollPane, BorderLayout.CENTER);
    add(south, BorderLayout.SOUTH);
    add(east, BorderLayout.EAST);

    setBorder(BorderFactory.createMatteBorder(10, 10, 10, 10, UIManager.getColor("Panel.background")));
}

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;/*w  w  w. j  a  v a 2  s .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:SampleSortingTableModel.java

public SortableTableModelAbstractTableModel() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    JTable tableOrig = new JTable(model);
    tableOrig.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    JTable tableSorted = new JTable(new SampleSortingTableModel(model, 0));
    tableSorted.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    JPanel panel = new JPanel(new GridLayout(1, 2));
    panel.add(new JScrollPane(tableOrig));
    panel.add(new JScrollPane(tableSorted));
    getContentPane().add(panel, BorderLayout.CENTER);
    pack();//from ww  w.  j  a  va  2 s.c  o m
}

From source file:com.emental.mindraider.ui.outline.OutlineSorterJPanel.java

public OutlineSorterJPanel() {
    setLayout(new BorderLayout());

    // table with archived concepts (title)
    // let table model to load discarded concepts itself
    tableModel = new SorterTableModel();
    table = new JTable(tableModel);
    table.getSelectionModel().addListSelectionListener(new SorterListSelectionListener(table, tableModel));
    table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.setFillsViewportHeight(true);/* www .jav a 2s . c  o m*/
    table.setShowHorizontalLines(false);
    table.setShowVerticalLines(false);
    table.getColumnModel().getColumn(COLUMN_NAME).setPreferredWidth(150);
    table.getColumnModel().getColumn(COLUMN_ANNOTATION).setPreferredWidth(220);
    table.getColumnModel().getColumn(COLUMN_CREATED).setPreferredWidth(60);
    table.getColumnModel().getColumn(COLUMN_MODIFIED).setPreferredWidth(60);
    table.getColumnModel().getColumn(COLUMN_REVISION).setPreferredWidth(35);

    TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
    table.setRowSorter(sorter);
    sorter.setComparator(COLUMN_REVISION, new Comparator<Long>() {
        @Override
        public int compare(Long o1, Long o2) {
            return o2.intValue() - o1.intValue();
        }
    });
    final Comparator<String> timestampComparator = new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            if (OutlineTreeInstance.getCreatedTimestampFromHtml(o2) > OutlineTreeInstance
                    .getCreatedTimestampFromHtml(o1)) {
                return 1;
            } else {
                if (OutlineTreeInstance.getCreatedTimestampFromHtml(o2) == OutlineTreeInstance
                        .getCreatedTimestampFromHtml(o1)) {
                    return 0;
                } else {
                    return -1;
                }
            }
        }
    };
    sorter.setComparator(COLUMN_MODIFIED, timestampComparator);
    sorter.setComparator(COLUMN_CREATED, timestampComparator);

    JScrollPane scroll = new JScrollPane(table);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    add(scroll, BorderLayout.CENTER);
}