Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Exception in VM code with JTable after inserting rows

0 views
Skip to first unread message

test

unread,
Feb 2, 2005, 9:46:16 AM2/2/05
to
Hello,

i have a problem with inserting rows in a JTable.

I have a MainFrame, wich had a ControlPanel (self made) wich had a ScrollPane in it and there is a JTable in the scroll pane.

Here is the important code (mostly generated by eclipse):

... MainFrame.java
private void initialize() {
this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
this.setJMenuBar(getJJMenuBar());
this.setSize(489, 220);
this.setContentPane(getJContentPane());
this.setTitle("Application");
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent e) {
System.out.println("windowOpened()"); // TODO Auto-generated Event stub windowOpened()

// *** added ***
controlPanel.generateData();
}
});
}

...ControlPanel.java
public void generateData() {
int count = 500;

String[] columnNames = { "x", "y", "couleur" };

jTable.removeAll();

TableColumn x = new TableColumn(0);
x.setHeaderValue("x");
TableColumn y = new TableColumn(1);
y.setHeaderValue("y");
TableColumn color = new TableColumn(2);
color.setHeaderValue("color");

jTable.addColumn(x);
jTable.addColumn(y);
jTable.addColumn(color);


jTable.doLayout();

jScrollPane.revalidate();

DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();

boolean exceptionWillOccur = false;
this.setVisible(exceptionWillOccur);

System.err.println("DEBUG: " + tableModel.toString());

for (int i = 0; i < count; i++) {
String[] line = new String[] { "" + Math.random(), "" + Math.random(),
getRandomColorString() };

tableModel.addRow(line);
//jTable.repaint();
}
}


The boolean "exceptionWillOccur" below reflect the exception behavior :)

If set to true, here is what i get, if false, no problem, everything is drawn corectly.

windowOpened()
DEBUG: javax.swing.table.DefaultTableModel@1b48197
java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Vector.java:431)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:618)
at javax.swing.JTable.getValueAt(JTable.java:1771)
at javax.swing.JTable.prepareRenderer(JTable.java:3724)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:1149)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1051)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:974)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:142)
at javax.swing.JComponent.paintComponent(JComponent.java:541)
at javax.swing.JComponent.paint(JComponent.java:808)
at javax.swing.JComponent.paintChildren(JComponent.java:647)
at javax.swing.JComponent.paint(JComponent.java:817)
at javax.swing.JViewport.paint(JViewport.java:722)
at javax.swing.JComponent.paintChildren(JComponent.java:647)
at javax.swing.JComponent.paint(JComponent.java:817)
at javax.swing.JComponent.paintWithOffscreenBuffer(JComponent.java:4787)
at javax.swing.JComponent.paintDoubleBuffered(JComponent.java:4740)
at javax.swing.JComponent._paintImmediately(JComponent.java:4685)
at javax.swing.JComponent.paintImmediately(JComponent.java:4488)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:410)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:117)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:178)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:454)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)


I believe the exception is because i modify the rows before some change have been notified, but i don't manage to make it work.

Plz help

John McGrath

unread,
Feb 2, 2005, 5:09:21 PM2/2/05
to
On 2/2/2005 at 9:46:16 AM, test wrote:

> i have a problem with inserting rows in a JTable.

> Here is the important code (mostly generated by eclipse):

> boolean exceptionWillOccur = false;
> this.setVisible(exceptionWillOccur);

> The boolean "exceptionWillOccur" below reflect the exception behavior :)

> If set to true, here is what i get, if false, no problem, everything is
> drawn corectly.

If exceptionWillOccur is false, the table is not visible, so it does not
paint the table and it does not call the TableModel's getValueAt() method.
If it is painting the table, then you have left out some important code.

Would you post a runnable example of the problem? I do not think the
cause can be determined from what you have posted.

--
Regards,

John McGrath

Nigel Wade

unread,
Feb 3, 2005, 5:47:29 AM2/3/05
to
test wrote:

I think the problem is threading. If you follow the Sun guidelines for
creating a GUI hopefully you should eliminate this.

