Solved

adding two component in single cell in jtable

Posted on 2002-04-14
3
346 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
[X]
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
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

734 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