Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

adding two component in single cell in jtable

Posted on 2002-04-14
3
Medium Priority
?
363 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 150 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

705 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