vertical ejemplo java swing jtable jscrollpane

java - ejemplo - Agregar JTable a JScrollPane



jtable scroll vertical (2)

El error es correcto: un JScrollPane solo puede tener un "cliente desplazable (también conocido como modelo de datos) mostrado por la vista de JViewport ". Solo necesita setViewportView() si " no proporciona la vista directamente al constructor JScrollPane ".

JTable table = new JTable(data, col); add(new JScrollPane(table));

Como un aparte, no use setBounds() de esta manera. En su lugar, anule el método de interfaz getPreferredScrollableViewportSize() y pack() la ventana getPreferredScrollableViewportSize() , como se muestra aquí .

JTable table = new JTable(model) { @Override public Dimension getPreferredScrollableViewportSize() { return new Dimension(1024, 512); } }; add(new JScrollPane(table)); pack();

He estado haciendo una pequeña aplicación que contiene JTable . La pregunta es la siguiente: cuando agrego JTable al JScrollPane , me da el error de que el componente de la tabla se agrega a un componente primario más de una vez.

getContentPane().add(new JScrollPane(table)); scrollPane.setViewportView(table);

Veo el problema pero no pude resolverlo. ¿Alguna sugerencia?

public thirdFrame() { thirdFrame = new JFrame(); thirdFrame.setFont(new Font("David", Font.BOLD, 17)); thirdFrame.setTitle("MGMSportif"); thirdFrame.setIconImage(Toolkit.getDefaultToolkit().getImage("C://Users//EXP//Desktop//mgm//src//MGM-LOGO.png")); thirdFrame.setBounds(100, 100, 1600, 620); thirdFrame.setVisible(true); thirdFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); thirdFrame.getContentPane().setLayout(null); JDesktopPane desktop = new JDesktopPane(); desktop.setOpaque(true); thirdFrame.getContentPane().add(desktop); JInternalFrame internalFrame = new JInternalFrame("MGMSportif ",false,true,false,true); internalFrame.setFrameIcon(new ImageIcon("C://Users//EXP//Desktop//mgm//src//reized logo.png")); internalFrame.setBounds(209, 126, 720, 400); JToolBar toolBar = new JToolBar(); toolBar.setBounds(0, 0, 1362, 32); thirdFrame.getContentPane().add(toolBar); JButton bt = new JButton(""); bt.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); bt.setToolTipText("Dosya a/u00E7mak i/u00E7in t/u0131klay/u0131n"); bt.setIcon(new ImageIcon("C://Users//EXP//Desktop//mgm//src//open-file-icon.png")); toolBar.add(bt); JButton bt1 = new JButton(""); bt1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Save sF =new Save(); sF.setVisible(false); dispose(); } }); bt1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); bt1.setToolTipText("Kay/u0131d etmek i/u00E7in t/u0131klay/u0131n"); bt1.setIcon(new ImageIcon("C://Users//EXP//Desktop//mgm//src//save.png")); toolBar.add(bt1); JButton bt2 = new JButton(""); bt2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); bt2.setToolTipText("Kesmek i/u00E7in t/u0131klay/u0131n"); bt2.setIcon(new ImageIcon("C://Users//EXP//Desktop//mgm//src//Cut-icon.png")); toolBar.add(bt2); JButton bt3 = new JButton(""); bt3.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); bt3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { internalFrame.setVisible(true); } }); thirdFrame.getContentPane().add(internalFrame); internalFrame.getContentPane().setLayout(null); JLabel lb = new JLabel("Tarihe g/u00F6re ara"); lb.setFont(new Font("Tahoma", Font.PLAIN, 13)); lb.setBounds(10, 11, 110, 26); internalFrame.getContentPane().add(lb); tF = new JTextField(); tF.setToolTipText("Aramak isteyen tarihi giriniz"); tF.setBounds(144, 16, 445, 20); internalFrame.getContentPane().add(tF); tF.setColumns(10); JProgressBar pBar = new JProgressBar(0,100); pBar.setString(""); pBar.setStringPainted(true); pBar.setBounds(10, 67, 684, 21); internalFrame.getContentPane().add( pBar); JButton sesarchButton = new JButton(""); sesarchButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); sesarchButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { sesarchButton.setEnabled(false); setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); pBar.setVisible(true); repaint(); for(int i=0;i<=100;i++){ final int percent = i; pBar.setValue(i); // jProgressBar1.setIndeterminate(false); try{ pBar.paintImmediately(0, 0, 100, 100);//0, 1, 100, 10 Thread.sleep(100); pBar.setStringPainted(true); } catch (InterruptedException err){} } } }); sesarchButton.setIcon(new ImageIcon("C://Users//EXP//Desktop//mgm//src//search48.png")); sesarchButton.setBounds(599, 7, 57, 56); internalFrame.getContentPane().add(sesarchButton); JLabel lb1 = new JLabel("Makineye g/u00F6re ara"); lb1.setFont(new Font("Tahoma", Font.PLAIN, 13)); lb1.setBounds(10, 43, 124, 20); internalFrame.getContentPane().add( lb1); tF1 = new JTextField(); tF1 .setBounds(144, 42, 445, 20); internalFrame.getContentPane().add(tF1 ); tF1 .setColumns(10); tF1 .setBounds(144, 42, 445, 20); JList list = new JList(); list.setBounds(10, 99, 684, 360); internalFrame.getContentPane().add(list); bt3.setToolTipText("Dosya aramak i/u00E7in t/u0131klay/u0131n"); bt3.setIcon(new ImageIcon("C://Users//EXP//Desktop//mgm//src//Zoom-icon.png")); toolBar.add( bt3); JButton bt4 = new JButton(""); ActionListener printAction = new ActionListener() { public void actionPerformed(ActionEvent e) { try { MessageFormat headerFormat = new MessageFormat("Page {0}"); MessageFormat footerFormat = new MessageFormat("- {0} -"); table.print(JTable.PrintMode.FIT_WIDTH, headerFormat, footerFormat); } catch (PrinterException pe) { System.err.println("Error printing: " + pe.getMessage()); } } }; bt4.addActionListener(printAction); bt4.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); bt4.setToolTipText("/u00C7/u0131kt/u0131 almak i/u00E7in t/u0131klay/u0131n"); bt4.setIcon(new ImageIcon("C://Users//EXP//Desktop//mgm//src//print-icon16.png")); toolBar.add(bt4); JButton bt5 = new JButton(""); bt5.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); bt5.setToolTipText("Pencereyi kapatmak i/u00E7in t/u0131klay/u0131n"); bt5.setIcon(new ImageIcon("C://Users//EXP//Desktop//mgm//src//Close.png")); toolBar.add(bt5); JCheckBox checkBox = new JCheckBox("Enjeksyon Makine No:"); checkBox.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED){ tF2.setEnabled(true); } else if(e.getStateChange() == ItemEvent.DESELECTED){ tF2.setEnabled(false); } } }); checkBox.setFont(new Font("Tahoma", Font.PLAIN, 11)); checkBox.setBounds(1015, 39, 131, 23); thirdFrame.getContentPane().add(checkBox); tF2 = new JTextField(); tF2.setBounds(1152, 43, 31, 19); thirdFrame.getContentPane().add( tF2); tF2.setColumns(10); JCheckBox checkBox1 = new JCheckBox("Kaynak Makine No:"); checkBox1.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED){ tF3.setEnabled(true); tF3.setText(""); } else if(e.getStateChange() == ItemEvent.DESELECTED){ tF3.setEnabled(false); tF3.setText(""); } } }); checkBox1.setFont(new Font("Tahoma", Font.PLAIN, 11)); checkBox1.setBounds(337, 39, 117, 23); thirdFrame.getContentPane().add(checkBox1); tF3 = new JTextField(); tF3.setBounds(460, 43, 31, 19); thirdFrame.getContentPane().add(tF3); tF3.setColumns(10); JCheckBox checkBox2 = new JCheckBox("Otomatsyon Makine No:"); checkBox2.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED){ tF4.setEnabled(true); tF4.setText(""); } else if(e.getStateChange() == ItemEvent.DESELECTED){ tF4.setEnabled(false); tF4.setText(""); } } }); checkBox2.setFont(new Font("Tahoma", Font.PLAIN, 11)); checkBox2.setBounds(577, 39, 141, 23); thirdFrame.getContentPane().add(checkBox2); tF4 = new JTextField(); tF4.setBounds(724, 43, 31, 19); thirdFrame.getContentPane().add(tF4); tF4.setColumns(10); JCheckBox checkBox3 = new JCheckBox("Pakitleme:"); checkBox3.setFont(new Font("Tahoma", Font.PLAIN, 11)); checkBox3.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if(e.getStateChange() == ItemEvent.SELECTED){ tF5.setEnabled(true); tF5.setText(""); } else if(e.getStateChange() == ItemEvent.DESELECTED){ tF5.setEnabled(false); tF5.setText(""); } } }); checkBox3.setBounds(844, 39, 85, 23); thirdFrame.getContentPane().add(checkBox3); tF5 = new JTextField(); tF5.setBounds(934, 43, 31, 19); thirdFrame.getContentPane().add(tF5); tF5.setColumns(10); JLabel lb2 = new JLabel(" Vardiya 1 Vardiya 2"); lb2.setForeground(SystemColor.desktop); lb2.setFont(new Font("Tahoma", Font.PLAIN, 13)); lb2.setBounds(0, 73, 1341,16); thirdFrame.getContentPane().add(lb2); JSeparator separator = new JSeparator(); separator.setBounds(0, 69, 1362, 2); thirdFrame.getContentPane().add(separator); JLabel lblNewLabel = new JLabel("TOSCOW"); lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 15)); lblNewLabel.setBounds(108, 43, 97, 19); thirdFrame.getContentPane().add(lblNewLabel); String[] col = new String[] { "TAR/u0130H", "BEYAZ", "KIRMIZI", "MAV/u0130", "SARI", "S/u0130YAH", "New column", "ADET", "PERS.", "/u00DCR.NEDEN/u0130", "Toplam", "TAR/u0130H", "BEYAZ", "KIRMIZI", "MAV/u0130", "SARI", "S/u0130YAH","New column", "ADET", "PERS.", "/u00DCR.NEDEN/u0130", "Toplam", "2VDT" }; Object[][] data = new Object[][] { {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}, {null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null}, }; JTable table = new JTable(data, col); add(new JScrollPane(table)); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(20, 94, 1324, 455); thirdFrame.getContentPane().add(scrollPane); scrollPane.setViewportView(table); // header header = table.getTableHeader(); header.addMouseListener(new MouseAdapter(){ @Override public void mouseClicked(MouseEvent event) { if (event.getClickCount() == 2) { editColumnAt(event.getPoint()); } } }); text = new JTextField(); text.setBorder(null); text.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { renameColumn(); } }); renamePopup = new JPopupMenu(); renamePopup.setBorder(new MatteBorder(0, 1, 1, 1, Color.DARK_GRAY)); renamePopup.add(text);

}

private void editColumnAt(Point p) { int columnIndex = header.columnAtPoint(p); if (columnIndex != -1) { column = header.getColumnModel().getColumn(columnIndex); Rectangle columnRectangle = header.getHeaderRect(columnIndex); text.setText(column.getHeaderValue().toString()); renamePopup.setPreferredSize( new Dimension(columnRectangle.width, columnRectangle.height - 1)); renamePopup.show(header, columnRectangle.x, 0); text.requestFocusInWindow(); text.selectAll(); } } private void renameColumn() { column.setHeaderValue(text.getText()); renamePopup.setVisible(false); header.repaint(); }

}


JTable table = new JTable(); JScrollPane scrollPane = new JScrollPane(table); JFrame frame = new JFrame(); frame.add(scrollPane);

Debería funcionar perfectamente