Java Code Examples for javax.swing.JTable#setAutoResizeMode()

The following examples show how to use javax.swing.JTable#setAutoResizeMode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TableExample.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public JScrollPane createTable() {
    sorter = new TableSorter();

    //connect();
    //fetch();

    // Create the table
    JTable table = new JTable(sorter);
    // Use a scrollbar, in case there are many columns.
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    // Install a mouse listener in the TableHeader as the sorter UI.
    sorter.addMouseListenerToHeaderInTable(table);

    JScrollPane scrollpane = new JScrollPane(table);

    return scrollpane;
}
 
Example 2
Source File: GuiShowReasonsOverviewAction.java    From WorldGrower with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void actionPerformed(ActionEvent e) {
	JFrame frame = new JFrame();
	
	JTable table = new JTable(new WorldModel(world));
	table.setBounds(50, 50, 700, 700);
	table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
	
	
	frame.add(new JScrollPane(table));
	
	frame.setBounds(100,  100, 800, 800);
	frame.setVisible(true);
}
 
Example 3
Source File: TableExample.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public JScrollPane createTable() {
    sorter = new TableSorter();

    //connect();
    //fetch();

    // Create the table
    JTable table = new JTable(sorter);
    // Use a scrollbar, in case there are many columns.
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    // Install a mouse listener in the TableHeader as the sorter UI.
    sorter.addMouseListenerToHeaderInTable(table);

    JScrollPane scrollpane = new JScrollPane(table);

    return scrollpane;
}
 
Example 4
Source File: ClassLevelTableModel.java    From pcgen with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static void initializeTable(JTable classLevelTable)
{
	JTableHeader tableHeader = classLevelTable.getTableHeader();
	tableHeader.setResizingAllowed(false);
	tableHeader.setReorderingAllowed(false);
	TableColumnModel columnModel = new DefaultTableColumnModel();
	TableCellRenderer headerRenderer = tableHeader.getDefaultRenderer();
	columnModel.addColumn(Utilities.createTableColumn(0, "Level", headerRenderer, false));
	columnModel.addColumn(Utilities.createTableColumn(1, "HP", headerRenderer, false));
	columnModel.addColumn(Utilities.createTableColumn(2, "Class (All Levels In Class)", headerRenderer, true));
	classLevelTable.setColumnModel(columnModel);
	classLevelTable.setAutoCreateColumnsFromModel(false);
	classLevelTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
	classLevelTable.setFocusable(false);
	classLevelTable.setCellSelectionEnabled(false);
	classLevelTable.setRowHeight(20);
}
 
Example 5
Source File: TableExample.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public JScrollPane createTable() {
    sorter = new TableSorter();

    //connect();
    //fetch();

    // Create the table
    JTable table = new JTable(sorter);
    // Use a scrollbar, in case there are many columns.
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    // Install a mouse listener in the TableHeader as the sorter UI.
    sorter.addMouseListenerToHeaderInTable(table);

    JScrollPane scrollpane = new JScrollPane(table);

    return scrollpane;
}
 
Example 6
Source File: TableExample.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public JScrollPane createTable() {
    sorter = new TableSorter();

    //connect();
    //fetch();

    // Create the table
    JTable table = new JTable(sorter);
    // Use a scrollbar, in case there are many columns.
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    // Install a mouse listener in the TableHeader as the sorter UI.
    sorter.addMouseListenerToHeaderInTable(table);

    JScrollPane scrollpane = new JScrollPane(table);

    return scrollpane;
}
 
