Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

adding two component in single cell in jtable

Posted on 2002-04-14
3
332 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 82
spring jars download 1 35
How to convert from xls to xlsx using java 7 54
hashmap order 17 36
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

791 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