What you are doing is to add the rows to the table in the main thread. This
is (possibly) ok if the component in question has not yet been realized.
However, once you realize the component (and setVisible will do that) the
event dispatch thread will start to draw it. Whilst the component is being
drawn your thread continues on its merry way and modifies it. Hence the
error.

Once the EDT has started you should not modify any components in your own
threads. There are some exceptions, but as a general rule it's best to
stick to only changing components in the EDT.

If you modify your code so that it executes in the EDT by putting it in an
invokeLater() call it might start working (there may be other problems, of
course). For examples of how to do this look at the Java Tutorial "Creating
a GUI with JFC/Swing". There, all GUI initialization is done in a method
called createAndShowGUI. That method is passed to the EDT from the main
thread by:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});

--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : n...@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555

test

unread,
Feb 4, 2005, 9:39:43 AM2/4/05
to
Ok, i've not yet tested the solution of creating a new thread/runable of Nigel.

So here is the full code:

ControlPanel.java-------------------------------
/*
* Created on 2 févr. 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package test01;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
* @author mancel
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class ControlPanel extends JPanel {

private JTable jTable = null;

private JScrollPane jScrollPane = null;

/**
* This is the default constructor
*/
public ControlPanel() {
super();
initialize();
}

/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setLayout(new BorderLayout());
this.setSize(300, 200);
this.add(getJScrollPane(), java.awt.BorderLayout.NORTH);
}

/**
* This method initializes jTable
*
* @return javax.swing.JTable
*/
private JTable getJTable() {
if (jTable == null) {
jTable = new JTable();
}
return jTable;
}

/**
* This method initializes jScrollPane1
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setViewportView(getJTable());
}
return jScrollPane;
}

public void generateData() {
int count = 500;

String[] columnNames = { "x", "y", "couleur" };

//jTable.removeAll();



TableColumn x = new TableColumn(0);
x.setHeaderValue("x");
TableColumn y = new TableColumn(1);
y.setHeaderValue("y");
TableColumn color = new TableColumn(2);
color.setHeaderValue("color");

//TableColumnModel columns = jTable.getColumnModel();
jTable.addColumn(x);
jTable.addColumn(y);
jTable.addColumn(color);


//jTable.doLayout();
//jTable.repaint();

//jScrollPane.revalidate();

/*
jTable.addColumn(x);
jTable.addColumn(y);
jTable.addColumn(color);
*/


DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();

boolean exceptionWillOccur = true;

System.err.println("DEBUG: " + tableModel.toString());

if (exceptionWillOccur) {


for (int i = 0; i < count; i++) {
String[] line = new String[] { "" + Math.random(), "" + Math.random(),
getRandomColorString() };

tableModel.addRow(line);
//jTable.repaint();
}
}
}

public String getRandomColorString() {
Color c = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());

return new String(c.toString());
}
}


DrawPanel.java--------------------------------------
/*
* Created on 2 févr. 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package test01;

import javax.swing.JPanel;

import javax.swing.JButton;
/**
* @author mancel
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class DrawPanel extends JPanel {

private JButton jButton = null;
/**
* This is the default constructor
*/
public DrawPanel() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(300,200);
this.add(getJButton(), null);
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setText("bouton 1");
}
return jButton;
}
}


MainFrame.java-------------------------------------
/*
* Created on 1 févr. 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package test01;

import javax.swing.JFrame;

import javax.swing.JButton;
import java.awt.FlowLayout;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import java.awt.BorderLayout;
/**
* @author mancel
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class MainFrame extends JFrame {

private javax.swing.JPanel jContentPane = null;
private javax.swing.JMenuBar jJMenuBar = null;
private javax.swing.JMenu fileMenu = null;
private javax.swing.JMenu editMenu = null;
private javax.swing.JMenu helpMenu = null;
private javax.swing.JMenuItem exitMenuItem = null;
private javax.swing.JMenuItem aboutMenuItem = null;
private javax.swing.JMenuItem cutMenuItem = null;
private javax.swing.JMenuItem copyMenuItem = null;
private javax.swing.JMenuItem pasteMenuItem = null;
private javax.swing.JMenuItem saveMenuItem = null;
private DrawPanel drawPanel = null;
private ControlPanel controlPanel = null;
/**
* This method initializes drawPanel
*
* @return test01.DrawPanel
*/
private DrawPanel getDrawPanel() {
if (drawPanel == null) {
drawPanel = new DrawPanel();
}
return drawPanel;
}
/**
* This method initializes controlPanel
*
* @return test01.ControlPanel
*/
private ControlPanel getControlPanel() {
if (controlPanel == null) {
controlPanel = new ControlPanel();
}
return controlPanel;
}
public static void main(String[] args) {
MainFrame application = new MainFrame();
application.show();
}
/**
* This is the default constructor
*/
public MainFrame() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/