Example 7
Source File: InventoryDialog.java    From WorldGrower with GNU General Public License v3.0 6 votes vote down vote up
private JTable createInventoryTable(WorldObjectContainer inventory, ImageInfoReader imageInfoReader) {
	JTable inventoryTable = JTableFactory.createJTable(new InventoryModel(inventory));
	
	inventoryTable.setDefaultRenderer(ImageIds.class, new InventoryItemImageRenderer(imageInfoReader));
	inventoryTable.getColumnModel().getColumn(1).setCellRenderer(new InventoryItemRenderer());
	inventoryTable.getColumnModel().getColumn(2).setCellRenderer(new InventoryItemRenderer());
	inventoryTable.getColumnModel().getColumn(3).setCellRenderer(new InventoryItemRenderer());
	
	
	inventoryTable.setRowHeight(50);
	inventoryTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
	inventoryTable.getColumnModel().getColumn(0).setPreferredWidth(50);
	inventoryTable.getColumnModel().getColumn(1).setPreferredWidth(245);
	inventoryTable.getColumnModel().getColumn(2).setPreferredWidth(66);
	inventoryTable.getColumnModel().getColumn(3).setPreferredWidth(61);
	inventoryTable.getTableHeader().setReorderingAllowed(false);
	
	inventoryTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	inventoryTable.setAutoCreateRowSorter(true);
	
	return inventoryTable;
}
 
Example 8
Source File: ProductPlacemarkView.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
public ProductPlacemarkView(VectorDataNode vectorDataNode) {
    this.vectorDataNode = vectorDataNode;
    this.vectorDataNode.getProduct().addProductNodeListener(new PNL());
    tableModel = new PlacemarkTableModel();
    JTable placemarkTable = new JTable();
    placemarkTable.setRowSorter(new TableRowSorter<>(tableModel));
    placemarkTable.addMouseListener(new PopupMenuHandler(this));
    placemarkTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    placemarkTable.setModel(tableModel);

    final TableCellRenderer renderer = placemarkTable.getTableHeader().getDefaultRenderer();
    final int margin = placemarkTable.getTableHeader().getColumnModel().getColumnMargin();

    Enumeration<TableColumn> columns = placemarkTable.getColumnModel().getColumns();
    while (columns.hasMoreElements()) {
        TableColumn tableColumn = columns.nextElement();
        final int width = getColumnMinWith(tableColumn, renderer, margin);
        tableColumn.setMinWidth(width);
    }

    final JScrollPane scrollPane = new JScrollPane(placemarkTable);

    setLayout(new BorderLayout());
    add(scrollPane, BorderLayout.CENTER);
}
 
Example 9
Source File: TableExample.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public JScrollPane createTable() {
    sorter = new TableSorter();

    //connect();
    //fetch();

    // Create the table
    JTable table = new JTable(sorter);
    // Use a scrollbar, in case there are many columns.
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    // Install a mouse listener in the TableHeader as the sorter UI.
    sorter.addMouseListenerToHeaderInTable(table);

    JScrollPane scrollpane = new JScrollPane(table);

    return scrollpane;
}
 
Example 10
Source File: Discrete1BandTabularForm.java    From snap-desktop with GNU General Public License v3.0 6 votes vote down vote up
public Discrete1BandTabularForm(ColorManipulationForm parentForm) {
    this.parentForm = parentForm;
    tableModel = new ImageInfoTableModel();
    moreOptionsForm = new MoreOptionsForm(this, false);

    final JTable table = new JTable(tableModel);
    table.setRowSorter(new TableRowSorter<>(tableModel));
    table.setDefaultRenderer(Color.class, new ColorTableCellRenderer());
    table.setDefaultEditor(Color.class, new ColorTableCellEditor());
    table.getTableHeader().setReorderingAllowed(false);
    table.getColumnModel().getColumn(1).setPreferredWidth(140);
    table.getColumnModel().getColumn(3).setCellRenderer(new PercentageRenderer());

    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    final JScrollPane tableScrollPane = new JScrollPane(table);
    tableScrollPane.getViewport().setPreferredSize(table.getPreferredSize());
    contentPanel = tableScrollPane;
}
 
