• Post Reply Bookmark Topic Watch Topic
  • New Topic

URGENT PLz

 
ghazanfar khan
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to add row dynamically into JTable but i also want ot Jbutton but it is throwing error. Please help me out


======================JAVA CODE ===============
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.*;

public class TableTest extends JFrame implements ActionListener
{
JButton btnAdd;
BorderLayout layout;
DefaultTableModel model;
JTable table;

public static void main(String[] args)
{
TableTest app = new TableTest();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public TableTest()
{
super("Table Example");

layout = new BorderLayout();

Container container = getContentPane();
container.setLayout(layout);

btnAdd = new JButton("Add");
btnAdd.addActionListener(this);

model = new DefaultTableModel()
{
public Class getColumnClass(int col)
{
switch (col)
{
case 1 :
return Boolean.class;
default :
return Object.class;
}
}
};

table = new JTable(model);

// Create a couple of columns
model.addColumn("Col1");
model.addColumn("Col2");

JButton but = new JButton("Select Desk");

// Buttons
//table.setValueAt( but, 0, 0 );

// Append a row
model.addRow(new Object[] { "v1", new Button("Submit")});
model.addRow(new Object[] { "v3", new Boolean(false)});

JScrollPane scrollPane = new JScrollPane(table);

container.add(btnAdd, BorderLayout.NORTH);
container.add(scrollPane, BorderLayout.CENTER);

setSize(300, 200);
setVisible(true);

}

public void actionPerformed(ActionEvent e)
{
if (e.getSource() == btnAdd)
{
model.addRow(new Object[] { "Karl", new Boolean(true)});
}

}
}
 
Fred Cunningham
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most of our issues were do to placment of code within improperly matched barckets. It complied but of couse did not run. After that was corrected
there was an issue with the structure of your switch statment.

Below your code reformatted and the corrected switch statment.

I still use the show() method with my swing apps.

You will the warning listed below if you continue to use it:

[deprecation] show() in java.awt.Window has been deprecated
app.show();

I just have not taken the time to find out what replaces it.

Good Luck finshing this app.

Fred Cunningham

//------------------ Reformatted Code -------------------------------------
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.table.*;

public class TableTest extends JFrame implements ActionListener {
JButton btnAdd;
BorderLayout layout;
DefaultTableModel model;
JTable table;

public static void main(String[] args) {
TableTest app = new TableTest();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//new code
app.pack();
app.setSize(500,600);
app.show();

}

public TableTest() {
super("Table Example");

layout = new BorderLayout();

Container container = getContentPane();
container.setLayout(layout);

btnAdd = new JButton("Add");
btnAdd.addActionListener(this);

model = new DefaultTableModel();

table = new JTable(model);

// Create a couple of columns
model.addColumn("Col1");
model.addColumn("Col2");

JButton but = new JButton("Select Desk");

// Buttons
//table.setValueAt( but, 0, 0 );

// Append a row
model.addRow(new Object[] { "v1", new Button("Submit")});
model.addRow(new Object[] { "v3", new Boolean(false)});

JScrollPane scrollPane = new JScrollPane(table);

container.add(btnAdd, BorderLayout.NORTH);
container.add(scrollPane, BorderLayout.CENTER);

//setSize(300, 200);
//setVisible(true);

}

public Class getColumnClass(int col) {
Class returnObject = null;

switch (col) {

case 1 :
returnObject = Boolean.class;
break;
default :
returnObject = Object.class;
}

return returnObject;
}


public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnAdd){
model.addRow(new Object[] { "Karl", new Boolean(true)});
}
}
}
 
Karen Baog
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think show() has been replaced with setVisible()
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!