private void initialize() {
this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
this.setJMenuBar(getJJMenuBar());
this.setSize(489, 220);
this.setContentPane(getJContentPane());
this.setTitle("Application");
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent e) {
System.out.println("windowOpened()"); // TODO Auto-generated Event stub windowOpened()

//

controlPanel.generateData();
}
});
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private javax.swing.JPanel getJContentPane() {
if(jContentPane == null) {
jContentPane = new javax.swing.JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getDrawPanel(), java.awt.BorderLayout.WEST);
jContentPane.add(getControlPanel(), java.awt.BorderLayout.CENTER);
}
return jContentPane;
}
/**
* This method initializes jJMenuBar
*
* @return javax.swing.JMenuBar
*/
private javax.swing.JMenuBar getJJMenuBar() {
if (jJMenuBar == null) {
jJMenuBar = new javax.swing.JMenuBar();
jJMenuBar.add(getFileMenu());
jJMenuBar.add(getEditMenu());
jJMenuBar.add(getHelpMenu());
}
return jJMenuBar;
}
/**
* This method initializes jMenu
*
* @return javax.swing.JMenu
*/
private javax.swing.JMenu getFileMenu() {
if (fileMenu == null) {
fileMenu = new javax.swing.JMenu();
fileMenu.setText("File");
fileMenu.add(getSaveMenuItem());
fileMenu.add(getExitMenuItem());
}
return fileMenu;
}
/**
* This method initializes jMenu
*
* @return javax.swing.JMenu
*/
private javax.swing.JMenu getEditMenu() {
if (editMenu == null) {
editMenu = new javax.swing.JMenu();
editMenu.setText("Edit");
editMenu.add(getCutMenuItem());
editMenu.add(getCopyMenuItem());
editMenu.add(getPasteMenuItem());
}
return editMenu;
}
/**
* This method initializes jMenu
*
* @return javax.swing.JMenu
*/
private javax.swing.JMenu getHelpMenu() {
if (helpMenu == null) {
helpMenu = new javax.swing.JMenu();
helpMenu.setText("Help");
helpMenu.add(getAboutMenuItem());
}
return helpMenu;
}
/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private javax.swing.JMenuItem getExitMenuItem() {
if (exitMenuItem == null) {
exitMenuItem = new javax.swing.JMenuItem();
exitMenuItem.setText("Exit");
exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(0);
}
});
}
return exitMenuItem;
}
/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private javax.swing.JMenuItem getAboutMenuItem() {
if (aboutMenuItem == null) {
aboutMenuItem = new javax.swing.JMenuItem();
aboutMenuItem.setText("About");
aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
new javax.swing.JDialog(MainFrame.this, "About", true).show();
}
});
}
return aboutMenuItem;
}
/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private javax.swing.JMenuItem getCutMenuItem() {
if (cutMenuItem == null) {
cutMenuItem = new javax.swing.JMenuItem();
cutMenuItem.setText("Cut");
cutMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.Event.CTRL_MASK, true));
}
return cutMenuItem;
}
/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private javax.swing.JMenuItem getCopyMenuItem() {
if (copyMenuItem == null) {
copyMenuItem = new javax.swing.JMenuItem();
copyMenuItem.setText("Copy");
copyMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.Event.CTRL_MASK, true));
}
return copyMenuItem;
}
/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private javax.swing.JMenuItem getPasteMenuItem() {
if (pasteMenuItem == null) {
pasteMenuItem = new javax.swing.JMenuItem();
pasteMenuItem.setText("Paste");
pasteMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.Event.CTRL_MASK, true));
}
return pasteMenuItem;
}
/**
* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private javax.swing.JMenuItem getSaveMenuItem() {
if (saveMenuItem == null) {
saveMenuItem = new javax.swing.JMenuItem();
saveMenuItem.setText("Save");
saveMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.Event.CTRL_MASK, true));
}
return saveMenuItem;
}
} // @jve:decl-index=0:visual-constraint="23,15"


Thanx for your help

test

unread,
Feb 4, 2005, 9:58:15 AM2/4/05
to
It's still not working (no line drawn, and one exception)... based on the source code posted before, i've modified this:

ControlPanel.java--------------------------


/*
* Created on 2 févr. 2005
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package test01;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public void generateData() {
int count = 500;

String[] columnNames = { "x", "y", "couleur" };

//jTable.removeAll();



TableColumn x = new TableColumn(0);
x.setHeaderValue("x");
TableColumn y = new TableColumn(1);
y.setHeaderValue("y");
TableColumn color = new TableColumn(2);
color.setHeaderValue("color");

//TableColumnModel columns = jTable.getColumnModel();
jTable.addColumn(x);
jTable.addColumn(y);
jTable.addColumn(color);


//jTable.doLayout();
//jTable.repaint();

//jScrollPane.revalidate();

DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();

boolean exceptionWillOccur = true;



System.err.println("DEBUG: " + tableModel.toString());

if (exceptionWillOccur) {

SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

public String getRandomColorString() {


Color c = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());

return new String(c.toString());
}

private void createAndShowGUI() {


DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();

System.err.println("DEBUG: " + tableModel.toString());

int count = 300;


for (int i = 0; i < count; i++) {
String[] line = new String[] { "" + Math.random(), "" + Math.random(),
getRandomColorString() };

tableModel.addRow(line);
}
}
}

Output:

windowOpened()
DEBUG: javax.swing.table.DefaultTableModel@1b48197


How can i have such a problem after adding so few lines to generated code...

I'm an unlucky man...

test

unread,
Feb 4, 2005, 10:10:10 AM2/4/05
to
I'm back with some more informations:

the columns i add gets deleted!!!

some modified files:

MainFrame.java----------------------------------------------
/*
* Created on 1 févr. 2005


*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package test01;

import javax.swing.JFrame;

import javax.swing.JButton;
import java.awt.FlowLayout;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import javax.swing.SwingUtilities;

import java.awt.BorderLayout;


/**
* @author mancel
*
* TODO To change the template for this generated type comment go to

* Window - Preferences - Java - Code Style - Code Templates
*/

