1

I have been making a small application containing JTable. The question is this: when I am adding the JTable to the JScrollPane, it is giving me the error that the table component is added to a parent component more than once.

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

I see the problem but could not solve it. Any suggestions?

  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();
  }

}

Latif
  • 125
  • 1
  • 4
  • 16
  • take a look at this page: http://stackoverflow.com/questions/10619048/adding-a-jscrollpane-to-a-jtable-component – anion Sep 08 '15 at 13:28
  • 1
    `I see the problem but could not solve it.` - what do you mean you can't solve it? Get rid of one of those statements? We can't tell you what to do because we don't know what your requirement is. – camickr Sep 08 '15 at 14:26
  • Thanks, Yes, I did took out one of those statements but when i took out, the table did not display. – Latif Sep 09 '15 at 06:16

2 Answers2

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

Should work perfectly.

Rick
  • 1,172
  • 11
  • 25
2

The error is correct: a JScrollPane can only have one "scrollable client (aka data model) displayed by the JViewport view." You only need setViewportView() if you "don't provide the view directly to the JScrollPane constructor."

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

As an aside, don't use setBounds() in this way. Instead, override the Scrollable interface method getPreferredScrollableViewportSize() and pack() the enclosing window, as shown here.

JTable table = new JTable(model) {

    @Override
    public Dimension getPreferredScrollableViewportSize() {
        return new Dimension(1024, 512);
    }
};
add(new JScrollPane(table));
pack();
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks, I tried the way you showed but this time the table did not display. – Latif Sep 09 '15 at 07:20
  • @Laddie: You might verify that the [example](http://stackoverflow.com/a/13919878/230513) works and then compare it to your code. Ensure that `setVisible()` _follows_ construction. Failing that, please edit your question to include a [complete example](http://stackoverflow.com/help/mcve) that exhibits the problem. – trashgod Sep 09 '15 at 07:34
  • Now you can take a look at it. by the way , don not care about the nulls . – Latif Sep 09 '15 at 07:50
  • @Laddie: That's way too much uncompilable code. I still see two instances of `JScrollPane` with the same table and a premature call to `setVisible()`. – trashgod Sep 09 '15 at 08:14
  • Thanks again. But I only used one JScrollPane for the table . – Latif Sep 09 '15 at 09:36
  • When i get rid of add(new JScrollPane(table)); it gave me the error WindowBuilder encountered unexpected internal error. This could be caused by a WindowBuilder bug or by a misconfiguration issue, conflict, partial update, etc. java.lang.ArrayIndexOutOfBoundsException: 22 – Latif Sep 09 '15 at 09:40
  • And when get rid of scrollPane.setViewportView(table); it gave me: the table component is added to a parent component more than once. getContentPane().add(new JScrollPane(table)); scrollPane.setViewportView(table); – Latif Sep 09 '15 at 09:42