Example 11
Source File: TransactionsDetailPanel.java    From zencash-swing-wallet-ui with MIT License 6 votes vote down vote up
private JTable createTransactionsTable(String rowData[][])
	throws WalletCallException, IOException, InterruptedException
{
	String columnNames[] = langUtil.getString("transactions.detail.panel.column.names").split(":");
       JTable table = new TransactionTable(
       	rowData, columnNames, this.parentFrame, this.clientCaller, this.installationObserver); 
       table.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
       table.getColumnModel().getColumn(0).setPreferredWidth(190);
       table.getColumnModel().getColumn(1).setPreferredWidth(145);
       table.getColumnModel().getColumn(2).setPreferredWidth(170);
       table.getColumnModel().getColumn(3).setPreferredWidth(210);
       table.getColumnModel().getColumn(4).setPreferredWidth(405);
       table.getColumnModel().getColumn(5).setPreferredWidth(800);

       return table;
}
 
Example 12
Source File: TableExample.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public JScrollPane createTable() {
    sorter = new TableSorter();

    //connect();
    //fetch();

    // Create the table
    JTable table = new JTable(sorter);
    // Use a scrollbar, in case there are many columns.
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    // Install a mouse listener in the TableHeader as the sorter UI.
    sorter.addMouseListenerToHeaderInTable(table);

    JScrollPane scrollpane = new JScrollPane(table);

    return scrollpane;
}
 