public class MainFrame extends JFrame {

private javax.swing.JPanel jContentPane = null;
private javax.swing.JMenuBar jJMenuBar = null;
private javax.swing.JMenu fileMenu = null;
private javax.swing.JMenu editMenu = null;
private javax.swing.JMenu helpMenu = null;
private javax.swing.JMenuItem exitMenuItem = null;
private javax.swing.JMenuItem aboutMenuItem = null;
private javax.swing.JMenuItem cutMenuItem = null;
private javax.swing.JMenuItem copyMenuItem = null;
private javax.swing.JMenuItem pasteMenuItem = null;
private javax.swing.JMenuItem saveMenuItem = null;
private DrawPanel drawPanel = null;

private ControlPanel controlPanel = null;
/**


* This method initializes drawPanel
*
* @return test01.DrawPanel
*/
private DrawPanel getDrawPanel() {
if (drawPanel == null) {
drawPanel = new DrawPanel();
}
return drawPanel;
}

/**


* This method initializes controlPanel
*
* @return test01.ControlPanel
*/
private ControlPanel getControlPanel() {
if (controlPanel == null) {
controlPanel = new ControlPanel();
}
return controlPanel;
}
public static void main(String[] args) {
MainFrame application = new MainFrame();
application.show();
}

/**
* This is the default constructor
*/

public MainFrame() {


super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {

this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
this.setJMenuBar(getJJMenuBar());
this.setSize(489, 220);
this.setContentPane(getJContentPane());
this.setTitle("Application");
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent e) {
System.out.println("windowOpened()"); // TODO Auto-generated Event stub windowOpened()

//

SwingUtilities.invokeLater(new Runnable() {
public void run() {

controlPanel.generateData();
}
});
}
});
}
/**


* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private javax.swing.JPanel getJContentPane() {
if(jContentPane == null) {
jContentPane = new javax.swing.JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getDrawPanel(), java.awt.BorderLayout.WEST);
jContentPane.add(getControlPanel(), java.awt.BorderLayout.CENTER);
}
return jContentPane;
}

/**


* This method initializes jJMenuBar
*
* @return javax.swing.JMenuBar
*/
private javax.swing.JMenuBar getJJMenuBar() {
if (jJMenuBar == null) {
jJMenuBar = new javax.swing.JMenuBar();
jJMenuBar.add(getFileMenu());
jJMenuBar.add(getEditMenu());
jJMenuBar.add(getHelpMenu());
}
return jJMenuBar;
}

