Solved

adding two component in single cell in jtable

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

615 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