List of usage examples for javax.swing JTable getPreferredSize
@Transient
public Dimension getPreferredSize()
preferredSize
has been set to a non-null
value just returns it. From source file:Main.java
public static void main(String[] args) { int maximum = 100; JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Integer[] oneRow = { 0, 0, 0, 0 }; String[] headers = { "A", "B", "C", "D" }; Integer[][] data = { oneRow, oneRow, oneRow, oneRow, oneRow, }; DefaultTableModel model = new DefaultTableModel(data, headers); JTable table = new JTable(model); table.setDefaultRenderer(Object.class, new ProgressRenderer(0, maximum)); table.setPreferredScrollableViewportSize(table.getPreferredSize()); frame.add(new JScrollPane(table)); frame.pack();/*from w w w . j a v a2 s .c o m*/ frame.setVisible(true); new Thread(new Runnable() { @Override public void run() { Object waiter = new Object(); synchronized (waiter) { int rows = model.getRowCount(); int columns = model.getColumnCount(); Random random = new Random(System.currentTimeMillis()); boolean done = false; while (!done) { int row = random.nextInt(rows); int column = random.nextInt(columns); Integer value = (Integer) model.getValueAt(row, column); value++; if (value <= maximum) { model.setValueAt(value, row, column); try { waiter.wait(15); } catch (InterruptedException e) { e.printStackTrace(); } } done = true; for (row = 0; row < rows; row++) { for (column = 0; column < columns; column++) { if (!model.getValueAt(row, column).equals(maximum)) { done = false; break; } } if (!done) { break; } } } } } }).start(); }
From source file:Main.java
public static void main(String[] args) { JTable table = new JTable(5, 5) { @Override/*from w w w.j ava 2s . co m*/ public Component prepareRenderer(TableCellRenderer renderer, int row, int col) { Component comp = super.prepareRenderer(renderer, row, col); ((JLabel) comp).setHorizontalAlignment(JLabel.RIGHT); return comp; } }; table.setPreferredScrollableViewportSize(table.getPreferredSize()); JScrollPane scrollPane = new JScrollPane(table); JFrame f = new JFrame(); f.getContentPane().add(scrollPane); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setVisible(true); }
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; }/* w w w .ja v a 2 s .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:Main.java
public Main() { JPanel buttonPanel = new JPanel(); buttonPanel.add(new JButton("Foo")); buttonPanel.add(Box.createHorizontalStrut(10)); buttonPanel.add(new JButton("Bar")); String[] columnNames = { "Mon", "Tues", "Wed" }; DefaultTableModel model = new DefaultTableModel(columnNames, 25); JTable table = new JTable(model); JScrollPane scrollPane = new JScrollPane(table); scrollPane.getViewport().setPreferredSize(table.getPreferredSize()); JLabel southLabel = new JLabel("OK!"); southLabel.setForeground(Color.white); JPanel southPanel = new JPanel(); southPanel.add(southLabel);//from w ww. j a v a 2 s. c om setLayout(new BorderLayout(5, 5)); add(buttonPanel, BorderLayout.NORTH); add(scrollPane, BorderLayout.CENTER); add(southPanel, BorderLayout.SOUTH); }
From source file:Main.java
/** * Setups the given table for usage as row-header. This method setups the background color to * the same one than the column headers. * * {@note In a previous version, we were assigning to the row headers the same cell renderer than * the one created by <cite>Swing</cite> for the column headers. But it produced strange * effects when the L&F uses a vertical grandiant instead than a uniform color.} * * @param table The table to setup as row headers. * @return The renderer which has been assigned to the table. *//*from ww w. j a v a 2s. co m*/ public static TableCellRenderer setupAsRowHeader(final JTable table) { final JTableHeader header = table.getTableHeader(); Color background = header.getBackground(); Color foreground = header.getForeground(); if (background == null || background.equals(table.getBackground())) { if (!SystemColor.control.equals(background)) { background = SystemColor.control; foreground = SystemColor.controlText; } else { final Locale locale = table.getLocale(); background = UIManager.getColor("Label.background", locale); foreground = UIManager.getColor("Label.foreground", locale); } } final DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(); renderer.setBackground(background); renderer.setForeground(foreground); renderer.setHorizontalAlignment(DefaultTableCellRenderer.RIGHT); final TableColumn column = table.getColumnModel().getColumn(0); column.setCellRenderer(renderer); column.setPreferredWidth(60); table.setPreferredScrollableViewportSize(table.getPreferredSize()); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.setCellSelectionEnabled(false); return renderer; }
From source file:Main.java
public Main() { ImageIcon aboutIcon = new ImageIcon("about16.gif"); ImageIcon addIcon = new ImageIcon("add16.gif"); ImageIcon copyIcon = new ImageIcon("copy16.gif"); String[] columnNames = { "Picture", "Description" }; Object[][] data = { { aboutIcon, "About" }, { addIcon, "Add" }, { copyIcon, "Copy" }, }; DefaultTableModel model = new DefaultTableModel(data, columnNames); JTable table = new JTable(model) { public Class getColumnClass(int column) { return (column == 0) ? Icon.class : Object.class; }//from w ww.ja v a 2s .co m }; table.setPreferredScrollableViewportSize(table.getPreferredSize()); JScrollPane scrollPane = new JScrollPane(table); getContentPane().add(scrollPane); }
From source file:edu.ku.brc.specify.plugins.sgr.SGRResultsDisplay.java
public SGRResultsDisplay(int width, MatchResults results) { if (Main.m_settings == null) { Main.m_settings = new JSettings(true); Main.m_settings.load(//from ww w . j av a 2 s. co m JPathHelper.addSeparator(System.getProperty("user.home")) + JSettings.SETTINGS_FILE, "1.8"); Main.m_sysLocale = Locale.getDefault(); if (Main.m_settings.getLocale().length() > 0) { Locale.setDefault(new Locale(Main.m_settings.getLocale())); } } SGRColumnOrdering columns = SGRColumnOrdering.getInstance(); String[] fields = columns.getFields(); DefaultTableModel resultsTableModel = new DefaultTableModel(columns.getHeadings(), 0); final List<List<Color>> rowColors = new LinkedList<List<Color>>(); for (Match result : results) { SGRRecord match = result.match; float score = result.score; float maxScore = 22.0f; //results.maxScore; List<Color> cellColors = new LinkedList<Color>(); List<String> data = new LinkedList<String>(); for (String field : fields) { if (field.equals("id")) { data.add(match.id); cellColors.add(SGRColors.colorForScore(score, maxScore)); } else if (field.equals("score")) { data.add(((Float) score).toString()); cellColors.add(SGRColors.colorForScore(score, maxScore)); } else { data.add(StringUtils.join(match.getFieldValues(field).toArray(), ';')); Float fieldContribution = result.fieldScoreContributions().get(field); Color color = SGRColors.colorForScore(score, maxScore, fieldContribution); cellColors.add(color); } } resultsTableModel.addRow(data.toArray()); rowColors.add(cellColors); } JTable table = createTable(resultsTableModel, rowColors); Dimension preferredSize = table.getPreferredSize(); preferredSize.width = Math.min((int) (0.9 * width), preferredSize.width); table.setPreferredScrollableViewportSize(preferredSize); table.getColumnModel().addColumnModelListener(this); add(new JScrollPane(table)); }
From source file:net.pandoragames.far.ui.swing.FileListPanel.java
private void init(SwingConfig config, ComponentRepository componentRepository) { this.setLayout(new BorderLayout()); this.setBorder( BorderFactory.createEmptyBorder(0, SwingConfig.PADDING, SwingConfig.PADDING, SwingConfig.PADDING)); tableModel = componentRepository.getTableModel(); componentRepository.getResetDispatcher().addResetable(tableModel); componentRepository.getSearchBaseListener().addResetable(tableModel); componentRepository.getUndoListener().setTableModel(tableModel); JTable fileListTable = componentRepository.getFileSetTable(); int totalWidth = fileListTable.getPreferredSize().width; fileListTable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); fileListTable.setColumnSelectionAllowed(true); fileListTable.getTableHeader().addMouseListener(new TableHeaderMouseListener()); fileListTable.getTableHeader().getColumnModel().getColumn(0) .setHeaderRenderer(new TableHeaderCheckBoxColumnRenderer()); fileListPopupMenu = new FileListPopupMenu(fileListTable, tableModel, componentRepository, config); fileListTable.setComponentPopupMenu(fileListPopupMenu); fileListTable.addMouseListener(new FileViewOpener(fileListTable, componentRepository.getRootWindow(), config, componentRepository)); fileListTable.getColumnModel().getColumn(0).setPreferredWidth(20); fileListTable.getColumnModel().getColumn(0).setMaxWidth(20); fileListTable.getColumnModel().getColumn(1).setCellRenderer(new TargetFileListTableCellRenderer()); fileListTable.getColumnModel().getColumn(1).setPreferredWidth(2 * totalWidth / 5); fileListTable.getColumnModel().getColumn(2).setCellRenderer(new PathColumnRenderer()); fileListTable.getColumnModel().getColumn(3).setCellRenderer(new InfoColumnRenderer(config)); JScrollPane scrollPane = new JScrollPane(fileListTable); this.add(scrollPane, BorderLayout.CENTER); SelectCounter fileCounter = new SelectCounter(); fileCounter.setBorder(BorderFactory.createEmptyBorder(1, SwingConfig.PADDING, 2, SwingConfig.PADDING)); fileCounter.setForeground(Color.GRAY); ErrorCounter errorCounter = new ErrorCounter(); errorCounter.setBorder(BorderFactory.createEmptyBorder(1, SwingConfig.PADDING, 2, SwingConfig.PADDING)); JPanel counterLine = new JPanel(); counterLine.setLayout(new BorderLayout()); counterLine.add(fileCounter, BorderLayout.WEST); counterLine.add(errorCounter, BorderLayout.EAST); JProgressBar progressBar = new JProgressBar(); progressBar.setEnabled(false);/*from w w w. j a va2 s .c o m*/ progressBar.setMaximumSize(new Dimension(100, 20)); progressBar.setBorder(BorderFactory.createEmptyBorder(1, SwingConfig.PADDING, 2, SwingConfig.PADDING)); componentRepository.getProgressBarUpdater().setProgressBar(progressBar); JPanel progressBarPanel = new JPanel(); progressBarPanel.add(progressBar); counterLine.add(progressBarPanel, BorderLayout.CENTER); counterLine.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1)); this.add(counterLine, BorderLayout.SOUTH); tableModel.addTableModelListener(new ColumnCountListener(fileCounter)); tableModel.addTableModelListener(errorCounter); componentRepository.getResetDispatcher().addResetable(fileCounter); componentRepository.getSearchBaseListener().addResetable(fileCounter); componentRepository.getOperationCallBackListener().addComponentStartReseted(fileCounter, OperationType.FIND); componentRepository.getResetDispatcher().addResetable(errorCounter); componentRepository.getSearchBaseListener().addResetable(errorCounter); viewAction = new ActionView(componentRepository, config); }
From source file:de.uka.aifb.com.systemDynamics.gui.ModelExecutionChartPanel.java
/** * Creates panel./* w w w . j a va2 s . c om*/ */ private void createPanel() { setLayout(new BorderLayout()); // CENTER: chart ChartPanel chartPanel = new ChartPanel(createChart()); // no context menu chartPanel.setPopupMenu(null); // not zoomable chartPanel.setMouseZoomable(false); add(chartPanel, BorderLayout.CENTER); // LINE_END: series table JPanel tablePanel = new JPanel(new GridBagLayout()); String[] columnNames = { messages.getString("ModelExecutionChartPanel.Table.ColumnNames.ExtraAxis"), messages.getString("ModelExecutionChartPanel.Table.ColumnNames.LevelNode") }; final MyTableModel tableModel = new MyTableModel(columnNames, xySeriesArray.length); for (int i = 0; i < xySeriesArray.length; i++) { tableModel.addEntry((String) xySeriesArray[i].getKey()); } JTable table = new JTable(tableModel); table.setRowSelectionAllowed(false); JScrollPane tableScrollPane = new JScrollPane(table); int width = (int) Math.min(300, table.getPreferredSize().getWidth()); int height = (int) Math.min(200, table.getPreferredSize().getHeight()); tableScrollPane.getViewport().setPreferredSize(new Dimension(width, height)); tableScrollPane.setMaximumSize(tableScrollPane.getViewport().getPreferredSize()); axesButton = new JButton(messages.getString("ModelExecutionChartPanel.AxesButton.Text")); axesButton.setToolTipText(messages.getString("ModelExecutionChartPanel.AxesButton.ToolTipText")); axesButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // create XYSeriesCollections (and renderer) XYSeriesCollection standardData = new XYSeriesCollection(); XYLineAndShapeRenderer standardRenderer = new XYLineAndShapeRenderer(true, false); LinkedList<XYSeriesCollection> extraDataList = new LinkedList<XYSeriesCollection>(); LinkedList<XYLineAndShapeRenderer> extraRendererList = new LinkedList<XYLineAndShapeRenderer>(); for (int i = 0; i < tableModel.getRowCount(); i++) { if (tableModel.getValueAt(i, 0).equals(Boolean.FALSE)) { standardData.addSeries(xySeriesArray[i]); standardRenderer.setSeriesPaint(standardData.getSeriesCount() - 1, DefaultDrawingSupplier.DEFAULT_PAINT_SEQUENCE[i % DefaultDrawingSupplier.DEFAULT_PAINT_SEQUENCE.length]); } else { // extra axis XYSeriesCollection extraData = new XYSeriesCollection(); extraData.addSeries(xySeriesArray[i]); extraDataList.add(extraData); XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, false); extraRendererList.add(renderer); renderer.setSeriesPaint(0, DefaultDrawingSupplier.DEFAULT_PAINT_SEQUENCE[i % DefaultDrawingSupplier.DEFAULT_PAINT_SEQUENCE.length]); } } LinkedList<XYSeriesCollection> dataList = new LinkedList<XYSeriesCollection>(); LinkedList<XYLineAndShapeRenderer> rendererList = new LinkedList<XYLineAndShapeRenderer>(); if (!standardData.getSeries().isEmpty()) { dataList.add(standardData); rendererList.add(standardRenderer); } for (XYSeriesCollection data : extraDataList) { dataList.add(data); } for (XYLineAndShapeRenderer renderer : extraRendererList) { rendererList.add(renderer); } // creates axes LinkedList<NumberAxis> axesList = new LinkedList<NumberAxis>(); if (!standardData.getSeries().isEmpty()) { NumberAxis axis = new NumberAxis(messages.getString("ModelExecutionChartPanel.Value")); axis.setNumberFormatOverride(NumberFormat.getInstance(locale)); axesList.add(axis); } for (XYSeriesCollection data : extraDataList) { NumberAxis axis = new NumberAxis((String) data.getSeries(0).getKey()); axis.setNumberFormatOverride(NumberFormat.getInstance(locale)); axesList.add(axis); } // store data and axes in plot XYPlot plot = chart.getXYPlot(); plot.clearRangeAxes(); plot.setRangeAxes(axesList.toArray(new NumberAxis[0])); for (int i = 0; i < plot.getDatasetCount(); i++) { plot.setDataset(i, null); } int datasetIndex = 0; Iterator<XYSeriesCollection> datasetIterator = dataList.iterator(); Iterator<XYLineAndShapeRenderer> rendererIterator = rendererList.iterator(); while (datasetIterator.hasNext()) { plot.setDataset(datasetIndex, datasetIterator.next()); plot.setRenderer(datasetIndex, rendererIterator.next()); datasetIndex++; } for (int i = 0; i < plot.getDatasetCount(); i++) { plot.mapDatasetToRangeAxis(i, i); } } }); GridBagConstraints c = new GridBagConstraints(); c.anchor = GridBagConstraints.CENTER; c.gridx = 0; c.gridy = 0; c.insets = new Insets(0, 0, 10, 0); tablePanel.add(tableScrollPane, c); c.gridx = 0; c.gridy = 1; tablePanel.add(axesButton, c); add(tablePanel, BorderLayout.LINE_END); // PAGE_END: number of rounds and execution button JPanel commandPanel = new JPanel(); commandPanel.add(new JLabel(messages.getString("ModelExecutionChartPanel.NumberRounds"))); final JTextField numberRoundsField = new JTextField("1", 5); numberRoundsField.addFocusListener(this); commandPanel.add(numberRoundsField); executionButton = new JButton(messages.getString("ModelExecutionChartPanel.ExecutionButton.Text")); executionButton.setToolTipText(messages.getString("ModelExecutionChartPanel.ExecutionButton.ToolTipText")); executionButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int numberRounds = 0; boolean correctNumber = false; try { numberRounds = integerNumberFormatter.parse(numberRoundsField.getText()).intValue(); } catch (ParseException parseExcep) { // do nothing } if (numberRounds >= 1) { correctNumber = true; } if (correctNumber) { ModelExecutionThread executionThread = new ModelExecutionThread(numberRounds); executionThread.start(); } else { JOptionPane.showMessageDialog(null, messages.getString("ModelExecutionChartPanel.Error.Message"), messages.getString("ModelExecutionChartPanel.Error.Title"), JOptionPane.ERROR_MESSAGE); } } }); commandPanel.add(executionButton); add(commandPanel, BorderLayout.PAGE_END); }
From source file:com.view.TradeWindow.java
private void TraderBlockOrdersActionPerformed(java.awt.event.ActionEvent evt) { TableModel dtm = (TableModel) TraderIncomingRequestsTable.getModel(); int nRow = dtm.getRowCount(); int nCol = dtm.getColumnCount(); Object[][] tableData = new Object[nRow][nCol]; ArrayList<SingleOrder> parsedOrders = new ArrayList(); ControllerBlockOrders control = new ControllerBlockOrders(); for (int i = 0; i < nRow; i++) { for (int j = 0; j < nCol; j++) { tableData[i][j] = dtm.getValueAt(i, j); }/*from w w w . jav a2s . co m*/ SingleOrder o = new SingleOrder(); o.SingleOrderMakeBlocks(tableData[i]); parsedOrders.add(o); } singleOrderLists = control.MakeBlock(parsedOrders); showMessageDialog(null, "Blocks have been successfully completed."); //dtm.setRowCount(0); TraderPlatformBlockedRequests.setLayout(new BorderLayout()); int count = 1; ArrayList<JScrollPane> paneList = new ArrayList<JScrollPane>(); for (ArrayList<SingleOrder> b : singleOrderLists) { JTable jTable = new JTable(); jTable.setModel(CTraderBlockOrder.getTableModel(b)); Dimension d = jTable.getPreferredSize(); // System.out.println(d); int rows = jTable.getRowCount(); // System.out.println(rows); JScrollPane jPane = new JScrollPane(); jPane.setPreferredSize(new Dimension(d.width, jTable.getRowHeight() * rows + 50)); jPane.add(jTable); jPane.setViewportView(jTable); paneList.add(jPane); count++; } test.add(blockOptions); int i = 0; for (final JScrollPane j : paneList) { // JButton btn = new JButton(); // btn.setText("Split Block"); // btn.setName(""+i); JPanel cPanel = new JPanel(); /* btn.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { JViewport viewport = j.getViewport(); final JTable mytable = (JTable)viewport.getView(); final ArrayList<Integer> index = new ArrayList<Integer>(); for(int row = 0;row<mytable.getRowCount();row++){ if((boolean)mytable.getValueAt(row, 11)){ index.add(row); } } SplitBlockActionPerformed(evt,index,cPanel,test); } });*/ JCheckBox check = new JCheckBox(); JLabel label = new JLabel(); label.setText("Select Block"); check.setName("" + i); check.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { SelectBlockActionPerformed(evt); } }); JPanel splitOptions = new JPanel(); splitOptions.add(label); splitOptions.add(check); splitOptions.setName("splitOpt"); // splitOptions.add(btn); cPanel.setName("cPanel" + i); cPanel.add(splitOptions); cPanel.add(j); cPanel.setLayout(new BoxLayout(cPanel, BoxLayout.Y_AXIS)); test.add(cPanel); cPanelList.add(cPanel); i++; } test.setLayout(new BoxLayout(test, BoxLayout.Y_AXIS)); JScrollPane p = new JScrollPane(test); p.setName("ParentP"); TraderPlatformBlockedRequests.add(p); TraderPlatformBlockedRequests.validate(); TraderPlatformTabbedPane.setSelectedIndex(TraderPlatformTabbedPane.getSelectedIndex() + 1); }