/**


* This method initializes jMenu
*
* @return javax.swing.JMenu
*/
private javax.swing.JMenu getFileMenu() {
if (fileMenu == null) {
fileMenu = new javax.swing.JMenu();
fileMenu.setText("File");
fileMenu.add(getSaveMenuItem());
fileMenu.add(getExitMenuItem());
}
return fileMenu;
}

/**


* This method initializes jMenu
*
* @return javax.swing.JMenu
*/
private javax.swing.JMenu getEditMenu() {
if (editMenu == null) {
editMenu = new javax.swing.JMenu();
editMenu.setText("Edit");
editMenu.add(getCutMenuItem());
editMenu.add(getCopyMenuItem());
editMenu.add(getPasteMenuItem());
}
return editMenu;
}

/**


* This method initializes jMenu
*
* @return javax.swing.JMenu
*/
private javax.swing.JMenu getHelpMenu() {
if (helpMenu == null) {
helpMenu = new javax.swing.JMenu();
helpMenu.setText("Help");
helpMenu.add(getAboutMenuItem());
}
return helpMenu;
}

/**


* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private javax.swing.JMenuItem getExitMenuItem() {
if (exitMenuItem == null) {
exitMenuItem = new javax.swing.JMenuItem();
exitMenuItem.setText("Exit");
exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(0);
}
});
}
return exitMenuItem;
}

/**


* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private javax.swing.JMenuItem getAboutMenuItem() {
if (aboutMenuItem == null) {
aboutMenuItem = new javax.swing.JMenuItem();
aboutMenuItem.setText("About");
aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
new javax.swing.JDialog(MainFrame.this, "About", true).show();
}
});
}
return aboutMenuItem;
}

/**


* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private javax.swing.JMenuItem getCutMenuItem() {
if (cutMenuItem == null) {
cutMenuItem = new javax.swing.JMenuItem();
cutMenuItem.setText("Cut");
cutMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.Event.CTRL_MASK, true));
}
return cutMenuItem;
}

/**


* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private javax.swing.JMenuItem getCopyMenuItem() {
if (copyMenuItem == null) {
copyMenuItem = new javax.swing.JMenuItem();
copyMenuItem.setText("Copy");
copyMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.Event.CTRL_MASK, true));
}
return copyMenuItem;
}

/**


* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private javax.swing.JMenuItem getPasteMenuItem() {
if (pasteMenuItem == null) {
pasteMenuItem = new javax.swing.JMenuItem();
pasteMenuItem.setText("Paste");
pasteMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.Event.CTRL_MASK, true));
}
return pasteMenuItem;
}

/**


* This method initializes jMenuItem
*
* @return javax.swing.JMenuItem
*/
private javax.swing.JMenuItem getSaveMenuItem() {
if (saveMenuItem == null) {
saveMenuItem = new javax.swing.JMenuItem();
saveMenuItem.setText("Save");
saveMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.Event.CTRL_MASK, true));
}
return saveMenuItem;
}
} // @jve:decl-index=0:visual-constraint="23,15"