Example 13
Source File: AbstractDemoFrame.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected JComponent createDefaultTable(final TableModel data)
{
  final JTable table = new JTable(data);
  table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

  for (int columnIndex = 0; columnIndex < data
      .getColumnCount(); columnIndex++)
  {
    final TableColumn column = table.getColumnModel().getColumn(columnIndex);
    column.setMinWidth(50);
    final Class c = data.getColumnClass(columnIndex);
    if (c.equals(Number.class))
    {
      column.setCellRenderer(new NumberCellRenderer());
    }
  }

  return new JScrollPane
      (table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
          JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
}
 
Example 14
Source File: AbstractDemoHandler.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected JComponent createDefaultTable(final TableModel data)
{
  final JTable table = new JTable(data);
  table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

  for (int columnIndex = 0; columnIndex < data.getColumnCount(); columnIndex++)
  {
    final TableColumn column = table.getColumnModel().getColumn(columnIndex);
    column.setMinWidth(50);
    final Class c = data.getColumnClass(columnIndex);
    if (c.equals(Number.class))
    {
      column.setCellRenderer(new NumberCellRenderer());
    }
  }

  return new JScrollPane
      (table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
          JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
}
 
Example 15
Source File: SystemPropertiesPanel.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Creates and returns a JTable containing all the system properties. This method returns a table that is configured
 * so that the user can sort the properties by clicking on the table header.
 *
 * @return a system properties table.
 */
public static JTable createSystemPropertiesTable() {
  final ResourceBundle resources = ResourceBundle.getBundle( SwingPreviewModule.BUNDLE_NAME );

  final String[] names =
      new String[] { resources.getString( "system-properties-table.column.name" ),
        resources.getString( "system-properties-table.column.value" ), };

  final Properties sysProps = System.getProperties();

  final TreeMap data = new TreeMap( sysProps );
  final Map.Entry[] entries = (Map.Entry[]) data.entrySet().toArray( new Map.Entry[data.size()] );
  final DefaultTableModel properties = new DefaultTableModel( names, entries.length );
  for ( int i = 0; i < entries.length; i++ ) {
    final Map.Entry entry = entries[i];
    properties.setValueAt( entry.getKey(), i, 0 );
    properties.setValueAt( entry.getValue(), i, 1 );
  }

  final JTable table = new JTable( properties );
  final TableColumnModel model = table.getColumnModel();
  TableColumn column = model.getColumn( 0 );
  column.setPreferredWidth( 200 );
  column = model.getColumn( 1 );
  column.setPreferredWidth( 350 );

  table.setAutoResizeMode( JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS );
  return table;

}
 
Example 16
Source File: GUIBrowser.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void addPropertiesTab() {
    if (compNode instanceof ComponentNode) {
        Hashtable<String, Object> props = ((ContainerNode) compNode).getProperties();
        if (props.size() > 0) {
            JTable propTable = new JTable(new MyModel(props));
            propTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
            tbd.add(PROPERTIES_TAB, new JScrollPane(propTable));
            /*
            propTable.addMouseListener(new MouseListener() {
                public void mouseClicked(MouseEvent e) {
                    new ComponentBrowser(getOwnr(),
                            getSelectedNode()).
                            show();
                }
                public void mouseExited(MouseEvent e) {
                }
                public void mouseEntered(MouseEvent e) {
                }
                public void mousePressed(MouseEvent e) {
                }
                public void mouseReleased(MouseEvent e) {
                }
            });
             */
        }
    }
}
 
Example 17
Source File: DemandsDialog.java    From WorldGrower with GNU General Public License v3.0 5 votes vote down vote up
public void initializeGUI(ImageInfoReader imageInfoReader, SoundIdReader soundIdReader) {
	DemandsModel worldModel = new DemandsModel(demands);
	JTable table = JTableFactory.createJTable(worldModel);
	table.setDefaultRenderer(ImageIds.class, new ImageTableRenderer(imageInfoReader));
	table.setAutoCreateRowSorter(true);
	table.setRowHeight(50);
	table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
	table.getColumnModel().getColumn(0).setPreferredWidth(100);
	table.getColumnModel().getColumn(1).setPreferredWidth(200);
	table.getColumnModel().getColumn(2).setPreferredWidth(100);
	table.getRowSorter().toggleSortOrder(1);

	TableCellEditor fce = new PositiveIntegerCellEditor(JTextFieldFactory.createJTextField());
       table.setDefaultEditor(Integer.class, fce);
	
	JScrollPane scrollPane = JScrollPaneFactory.createScrollPane(table);
	scrollPane.setBounds(15, 15, 418, 700);
	addComponent(scrollPane);
	
	JPanel buttonPane = new JPanel();
	buttonPane.setOpaque(false);
	buttonPane.setBounds(0, 720, 428, 75);
	buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
	addComponent(buttonPane);
	
	JButton okButton = JButtonFactory.createButton("OK", imageInfoReader, soundIdReader);
	okButton.setActionCommand("OK");
	buttonPane.add(okButton);
	addActionHandlers(okButton, worldModel, this, demands);
	getRootPane().setDefaultButton(okButton);
	
	SwingUtils.makeTransparant(table, scrollPane);
}
 
Example 18
Source File: ItemListImageViewerEvent.java    From stendhal with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void prepareView(final Dimension maxSize) {
	Dimension screenSize = GameScreen.get().getSize();
	int maxPreferredWidth = screenSize.width - 80;
	if (event.has("caption")) {
		JLabel caption = new JLabel("<html><div width=" + (maxPreferredWidth
				- 10) + ">" + event.get("caption") + "</div></html>");
		caption.setBorder(BorderFactory.createEmptyBorder(PAD, PAD, PAD, PAD));
		add(caption, BorderLayout.NORTH);
	}

	JTable table = createTable(maxPreferredWidth);
	// Prevents selection
	table.setEnabled(false);
	table.setFillsViewportHeight(true);
	table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
	TableColumn col = table.getColumnModel().getColumn(0);
	col.setCellRenderer(new SpriteCellRenderer());

	col = table.getColumnModel().getColumn(1);
	DefaultTableCellRenderer r = new DefaultTableCellRenderer();
	r.setHorizontalAlignment(SwingConstants.CENTER);
	col.setCellRenderer(r);

	col = table.getColumnModel().getColumn(2);
	col.setCellRenderer(new DescriptionCellRenderer());

	HeaderRenderer hr = new HeaderRenderer();
	Enumeration<TableColumn> cols = table.getColumnModel().getColumns();
	while (cols.hasMoreElements()) {
		TableColumn c = cols.nextElement();
		c.setHeaderRenderer(hr);
	}

	adjustColumnWidths(table);
	adjustRowHeights(table);

	ScrolledViewport viewPort = new ScrolledViewport(table);
	/*
	 * maxPreferredWidth is incorrect, but java does not seem to support
	 * max-width property for div's, so all the cells report the same
	 * preferred width anyway.
	 */
	viewPort.getComponent().setPreferredSize(new Dimension(maxPreferredWidth,
			Math.min(screenSize.height - 100, table.getPreferredSize().height
					+ hr.getPreferredSize().height + 4 * PAD)));
	viewPort.getComponent().setBackground(table.getBackground());
	add(viewPort.getComponent(), BorderLayout.CENTER);

	setVisible(true);
}
 
Example 19
Source File: AnimPlayList.java    From open-ig with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
* Resizes the table columns based on the column and data preferred widths.
* @param table the original table
* @param model the data model
* @return the table itself
*/
  public static JTable autoResizeColWidth(JTable table, AbstractTableModel model) {
      table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
      table.setModel(model);
 
      int margin = 5;
 
      for (int i = 0; i < table.getColumnCount(); i++) {
          int                     vColIndex = i;
          DefaultTableColumnModel colModel  = (DefaultTableColumnModel) table.getColumnModel();
          TableColumn             col       = colModel.getColumn(vColIndex);
          int                     width;
 
          // Get width of column header
          TableCellRenderer renderer = col.getHeaderRenderer();
 
          if (renderer == null) {
              renderer = table.getTableHeader().getDefaultRenderer();
          }
 
          Component comp = renderer.getTableCellRendererComponent(table, col.getHeaderValue(), false, false, 0, 0);
 
          width = comp.getPreferredSize().width;
 
          // Get maximum width of column data
          for (int r = 0; r < table.getRowCount(); r++) {
              renderer = table.getCellRenderer(r, vColIndex);
              comp     = renderer.getTableCellRendererComponent(table, table.getValueAt(r, vColIndex), false, false,
                      r, vColIndex);
              width = Math.max(width, comp.getPreferredSize().width);
          }
 
          // Add margin
          width += 2 * margin;
 
          // Set the width
          col.setPreferredWidth(width);
      }
 
      ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()).setHorizontalAlignment(
          SwingConstants.LEFT);
 
      return table;
  }
 
Example 20
Source File: TableDemo.java    From beautyeye with Apache License 2.0 4 votes vote down vote up
protected void initComponents() {
	setLayout(new BorderLayout());

	controlPanel = createControlPanel();
	add(controlPanel, BorderLayout.NORTH);

	// <snip>Create JTable
	oscarTable = new JTable(oscarModel);
	// </snip>

	// </snip>Set JTable display properties
	oscarTable.setColumnModel(createColumnModel());
	oscarTable.setAutoCreateRowSorter(true);
	oscarTable.setRowHeight(26);
	oscarTable.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);
	oscarTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	oscarTable.setIntercellSpacing(new Dimension(0, 0));
	// </snip>

	// <snip>Initialize preferred size for table's viewable area
	Dimension viewSize = new Dimension();
	viewSize.width = oscarTable.getColumnModel().getTotalColumnWidth();
	viewSize.height = 10 * oscarTable.getRowHeight();
	oscarTable.setPreferredScrollableViewportSize(viewSize);
	// </snip>

	// <snip>Customize height and alignment of table header
	JTableHeader header = oscarTable.getTableHeader();
	header.setPreferredSize(new Dimension(30, 26));
	TableCellRenderer headerRenderer = header.getDefaultRenderer();
	if (headerRenderer instanceof JLabel) {
		((JLabel) headerRenderer).setHorizontalAlignment(JLabel.CENTER);
	}
	// </snip>

	JScrollPane scrollpane = new JScrollPane(oscarTable);
	dataPanel = new Stacker(scrollpane);
	add(dataPanel, BorderLayout.CENTER);

	add(createStatusBar(), BorderLayout.SOUTH);

}