• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

URGENT PLz

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)});
}

}
}
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)});
}
}
}
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think show() has been replaced with setVisible()
 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic