Solved

adding two component in single cell in jtable

Posted on 2002-04-14
3
313 Views
Last Modified: 2008-03-06
I need to add a textfield and a button in a single cell of jtable ( something like a textfield with browse button).

Please help me with code snippet to achieve this.

Thanks,
0
Comment
Question by:vibhav
  • 2
3 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6941163
How are you adding one component to a table cell?
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6941319
Add a JPanel containing those two components.
0
 
LVL 9

Accepted Solution

by:
Ovi earned 50 total points
ID: 6941373
Here is the code :

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;

public class Test extends JFrame {
  public Test() {
    init();
  }

  private void init() {
    setSize(600, 400);
    setLocation(250, 200);
    getContentPane().setLayout(new BorderLayout());
    JTable table = new JTable();
    Object[][] data = new Object[5][3];
    for(int i = 0; i<5; i++) {
      for(int j = 0; j<3; j++) {
        data[i][j] = ("Data [" + i + ", " + j + "]");
      }
    }
    Object[] columns = new Object[3];
    columns[0] = "Column 0";
    columns[1] = "Column 1";
    columns[2] = "Column 2";
    table.setModel(new DefaultTableModel(data, columns));
    Class c1 = table.getColumnClass(0);
    Class c2 = table.getColumnClass(1);
    Class c3 = table.getColumnClass(2);
    table.setDefaultRenderer(c1, new X());
    table.setDefaultRenderer(c2, new X());
    table.setDefaultRenderer(c3, new X());
    table.setRowHeight(35);
    table.setRowMargin(3);
    getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
  }

  class X extends JPanel implements TableCellRenderer {
    private JTextField tf = new JTextField(10);
    private JButton b = new JButton("Button");
    public X() {
      super();
      tf.setMargin(new Insets(0, 0, 0, 0));
      b.setMargin(new Insets(0, 0, 0, 0));
      add(tf);
      add(b);
    }

    public Component getTableCellRendererComponent(JTable t,
    Object value, boolean selected, boolean hasFocus, int row, int col) {
      tf.setText(value.toString());
      return(this);
    }
  }

  public static void main(String[] args) {
    (new Test()).setVisible(true);
  }
}


Note : in order to see the desired components on the table, you must load them as default renderers for desired columns. If you want some editable fields coresponding to thowse you must implement and substitute also the cell editor.
Cheers.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
computer science syllabus 3 53
Java asynchronous logging 4 35
Problem to start Neon 20 54
Error with Java/Cache JDBC Classpath 2 21
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now