Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JTable setRowCount(0) gives error!

843805Apr 6 2007 — edited Apr 8 2007
hi, i am creating a database application. I am trying to utilise the JTable as a shopping basket. I have create a JTable with 2 buttons, 1 to add item and another to clear the whole table. I've read some forum post that i can use the method TableModel/Table .setRowCount(0) but i keep having errors when i try to do it.
it generates this to be exact >>
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0

Below is part of the code. If you need the rest pls just let me know.
import java.awt.*; import java.awt.event.*; import javax.swing.*;
import javax.swing.table.*;import javax.swing.border.*;

public class fpanel extends JPanel implements ActionListener{
	PaulTM PTM1 = new PaulTM();
	JTable t1 = new JTable(PTM1);


    
	public fpanel(){
		t1.setDefaultRenderer(Object.class, new PaulCell());

		//t1.setIntercellSpacing(new Dimension(1, 1));
		t1.setShowHorizontalLines(false);
		t1.setShowVerticalLines(true);
		t1.setGridColor(Color.lightGray);
 		JScrollPane sp1 = new JScrollPane(t1);
 		t1.setPreferredScrollableViewportSize(new Dimension(650, 70));

		JButton press1 = new JButton("Press Me Now");
		JButton press2 = new JButton("Clear Me Now");
		press1.addActionListener(this);
		press2.addActionListener(this);

		add(press1);
		add(press2);
        add(sp1);
		setBorder(new TitledBorder("Search"));
    }


		public void actionPerformed(ActionEvent e){
		
		String acstring = e.getActionCommand();
			
		if(acstring.equals("Press Me Now")){
			System.out.println(acstring);
			Object c1[]={"Mental", "Boy", "Fo", "456.76"};
			t1.setValueAt("Jason Bonham",1,1);
System.out.println(t1.getRowCount()); 

		}
		else if(acstring.equals("Clear Me Now")){
			System.out.println(acstring);
PTM1.setRowCount(0);
	}
    
    }




private class PaulCell extends DefaultTableCellRenderer{
	private Color whiteColor = new Color(255, 255, 254);
	private Color alternateColor = new Color(237, 243, 254);
	private Color selectedColor = new Color(61, 128, 223);

	public Component getTableCellRendererComponent(JTable table,Object value, boolean selected, boolean focused,int row, int column){
			super.getTableCellRendererComponent(table, value,selected, focused, row, column);

	// Set the background color
	Color bg;
	if (!selected)
		bg = (row % 2 == 0 ? alternateColor : whiteColor);
	else
		bg = selectedColor;
		setBackground(bg);

	// Set the foreground to white when selected
	Color fg;
	if (selected)
		fg = Color.white;
	else
		fg = Color.black;
		setForeground(fg);

return this;
		}

	}

}
Message was edited by:
bronwerkz

Message was edited by:
bronwerkz

Message was edited by:
bronwerkz
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 6 2007
Added on Apr 6 2007
4 comments
1,133 views