ControlPanel.java--------------------------------------

import java.awt.BorderLayout;
import java.awt.Color;

System.err.println("DEBUG: " + jTable.getModel().toString());
System.err.println("DEBUG: " + jTable.getColumnCount());

boolean exceptionWillOccur = true;



if (exceptionWillOccur) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

public String getRandomColorString() {
Color c = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());

return new String(c.toString());
}

private void createAndShowGUI() {
DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();

System.err.println("DEBUG: " + tableModel.toString());
System.err.println("DEBUG: " + tableModel.getColumnCount());



int count = 300;
for (int i = 0; i < count; i++) {
String[] line = new String[] { "" + Math.random(), "" + Math.random(),
getRandomColorString() };

tableModel.addRow(line);
}
}
}


Output: from 3 column, it becomes 0 ????????????????? but the hasCode of the table model is the same... and everything is done in a separate "Runable"... i just don't get it

windowOpened()
DEBUG: javax.swing.table.DefaultTableModel@1a80a69
DEBUG: 3
DEBUG: javax.swing.table.DefaultTableModel@1a80a69
DEBUG: 0

test

unread,
Feb 4, 2005, 10:26:20 AM2/4/05
to
SOLVED !!!

in fact, for some obscur reason (a special bug/feature maybe), it's not enough to add column to the JTable, or to its model, you have to add columns to BOTH !!! NO automatic syncing...

great java...


Anyway, it's solved (lines are drawned, and no exception)

Thx

John McGrath

unread,
Feb 4, 2005, 3:34:16 PM2/4/05
to
On 2/4/2005 at 10:26:20 AM, test wrote:

> in fact, for some obscur reason (a special bug/feature maybe), it's not
> enough to add column to the JTable, or to its model, you have to add
> columns to BOTH !!! NO automatic syncing...

That is not correct. Unless you have told it not to, a JTable will
automatically create the TableColumns from the information provided by the
TableModel. It will also do this when you add a column to the TableModel,
assuming the TableModel fires a "tableStructureChanged" event, as it is
supposed to.

--
Regards,

John McGrath

test

unread,
Feb 7, 2005, 6:31:58 AM2/7/05
to
On Fri, 04 Feb 2005 20:34:16 GMT
"John McGrath" <n...@jpmcgrath.net> wrote:

> > in fact, for some obscur reason (a special bug/feature maybe), it's not
> > enough to add column to the JTable, or to its model, you have to add
> > columns to BOTH !!! NO automatic syncing...
>
> That is not correct. Unless you have told it not to, a JTable will
> automatically create the TableColumns from the information provided by the
> TableModel. It will also do this when you add a column to the TableModel,
> assuming the TableModel fires a "tableStructureChanged" event, as it is
> supposed to.

so where my bug did come from? because it's solved now...

Mr Smith

unread,
Feb 18, 2005, 5:38:15 AM2/18/05
to
I found something:

my GUI designer don't put the JTable attribute "autoCreateColumnsFromModel" to true automaticaly, and the value true doens't seem to be the default for JTables.

Fomr JavaDoc:
Sets this table's autoCreateColumnsFromModel flag. This method calls
createDefaultColumnsFromModel if autoCreateColumnsFromModel changes from false
to true.
Parameters:
autoCreateColumnsFromModel true if JTable should automatically create columns

John McGrath

unread,
Feb 20, 2005, 12:10:13 AM2/20/05
to
On 2/18/2005 at 5:38:15 AM, Mr Smith wrote:

> my GUI designer don't put the JTable attribute
> "autoCreateColumnsFromModel" to true automaticaly, and the value true
> doens't seem to be the default for JTables.

The "autoCreateColumnsFromModel" will be set to true unless you provide a
TableColumnModel in the constructor.

--
Regards,

John McGrath

0 new messages