I can't get ArrayList to work in my JTable
I want to be able to dynamically add and remove data (and rows) from my JTable, and so I wanted to just Lists when I implemented it. I've tried 2 ways and they both didn't work. Can someone show me some sample code to do this?
I tried 2 different methods:
method 1:
JTable Data_Table = new JTable(data_Vector, Collumn_Names_Vector);when I started the applet I got an error saying Applet init error
But the error could also have happened in 2 other places here. 1, I created 2 Lists instead of Vectors and converted them to Vectors when passing them to JTable using:
new Vector(data)
and 2, I may have created my 2 dimensional data List incorrectly. I tried:
List Parent = new ArrayList();
List child1 = new ArrayList();
List child2 = new ArrayList();
child1.add(data1)
child2 ...
Parent.add(child1)
Parent.add(child2)and I tried:
List Parent = new ArrayList();
Object[] temp = { new Integer(); new Integer()};
Parent.add(temp.asList);But as I said method 1 did not work.
method 2:
I tried creating my own TableModel.
In this way, the applet would start, and I can see my collumn names, but the data is blank and all cells are invisible or nonexistent, I don't know which. How do I initialize the Table values? I'd like to have one row with all 0's when the Applet opens? I'd also like to see a row so I can edit it.
then I used this code (inside my model):
public int getColumnCount() {
return Analog_columnNames.length;
public int getRowCount() {
return Analog_data.size();
public String getColumnName(int col) {
return Analog_columnNames[col];
public Object getValueAt(int row, int col) {
java.util.List aRow = (ArrayList)Analog_data.get(row);
return aRow.get(col);
public void setValueAt(Object value, int row, int col) {
if (getValueAt(0,col) instanceof Integer
&& !(value instanceof Integer)) {
try {
java.util.List tmp = (java.util.List) Analog_data.get(row);
tmp.set(col, (Object)new Integer(value.toString()));
fireTableCellUpdated(row, col);
} catch (NumberFormatException e) {
} else {
java.util.List tmp = (java.util.List) Analog_data.get(row);
tmp.set(col, (Object)new Integer(value.toString()));
fireTableCellUpdated(row, col);
}Analog_columnNames is a normal array
this may help You have to remove the button from the columns
otherwise You need som other classes.
jTable1.addextrarowtotable(Name, aantal , prijs);CopyCalcTable.java
import javax.swing.*;
import java.util.Vector;
import javax.swing.table.*;
import java.text.DecimalFormat;
public class CopyCalcTable extends JTable{
protected CopyCalcTableModel model;
CopyCalcTable(){
super();
Vector columns= new Vector();
columns.add("Type");
columns.add("Omschrijving");
columns.add("P/S");
columns.add("Aantal");
columns.add("Prijs");
columns.add("Verwijderen");
model = new CopyCalcTableModel(columns,1);
setModel(model);
public void setColumSize(){
TableColumn column;
int width = getWidth();
column = getColumnModel().getColumn(0);
column.setResizable(false);
column.setMinWidth((width*2)/14);
column.setPreferredWidth((width*2)/14);
column = getColumnModel().getColumn(1);
column.setResizable(false);
column.setMinWidth((width*6)/12);
column.setPreferredWidth((width*2)/14);
column = getColumnModel().getColumn(2);
column.setMinWidth((width*1)/14);
column.setResizable(false);
column.setPreferredWidth((width*1)/14);
column = getColumnModel().getColumn(3);
column.setResizable(false);
column.setMinWidth((width*1)/14);
column.setPreferredWidth((width*1)/14);
column = getColumnModel().getColumn(4);
column.setResizable(false);
column.setMinWidth((width*1)/14);
column.setPreferredWidth((width*1)/14);
column = getColumnModel().getColumn(5);
column.setResizable(false);
column.setMinWidth((width*2)/14);
column.setPreferredWidth((width*2)/14);
public void HideColum(){
TableColumn column;
int width = getWidth();
column = getColumnModel().getColumn(0);
column.setResizable(false);
column.setMinWidth((width*2)/14);
column.setPreferredWidth((width*2)/14);
column = getColumnModel().getColumn(1);
column.setResizable(false);
column.setMinWidth((width*6)/12);
column.setPreferredWidth((width*2)/14);
column = getColumnModel().getColumn(2);
column.setMinWidth((width*1)/14);
column.setResizable(false);
column.setPreferredWidth((width*1)/14);
column = getColumnModel().getColumn(3);
column.setResizable(false);
column.setMinWidth((width*1)/14);
column.setPreferredWidth((width*1)/14);
column = getColumnModel().getColumn(4);
column.setResizable(false);
column.setMinWidth((width*1)/14);
column.setPreferredWidth((width*1)/14);
column = getColumnModel().getColumn(5);
column.setResizable(false);
column.setMinWidth(0);
column.setPreferredWidth(0);
public void removefirstrow(){
model.removeRow(0);
public void removeTotalrow(){
model.removeRow(model.getRowCount()-1);
public void addTotalrow(){
Vector data;
String Col_Bediening = "";
String Col_Uitboek ="";
String Col_Rectoverso="";
String Col_Formaat="";
String Col_Kleur="";
String Col_Gewicht="";
Double Col_PS;
String Col_Aantal;
Double Col_Prijs;
data = new Vector();
Col_Prijs = new Double("0.00");
double totaal= Col_Prijs.doubleValue();
// number of extra's
java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
// set whether you want commas (or locale equivalent) inserted
nf.setGroupingUsed(true );
// set how many places you want to the right of the decimal.
nf.setMinimumFractionDigits(2 );
nf.setMaximumFractionDigits(2 );
// set how many places you want to the left of the decimal.
nf.setMinimumIntegerDigits(1 );
int rows = model.getRowCount();
for (int i = 0;i < rows; i++){
Double d = new Double((String) model.getValueAt(i, 4));
totaal += d.doubleValue();
Col_Prijs = new Double(totaal*1.00);
data.add("Totaal : EUR");
data.add("");
data.add(new String(""));
data.add("");
data.add(nf.format(Col_Prijs));
//data.add(Col_Prijs.toString());
javax.swing.JButton button = new javax.swing.JButton("EUR");
data.add(button);
model.addRow(data);
setColumSize();
public void addkopierowtotable(Integer aantal, double prijs, String omschrijving){
Vector data;
CopyCalcTableModel model = (CopyCalcTableModel) getModel();
Double Col_PS;
String Col_Aantal;
Double Col_Prijs;
//model.addColumn("Verwijderen");
data = new Vector();
Col_PS = new Double(prijs);
Col_Aantal = new String(aantal.toString());
Col_Prijs = new Double(prijs*aantal.intValue());
data.add("Kopie�n");
data.add(omschrijving);
data.add(Col_PS.toString());
data.add(Col_Aantal);
data.add(Col_Prijs.toString());
javax.swing.JButton button = new JButton("Verwijderen");
button.addMouseListener(new MyJButtonMouseListener(this));
data.add(button);
removeTotalrow();
model.addRow(data);
addTotalrow();
public void addextrarowtotable(String Name, Integer aantal, double prijs){
Vector data;
Double Col_PS;
String Col_Aantal;
Double Col_Prijs;
data = new Vector();
Col_PS = new Double(prijs);
Col_Aantal = new String(aantal.toString());
Col_Prijs = new Double(prijs*aantal.intValue());
data.add("Extra");
data.add(Name);
data.add(Col_PS.toString());
data.add(Col_Aantal);
data.add(Col_Prijs.toString());
javax.swing.JButton button = new javax.swing.JButton("Verwijderen");
button.addMouseListener(new MyJButtonMouseListener(this));
data.add(button);
removeTotalrow();
model.addRow(data);
addTotalrow();
public void addspiraalrowtotable(String Name, Integer aantal, double prijs){
Vector data;
Double Col_PS;
String Col_Aantal;
Double Col_Prijs;
//model.addColumn("Verwijderen");
data = new Vector();
Col_PS = new Double(prijs);;
Col_Aantal = new String(aantal.toString());
Col_Prijs = new Double(prijs*aantal.intValue());
data.add("Spiraal");
data.add(Name);
data.add(Col_PS.toString());
data.add(Col_Aantal);
data.add(Col_Prijs.toString());
javax.swing.JButton button = new javax.swing.JButton("Verwijderen");
button.addMouseListener(new MyJButtonMouseListener(this));
data.add(button);
removeTotalrow();
model.addRow(data);
addTotalrow();
public void addklemrugrowtotable(String Name, Integer aantal, double prijs){
Vector data;
Double Col_PS;
String Col_Aantal;
Double Col_Prijs;
//model.addColumn("Verwijderen");
data = new Vector();
Col_PS = new Double(prijs);
Col_Aantal = new String(aantal.toString());
Col_Prijs = new Double(prijs*aantal.intValue());
data.add("Klemrug");
data.add(Name);
data.add(Col_PS.toString());
data.add(Col_Aantal);
data.add(Col_Prijs.toString());
javax.swing.JButton button = new javax.swing.JButton("Verwijderen");
button.addMouseListener(new MyJButtonMouseListener(this));
data.add(button);
removeTotalrow();
model.addRow(data);
addTotalrow();
public void addplastificerenrowtotable(String Name, Integer aantal, double prijs){
Vector data;
Double Col_PS;
String Col_Aantal;
Double Col_Prijs;
//model.addColumn("Verwijderen");
data = new Vector();
Col_PS = new Double(prijs);
Col_Aantal = new String(aantal.toString());
Col_Prijs = new Double(prijs*aantal.intValue());
data.add("Plastcificeren");
data.add(Name);
data.add(Col_PS.toString());
data.add(Col_Aantal);
data.add(Col_Prijs.toString());
javax.swing.JButton button = new javax.swing.JButton("Verwijderen");
button.addMouseListener(new MyJButtonMouseListener(this));
data.add(button);
removeTotalrow();
model.addRow(data);
addTotalrow();
public int getRowCount() {
if ( model == null ){
return 0;
}else {
return model.getRowCount();
class CopyCalcTableModel extends AbstractTableModel {
Vector data;
Vector columnNames;
CopyCalcTableModel(Vector head_vec, int rows) {
columnNames = (Vector) head_vec.clone();
// coldata = new Vector(columnNames.size());
data = new Vector(rows);
CopyCalcTableModel(Vector head_vec) {
columnNames = (Vector) head_vec.clone();
// coldata = new Vector(columnNames.size());
data = new Vector();
CopyCalcTableModel(Vector col_vec, Vector head_vec) {
Vector coldata;
columnNames = (Vector) head_vec.clone();
data = (Vector) col_vec.clone();
public int getColumnCount() {
// System.out.println(columnNames);
return columnNames.size();
public int getRowCount() {
// System.out.println(data);
return data.size();
public void removeRow(int row) {
// System.out.println(data);
data.removeElementAt(row);
fireTableStructureChanged();
public String getColumnName(int col) {
return (String) columnNames.elementAt(col);
public Object getValueAt(int row, int col) {
Vector v_row;
Vector v_col;
v_row = (Vector) data.elementAt(row);
return v_row.elementAt(col);
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
public Class getColumnClass(int c) {
// return getValueAt(0, c).getClass();
int rowIndex = 0;
Object o = getValueAt(rowIndex, c);
if (o == null) {
return Object.class;
} else {
// System.out.println(o.getClass());
return o.getClass();
* Don't need to implement this method unless your table's
* editable.
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
/*Vector v_row;
v_row = (Vector) data.elementAt(row);
if (col == 10 ) {
return true;
} else {
return false;
return false;
* add collumn
public void addColumn(String label, Object value){
columnNames.add(label);
for ( int i=0;data.size()>i;i++){
Vector row = (Vector) data.elementAt(i);
row.add(value);
data.setElementAt(row, i);
fireTableStructureChanged();
public void addRow(Vector values){
data.add(values);
fireTableStructureChanged();
* Don't need to implement this method unless your table's
* data can change.
public void setValueAt(Object value, int lrow, int lcol) {
}
Similar Messages
-
Have entrourage and want to use Mail. Can't get it to work.
I had a macbook and purchased the Microsoft office for Mac which included Entrourage.
I have now purchased a new macbook Pro and want to begin to use Mail rather than Entrourage.
I have Entrourage on my new computer but i can't get it to transfer my information to the the Mail program
I am on Charter.net at my office and sbcglobal.net at home.
How can i get this to work. I can receive on Mail, but i can't send.check your outgoing port settings and make sure it's using a specific port and not the default port, also you might want to check to see if it is using SSL, TLS or any other encryption. Verify the setting from your old machine setup.
-
got my password wrong twice and my mini is disable. I connected to iTunes, but can't get it to work. Can some one guide my through?
If it's showing the red disabled screen due to incorrect passcodes then you may need to put the iPad into recovery mode : http://support.apple.com/kb/ht1808 - you should then be able to reset the iPad via your computer's iTunes and restore/resync your content to it
-
HT1151 i have a new apple external dvd drive, and i can't get it to work with my iMac?
I have a new apple dvd drive, I was told it will work with my imac, but when i plug it in ...nothing... iv been for update... still nothing. How can i get it to work???
If it is this drive, note that it is for MBA's and Mini's only:
http://store.apple.com/us/product/MC684ZM/A
Ciao. -
Is there a way to reset my messages account, I can't get iMessage to work on IPad nor Mac osx. Help please this is really frustrating.
Hi,
Is there an iPhone involved ?
If not, what happens if you remove the Apple ID on the iPad and then set it in Airplane Mode (allow a couple of minutes at this point) followed by re-adding the Apple ID ?
What, if any error messages are appearing on the Mac when you try to launch the App or Login to the iMessages server ?
Is the Apple ID linked to an iCloud account ?
In Messages 7 (Mountain Lion) this did not seem to be important but it seems it is in Mavericks/Messages 8
9:14 pm Friday; April 11, 2014
iMac 2.5Ghz i5 2011 (Mavericks 10.9)
G4/1GhzDual MDD (Leopard 10.5.8)
MacBookPro 2Gb (Snow Leopard 10.6.8)
Mac OS X (10.6.8),
Couple of iPhones and an iPad -
I bought an IR receiver and an Apple Remote, and I can't get it to work. What am I doing wrong? The receiver is a Lenovo eHome OVU430006/01 USB IR Receiver.
Hello,
You would need to pair the Apple Remote with the receiver. So I would use the generic Mac/PC receiver and follow these instructions:
http://support.apple.com/kb/HT1619
If that doesn't work then check in System Profiler to see if the IR receiver has been recognized by the PowerBook's hardware. You may require the install of drivers for this device to be recognized.
Best of luck -
I have a go daddy email account and I can't get it to work on my iPad
I have a go daddy email account and I can't get it to work on my iPad
Try turning the email account off and then turn it on again. Settings>Mail, Contacts, Calendars>select your gmail account>Off. Go to the mail app and then come back to the account in the settings and turn it on again.
If you can't find any other way for the iPad to accept the password, delete the email account and then add it back onto the iPad. -
I have a Mac Mini and I can't get it to work with my Ricoh Aficio MP 161. Any ideas?
I have a Mac Mini and I can't get it to work with my Ricoh Aficio MP 161. Any ideas?
The latest Apple driver support begins with the MP 171.
Here is Ricoh's official driver download page specifically for the Afico MP 161. Note that new drivers for this device were released on Aug 2, and include Lion and Mountain Lion.
Hopefully, this will solve your device issues. -
Iphone 5 went black. Tried pushing on/off and circle at the same time. Worked yesterday and then did it again today. Can't get it to work.
You need to hold those button longer until you see the apple logo, then release, then wait for the phone to boot back up.
-
I just bought a new computer and had files transferred along with itunes to the new computer. I can't get it to work. It doesn't recognize the extensions for itunes. I also tried downloading Quicktime and couldn't get it to download.
iOS: Device not recognized in iTunes for Windows
I would start with:
Removing and reinstalling iTunes, QuickTime, and other software components for Windows Vista or Windows 7 -
I bought a DATAPILOT to transfer pictures to my macbook and can't get it to work. Any suggestions please?
Best if you try and explain what you are trying to acheive.
Iphoto is a different paradigm than you're used to. I don't understand why you are trying to transfer your edited photos back to an external disk.
There are tutorials here
http://www.apple.com/findouthow/photos/
Regards
TD -
I have signed up for adobe conversion to microsoft word and i can't get it to work - can you help me
I have signed up for adobe conversion to microsoft word and I can not get it to work - can you help me?
Hi Mike,
I've checked your account. I see that you just purchased the subscription this morning. The order is still pending processing, which is why you haven't yet been able to log in an use your subscription. It can take 24-48 hours for a subscription to process fully. Once it does, you'll be able to log in and convert files.
I apologize for the inconvenience.
Best,
Sara -
Trying to connect 2010 Mac mini to alba hd tv but can't get it to work, any theories?
Trying to connect 2010 Mac mini to alba hd tv but can't get it to work, any theories?
Welcome to the Apple Community.
The iPad 1 is only able to do basic AirPlay, many providers of programming have blocked their apps from working with AirPlay. -
I just bought Quick time Pro and I can't get it to install. It shows a yellow box in my application folder, but I can't get it to work. Help!
you launch QT then go to its REGISTRATION
-
Downloaded the App Sales Call Manager. I can't get it to work on either my Iphone 4s or my Ipad2? Can anybody advise how to fix or even get back my money that i paid for it?
Contact the app developer for support. A link to their website will be in the apps description in the AppStore.
Maybe you are looking for
-
I have a mpg that I took on my digital camera. It plays fine in QT but the editing controls are not showing up as I roll over the timeline. Also when i import into iMovie, I get the video but there is no audio. This is a movie of my 4 month old which
-
I cant delet all the itunes files o my cp to download the
I cant delet all the bonjur files to download the new. Itunes what do i do
-
Hi, Can somebody forward me the steps related to Validation & substitution, its urgent. In T. code GGB0 ==> Line Item > Steps> check it is defined as : NOT BSEG-HKONT IN ZBANK What is this ZBANK ... is it the variable. How is my BANK GL Account relat
-
How do you find what objects you modified?
I am leaving my current company soon and they want me to document all objects that I touched. Is there a simple way to do this? I am thinking of a program that I can run (create) by searching z-objects (programs, structures, tables, forms, etc...)
-
Why do I have 13 versions of httpd running?
I realize that I am running a MAMP server, but for the life of me I can't figure out why 13 instances of httpd are running in Activity Manager!. All but one are attributed to my user, and one of them is tied to root. They seem to take no CPU, but I