Adding JTree to JTable cell is not expanding??
Hi experts
My problem is that as Tree expands, my table cell should expands accordingly.
How do I do that??I do n't mind if other cells get expands.
Please help me...?
Thanks in advance
How do I do that no such method is available.can you guide me or give me some code.I need urgently..
Thank you very much
Similar Messages
-
Adding JButton into JTable cells
Hi there!!
I want to add a JButton into JTable cells.In fact I have got two classes.Class2 has been extended from the AbstractTableModel class and Class1 which is using Class2's model,,,here's the code,,
Class1
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Class1 extends javax.swing.JFrame {
//////GUI specifications
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TestTableButton().setVisible(true);
Class2 model=new Class2();
jTable1=new JTable(model);
jScrollPane1.setViewportView(jTable1);
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration
private javax.swing.JTable jTable1;
}Class2
import javax.swing.table.*;
public class Class2 extends AbstractTableModel{
private String[] columnNames = {"A","B","C"};
private Object[][] data = {
public int getColumnCount() {
return columnNames.length;
public int getRowCount() {
return data.length;
public String getColumnName(int col) {
return columnNames[col];
public Object getValueAt(int row, int col) {
return data[row][col];
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
* Don't need to implement this method unless your table's
* editable.
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
return false;
* Don't need to implement this method unless your table's
* data can change.
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}what modifications shud I be making to the Class2 calss to add buttons to it?
Can anybody help plz,,,,,??
Thanks in advance..Hi rebol!
I did search out a lot for this but I found my problem was a bit different,,in fact I want to use another class's model into a class being extended by JFrame,,so was a bit confused,,,hope you can give me some ideas about how to handle that scenario,,I know this topic has been discussed before here many a times and also have visited this link,,
http://forum.java.sun.com/thread.jspa?threadID=465286&messageID=2147913
but am not able to map it to my need,,,hope you can help me a bit...
Thanks ..... -
Code to change JTable cell color not working
My code below is supposed to change the color
of a single JTable cell however its not working.
Could anyone please tell me why?
Here's the code:
import java.awt.*;
import javax.swing.table.*;
import javax.swing.*;
import java.awt.event.*;
public class ColorTable extends JTable {
public ColorTable() {
AbstractTableModel tableModel = new AbstractTableModel() {
public Class getColumnClass(int column) { return Integer.class; }
public int getColumnCount() { return 6; }
public int getRowCount() { return 10;}
public Object getValueAt(int row,int col) { return new Integer(row * col); }
setModel(tableModel);
setDefaultRenderer(Integer.class,new ColorRenderer(Color.cyan));
this.setRowSelectionAllowed(false);
this.setCellSelectionEnabled(true);
addMouseListener(new MouseAdapter() {
private ColorRenderer renderer;
private JColorChooser chooser = new JColorChooser();
public void mousePressed(MouseEvent e) {
if(e.getModifiers() == MouseEvent.BUTTON3_MASK) {
System.out.print("rowAtPoint(e.getPoint())=" +rowAtPoint(e.getPoint()));
System.out.print( "columnAtPoint(e.getPoint()))=" + columnAtPoint(e.getPoint()));
renderer = (ColorRenderer)getCellRenderer(rowAtPoint(e.getPoint()), columnAtPoint(e.getPoint()));
// chooser.setColor(renderer.getColor());
renderer.setColor(chooser.showDialog((Component)e.getSource(),"Choose Cell Color",chooser.getColor()));
class ColorRenderer extends DefaultTableCellRenderer {
private Color cellColor;
public ColorRenderer(Color color) { cellColor = color; }
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
System.out.print("row= " + row + "\n");
System.out.print("column= " + column + "\n");
System.out.print("OBJECT VALUE=" + value.toString());
//Component comp = super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
if (isSelected) {
setOpaque(true);
setBackground(cellColor);
else {
setBackground(Color.white);
setForeground(Color.black);
return this;
public void setColor(Color color)
cellColor = color;
ColorTable.this.repaint();
public Color getColor() { return cellColor; }
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
ColorTable table = new ColorTable();
panel.add(table);
frame.getContentPane().add(panel);
frame.setSize(500,250);
frame.setVisible(true);My code below is supposed to change the color
of a single JTable cell however its not working.
Could anyone please tell me why?
Here's the code:
import java.awt.*;
import javax.swing.table.*;
import javax.swing.*;
import java.awt.event.*;
public class ColorTable extends JTable {
public ColorTable() {
AbstractTableModel tableModel = new AbstractTableModel() {
public Class getColumnClass(int column) { return Integer.class; }
public int getColumnCount() { return 6; }
public int getRowCount() { return 10;}
public Object getValueAt(int row,int col) { return new Integer(row * col); }
setModel(tableModel);
setDefaultRenderer(Integer.class,new ColorRenderer(Color.cyan));
this.setRowSelectionAllowed(false);
this.setCellSelectionEnabled(true);
addMouseListener(new MouseAdapter() {
private ColorRenderer renderer;
private JColorChooser chooser = new JColorChooser();
public void mousePressed(MouseEvent e) {
if(e.getModifiers() == MouseEvent.BUTTON3_MASK) {
System.out.print("rowAtPoint(e.getPoint())=" +rowAtPoint(e.getPoint()));
System.out.print( "columnAtPoint(e.getPoint()))=" + columnAtPoint(e.getPoint()));
renderer = (ColorRenderer)getCellRenderer(rowAtPoint(e.getPoint()), columnAtPoint(e.getPoint()));
// chooser.setColor(renderer.getColor());
renderer.setColor(chooser.showDialog((Component)e.getSource(),"Choose Cell Color",chooser.getColor()));
class ColorRenderer extends DefaultTableCellRenderer {
private Color cellColor;
public ColorRenderer(Color color) { cellColor = color; }
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
System.out.print("row= " + row + "\n");
System.out.print("column= " + column + "\n");
System.out.print("OBJECT VALUE=" + value.toString());
//Component comp = super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
if (isSelected) {
setOpaque(true);
setBackground(cellColor);
else {
setBackground(Color.white);
setForeground(Color.black);
return this;
public void setColor(Color color)
cellColor = color;
ColorTable.this.repaint();
public Color getColor() { return cellColor; }
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
ColorTable table = new ColorTable();
panel.add(table);
frame.getContentPane().add(panel);
frame.setSize(500,250);
frame.setVisible(true); -
JTable cell editor not working
Hello there,
I am new to Java programming,
I am trying to create a check box in a table cell which the user should able manipulate when the JTable is showing, the renderer is working fine but the editor is not , the overriden method getTableCellEditorComponent never gets a call, getTableCellRendererComponent does get a call and renderer works fine.
any ideas whats wrong here ?? or what could be wrong which makes getTableCellEditorComponent not get called ?
my renderere and editor code is like this
package com.itrsgroup.swing.componentmanager.dockablemanager;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.EventObject;
import javax.swing.AbstractCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
* A cell renderer and a cell editor for rendering and editing the filters active state.
public class ActiveCheckBoxRendererEditor extends AbstractCellEditor
implements TableCellEditor, TableCellRenderer, ItemListener
private Font font = new Font("Verdana", Font.BOLD, 11 );
private JCheckBox editor = new JCheckBox("Inactive");
private JCheckBox renderer = new JCheckBox("Inactive");
private String strSelectedText;
private String strNotSelectedText;
private Color colourActive = new Color(51,153,51);
private Color colourInActive = Color.RED;
/** Constructor. */
public ActiveCheckBoxRendererEditor()
this( "Selected", "Not Selected" );
* Constructor.
* @param strSelText - the text to render when selected.
* @param strNotSelText - the text to render when not selected.
public ActiveCheckBoxRendererEditor(String strSelText, String strNotSelText)
strSelectedText = strSelText;
strNotSelectedText = strNotSelText;
renderer = new JCheckBox( strNotSelectedText );
renderer.setHorizontalTextPosition( SwingConstants.CENTER );
renderer.setVerticalTextPosition( SwingConstants.TOP);
renderer.setHorizontalAlignment( SwingConstants.CENTER );
renderer.setVerticalAlignment( SwingConstants.CENTER );
renderer.setFont( font );
editor = new JCheckBox( strNotSelectedText );
editor.setBackground( UIManager.getColor("Table.selectionBackground") );
editor.setHorizontalTextPosition( SwingConstants.CENTER );
editor.setVerticalTextPosition( SwingConstants.TOP );
editor.setHorizontalAlignment( SwingConstants.CENTER );
editor.setVerticalAlignment( SwingConstants.CENTER );
editor.setFont( font );
editor.addItemListener( this );
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
return editor;
@Override
public boolean isCellEditable(EventObject arg0)
// TODO Auto-generated method stub
return true;
@Override
public Object getCellEditorValue()
return editor.isSelected();
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
Boolean bool = (Boolean) value;
renderer.setSelected( bool );
renderer.setText( bool ? strSelectedText : strNotSelectedText);
renderer.setForeground( bool ? colourActive : colourInActive );
if( isSelected )
renderer.setBackground( table.getSelectionBackground() );
else
renderer.setBackground( table.getBackground() );
return renderer;
@Override
public void itemStateChanged(ItemEvent e)
boolean isSelected = e.getStateChange() == ItemEvent.SELECTED;
editor.setText( isSelected ? strSelectedText : strNotSelectedText);
stopCellEditing();
and this is how I install it on my JTable
ActiveCheckBoxRendererEditor cbc = new ActiveCheckBoxRendererEditor("Active", "Inactive");
TableColumn
activeColumn = this.getColumnModel().getColumn( 3 );
activeColumn.setCellEditor( cbc);
activeColumn.setCellRenderer( cbc );
activeColumn.setPreferredWidth( 80 );
activeColumn.setMaxWidth( 200 );
activeColumn.setMinWidth( 80 );thanks
AhmedIs the column editable?
If you need further help then you need to create a [Short, Self Contained, Compilable and Executable, Example Program (SSCCE)|http://homepage1.nifty.com/algafield/sscce.html], that demonstrates the incorrect behaviour. -
Cell borders not expanding using nested subforms
I have an existing form whose table was created using subforms (not a table object). I found this post which addresses my issue: Cell Borders in tables of adobe form. However, I'm still having an issue and I'm hoping someone can point me in the right direction.
The subform 'ITEM' contains 12 columns. I wrapped column 12 into a subform and named it Subform12. I then wrapped column 11 and Subform12 into a subform and called it Subform11. I continued to do this for the remaining 10 columns. I removed the borders for each individual column and instead set the border for each SubformXX. I also selected 'Auto-Fit' for the height for each SubformXX. The sixth column is the column that can contain multiple lines of text. After doing this the first 6 columns expand correctly, however, the last 6 do not.
Can someone please tell me what I'm missing?
Thanks!
JulieHi Julie,
Can't you just do the following:
For the first cell (the one with the large amount of text), you but on the right border
You take this cell together with the next one and put it into a flowed subform with the right border
Then you take this subform together with the next one:
Repeat number 3:
Kind regards,
Niels De Greef -
Problem in adding image in jtable cells
hi all.
im creating a swing application using netbeans 6.0.
i want to display image in cells of one column.
for that i set an "image icon" directly to that cell in the "default table model", in that case it is displaying the path of that image in that cell!
then i created a new label, and set the image as icon of that label. added that label in that cell. in this case it is printing label.tostring() to that cell!!
please help.
thanks in advance.You need to override getColumnClass to return Icon.class for the column containing the icon. A code snippet that demonstrates how you might do this: table = new JTable (new DefaultTableModel (data, colNames) {
public Class getColumnClass (int columnIndex) {
switch (columnIndex) {
case 0: return Calendar.class;
case 1: return String.class;
case 2: return Icon.class;
//case 2: return Boolean.class;
return null;
});luck, db -
My JButton inside a JTable cell does not respond to clicks
I have a Jtable which extends AbstractTableModel and uses an arraylist to fill the data in the model. The first column of this table is my button column. I have a class for my button coumn which is:
class MyButtonCol extends AbstractCellEditor
implements TableCellRenderer, TableCellEditor, ActionListenerMy button column class has getTableCellRendererComponent and getTableCellEditorComponent. My problem is that the button shows on the table but it does not respond to clicks. Any help will be appreciated.That does not seem to be the problem. I have the method in my class but it still does not respond to clicks. This is my AbstractTableModel class:
class WorklisttableModel extends AbstractTableModel{
protected static List<Worklist> transaction;
protected String[] columnNames = new String[]{" ", "Modality", "Status","Patient name",
"Patient ID","Date of birth","Study date","Referring physician","Description"};
public WorklisttableModel(){
transaction = new ArrayList<Worklist>();
fillmodel();
@Override
public boolean isCellEditable(int row, int column) {
return false;
@Override
public int getColumnCount() {
return 9;
@Override
public int getRowCount() {
return (transaction!=null) ? transaction.size() : 0;
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
if(rowIndex < 0 || rowIndex>=getRowCount())
return" ";
Worklist row = (Worklist)transaction.get(rowIndex);
switch(columnIndex){
//case 0:return "";
case 1:return " "+row.getModality();
case 2: return row.getStatus();
case 3:return row.getName();
case 4:return row.getID();
case 5:return row.getDOB();
case 6:return row.getStudyDate();
case 7:return row.getReferringP();
case 8:return row.getDescription();
return " ";
public Class getColumnClass(int col){
return getValueAt(0,col).getClass();
@Override
public String getColumnName(int columnIndex) {
return columnNames[ columnIndex ];
protected void fillmodel(){
transaction.add(new Worklist("","US","active","Simpson","1232222",new java.util.Date(73,8,12),new Date(18,8,13),"Dr. Francis","Brain"));
transaction.add(new Worklist("","US","inactive","Dodggy","3498222",new java.util.Date(83,8,12),new Date(16,8,17),"Dr. Francis","Heart"));
transaction.add(new Worklist("","CT","active","Williams","7892222",new java.util.Date(98,9,5),new Date(19,2,13),"Dr. Evans","Dental"));
transaction.add(new Worklist("","MR","inactive","Brian","89765412",new java.util.Date(65,5,23),new Date(19,1,18),"Dr. Evans","Brain"));
Collections.sort( transaction, new Comparator<Worklist>(){
public int compare( Worklist a, Worklist b) {
return a.getName().compareTo( b.getName() );
} -
Adding checkbox to JTable cell
Hi, I am tryin to add a checkbox to JTabel cell.
But somehow i am ending up getting 'false' in the cell instead of checkbox.
Can anyone please help me.
here is my getColumnClass and getValuAt
public Class getColumnClass(int column)
{ if(column==0) return Boolean.class;
else return Object.class;
public Object getValueAt(int nRow, int nCol) {
if(nCol==0)
return new Boolean(ch.isSelected());
}You can use a cell rederer that will give you more flexibility .
Add the renderer to a specific column and that will do the job.
Here is the example:
public class CheckBoxRenderer extends JCheckBox implements TableCellRenderer {
public CheckBoxRenderer() {
super();
public Component getTableCellRendererComponent(JTable table, java.lang.Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
Color c = table.getBackground();
this.setBackground(c);
setSelected(value != null && ((Boolean) value).booleanValue());
return this;
}In order to edit the value in a check box you will need to write a cell editor which
is going to be easy. -
Adding JPopupMenu to JTable cell
Hi,
I am just wondering if I can add a JPopup menu to each cell in a JTable, so that a user can perform some tasks.For example , a user should be able to delete a table cell by choosing the delete menu item in the popup menu
many thanks
RameshHi,
Add JPopupmenu and add JMenuItems on that according to your requirement. Add MouseListener to your table and on mouseReleased event check whether you have clicked the right mouse. If right mouse is clicked, then show the popup menu that you have already created. I think the following code will help you.
JPopupMenu lJPopupMenu = new JPopupMenu();
JMenuItem lJMenuItem1 = new lJMenuItem();
lJPopupMenu.add(lJMenuItem1);
lJTable.addMouseListener(this);
MouseReleased(java.awt.event.MouseEvent mouseEvent) {
if(mouseEvent.isPopupTrigger()){
lJPopupMenu.show(lJTable, mouseEvent.getX(), mouseEvent.getY());
ActionPerformed of the JMenuItem, you write the code based on the requirement. -
Can not show the JCheckBox in JTable cell
I want to place a JCheckBox in one JTable cell, i do as below:
i want the column "d" be a check box which indicates "true" or "false".
String[] columnNames = {"a","b","c","d"};
Object[][] rowData = {{"", "", "", Boolean.FALSE}};
tableModel = new DefaultTableModel(rowData, columnNames);
dataTable = new JTable(tableModel);
dataTable.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(new JCheckBox()));
But when i run it, the "d" column show the string "false" or "true", not the check box i wanted.
I do not understand it, can you help me?
Thank you very much!
coral9527Do not use DefaultTableModel, create your own table model and you should implement the method
getColumnClass to display the boolean as checkbox ...
I hope the following colde snippet helps you :
class MyModel extends AbstractTableModel {
private String[] columnNames = {"c1",
"c2"};
public Object[][] data ={{Boolean.valueOf(true),"c1d1"}};
public int getColumnCount() {
//System.out.println("Calling getColumnCount");
return columnNames.length;
public int getRowCount() {
//System.out.println("Calling row count");
return data.length;
public String getColumnName(int col) {
return columnNames[col];
public Object getValueAt(int row, int col) {
return data[row][col];
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
* Don't need to implement this method unless your table's
* editable.
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
return true;
* Don't need to implement this method unless your table's
* data can change.
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col); -
Setting tooltip when the content of the cell is not visible in jtable
hello,
i want to set a tooltip when the content of the cell is not visible in jtable
please guide me.
thanks
dayathanks for your replay,
i did in this way
final TableColumn colTableColumn = getColumnModel().getColumn(vColIndex);
if (colTableColumn.getWidth() < this.getPreferredSize().width)
setToolTipText(value.toString());
else
setToolTipText(null);
}thanks
daya -
How to write an element in a JTable Cell
Probably it's a stupid question but I have this problem:
I have a the necessity to build a JTable in which, when I edit a cell and I push a keyboard button, a new Frame opens to edit the content of the cell.
But the problem is how to write something in the JTable cell, before setting its model. Because, I know, setCellAT() method of JTree inserts the value in the model and not in the table view. And repainting doesn't function!
What to do??
ThanksHi there
Depending on your table model you should normally change the "cell value" of the tablemodel.
This could look like:
JTable table = new JTable();
TableModel model = table.getModel();
int rowIndex = 0, columnIndex = 0;
model.setValueAt("This is a test", rowIndex, columnIndex);
The tablemodel should then fire an event to the view (i.e. JTable) and the table should be updated.
Hope this helps you -
How to select rows in the inner JTable rendered in an outer JTable cell
I have wrriten the following code for creating cell specific renderer - JTable rendered in a cell of a JTable.
table=new JTable(data,columnNames)
public TableCellRenderer getCellRenderer(int row, int column)
if ((row == 0) && (column == 0))
return new ColorRenderer();
else if((row == 1) && (column == 0))
return new ColorRenderer1();
else
return super.getCellRenderer(row, column);
ColorRenderer and ColorRenderer1 are two inner classes, which implement TableCellRenderer to draw inner JTable on the outer JTable cell, having 2 rows and 1 column each.
Now what is happening the above code keeps executing continously, that is the classes are being initialised continously, inner JTables are rendered (drawn) continously, and this makes the application slow after some time. It throws java.lang.OutOfMemoryException.
WHY IS IT SO??? I can't understand where's the bug..
Any advice please???
Moreover i want selections in inner tables and not on outer table, how can this be possible.
I am working on this since a long time but have not yet found a way out...With your help i have overcome the problem of continous repeatition.
The major problem which I am facing is, in selecting rows in the inner rendered JTables.
I have added listener on outer JTable which select rows on the outer JTable, hence the complete inner JTable which being treated as a row, gets selected.
The thing is i need to select the rows of inner rendered JTables,not the outer JTable.
How to go about it??
I have even added listener to inner rendered JTables, but only first row of every table gets selected.
Please help....
Thanks in advance. -
Hi
I am facing one problem. there is some data should be displayed in Jtable cell.
The thing is that the whole data shall be visible in the cell.. for this I am writing one renderer.. but I could not find the desire solution.. please check it out
class Item_Details extends JFrame {
ApsJTable itemTable = null;
ApsJTable imageTable = null;
ArrayList data = new ArrayList();
String[] columns = new String[2];
ArrayList data1 = new ArrayList();
String[] columns1 = new String[2];
ItemTableModel itemTableModel = null;
ItemTableModel itemTableModel1 = null;
public Item_Details()
super("Item Details");
this.setSize(600,100);
this.setVisible(true);
init();
private void init(){
////////////// Get data for first Table Model ////////////////////////////
data = getRowData();
columns = getColData();
System.out.println(columns[0]);
itemTableModel = new ItemTableModel(data,columns);
/////////////Get Data for Second Table Model //////////////////////////////
try{
data1 = getRowData1();
}catch(Exception e){}
columns1 = getColumns1();
itemTableModel1 = new ItemTableModel(data1,columns1);
///////////// Set Data In Both Table Model //////////////////////////////////
itemTable = new ApsJTable(itemTableModel);
imageTable = new ApsJTable(itemTableModel1);
this.itemTable.setShowGrid(false);
this.imageTable.setShowGrid(false);
this.itemTable.setColumnSelectionAllowed(false);
this.imageTable.setColumnSelectionAllowed(false);
System.out.println(itemTable.getColumnCount());
this.imageTable.setRowHeight(getImageHeight()+3);
JScrollPane tableScrollPane = new JScrollPane(this.imageTable,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
tableScrollPane.setRowHeaderView(this.itemTable);
tableScrollPane.getRowHeader().setPreferredSize(new Dimension(800, 0));
itemTable.getTableHeader().setReorderingAllowed(false);
itemTable.setColumnSelectionAllowed(false);
//itemTable.setRowHeight(25);
itemTable.setCellSelectionEnabled(false);
itemTable.setFocusable(false);
imageTable.getTableHeader().setReorderingAllowed(false);
imageTable.setFocusable(false);
imageTable.setCellSelectionEnabled(false);
//tableScrollPane.setOpaque(false);
itemTable.setAutoCreateColumnsFromModel(false);
int columnCount = itemTable.getColumnCount();
for(int k=0;k<columnCount;k++)
/* TableCellRenderer renderer = null;
TableCellEditor editor = null;
renderer = new TextAreaCellRenderer(); // NEW
editor = new TextAreaCellEditor();
TableColumn column = new TableColumn(k,itemTable.getColumnModel().getColumn(k).getWidth(),renderer, editor);
itemTable.addColumn(column);*/
itemTable.getColumnModel().getColumn(k).setCellRenderer(new MultiLineCellRenderer());
//itemTable.getColumnModel().getColumn(k).setCellEditor(new TextAreaCellEditor());
////////////---------------------- Here background color is being set--------------//////////////////
this.imageTable.getParent().setBackground(Color.WHITE);
this.itemTable.getParent().setBackground(Color.WHITE);
tableScrollPane.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER,this.itemTable.getTableHeader());
getContentPane().add(tableScrollPane,BorderLayout.CENTER);
getContentPane().setVisible(true);
public static void main(String[] str){
com.incors.plaf.alloy.AlloyLookAndFeel.setProperty("alloy.licenseCode", "2005/05/28#[email protected]#1v2pej6#1986ew");
try {
javax.swing.LookAndFeel alloyLnF = new com.incors.plaf.alloy.AlloyLookAndFeel();
javax.swing.UIManager.setLookAndFeel(alloyLnF);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
Item_Details ID = new Item_Details();
ID.setVisible(true);
public ArrayList getRowData()
ArrayList rowData=new ArrayList();
Hashtable item = new Hashtable();
item.put(new Long(0),new String("Item No:"));
item.put(new Long(1),new String("RED-1050"));
rowData.add(0,item);
item = new Hashtable();
item.put(new Long(0),new String("Description:"));
item.put(new Long(1),new String("SYSTEM 18 mbh COOLING 13 mbh HEATING 230/208 v POWER AIRE "));
rowData.add(1,item);
item = new Hashtable();
item.put(new Long(0),new String("Stage:"));
item.put(new Long(1),new String("Draft"));
rowData.add(2,item);
item = new Hashtable();
item.put(new Long(0),new String("Price: "));
item.put(new Long(1),new String(" 999.00"));
rowData.add(3,item);
item = new Hashtable();
item.put(new Long(0),new String("Features:"));
item.put(new Long(1),new String("SYSTEM COOLING & HEATING 12 mbh 230/208 v POWER AIRE SYSTEM1234 COOLING & HEATING 12 mbh 230/208 v POWER AIRE "));
rowData.add(4,item);
/*item.put(new Long(0),new String("Family Sequence"));
item.put(new Long(1),new String("8.00"));
rowData.add(5,item);
item.put(new Long(0),new String("Family Sequence"));
item.put(new Long(1),new String("8.00"));
rowData.add(6,item);
item.put(new Long(0),new String("Family Sequence"));
item.put(new Long(1),new String("8.00"));
rowData.add(7,item);
return rowData;
public String[] getColData()
String[] colData = new String[]{"Attribute","Value"};
return colData;
public ArrayList getRowData1()throws MalformedURLException{
ArrayList rowData = new ArrayList();
Hashtable item = new Hashtable();
String str = new String("http://biis:8080/assets/PRIMPRIM/Adj_BeacM_Small.jpg");
URL url = new URL(str);
ImageIcon ic = new ImageIcon(url);
ImageIcon scaledImage = new ImageIcon(ic.getImage().getScaledInstance(getImageHeight(), -1,Image.SCALE_SMOOTH));
item.put(new Long(0), scaledImage);
rowData.add(0,item);
String str1 = new String("http://biis:8080/assets/PRIMPRIM/Adj_BeacM_Small.jpg");
URL url1 = new URL(str1);
ImageIcon ic1 = new ImageIcon(url1);
ImageIcon scaledImage1 = new ImageIcon(ic1.getImage().getScaledInstance(120, -1,Image.SCALE_DEFAULT));
item.put(new Long(0),scaledImage1);
rowData.add(1,item);
return rowData;
public String[] getColumns1(){
String[] colData = new String[]{"Image"};
return colData;
public int getImageHeight(){
ImageIcon ic = new ImageIcon("c:\\image\\ImageNotFound.gif");
return ic.getIconHeight();
class MultiLineCellRenderer extends JTextArea implements TableCellRenderer {
public MultiLineCellRenderer() {
setLineWrap(true);
setWrapStyleWord(true);
JScrollPane m_scroll = new JScrollPane(this,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
setOpaque(true);
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
/*if (isSelected) {
setForeground(table.getSelectionForeground());
setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
// setFont(table.getFont());
/* if (hasFocus) {
setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
if (table.isCellEditable(row, column)) {
setForeground( UIManager.getColor("Table.focusCellForeground") );
setBackground( UIManager.getColor("Table.focusCellBackground") );
} else {
setBorder(new EmptyBorder(1, 2, 1, 2));
int width = table.getColumnModel().getColumn(column).getWidth();
//setSize(width, 1000);
int rowHeight = getPreferredSize().height;
if (table.getRowHeight(row) != rowHeight)
table.setRowHeight(row, rowHeight);
setText((value == null) ? "" : value.toString());
return this;
}what wrong with this code..
ThanksIn summary, you have one or more columns for which the data must be wholly visible - correct? If you need all the columns to show the whole of their data, you are goinf to have to expand the table, otherwise you can expand a column with something like
myTable.getColumnModel().getColumn(whichever).setPreferredWidth(whatever); -
AutoComplete JComboBox As JTable cell editor
Hello, when I try to use AutoComplete JComboBox as my JTable cell editor, I facing the following problem
1) Exception thrown when show pop up. - Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
2) Unable to capture enter key event.
Here is my complete working code. With the same JComboBox class, I face no problem in adding it at JFrame. But when using it as JTable cell editor, I will have the mentioned problem.
Any advice? Thanks
import javax.swing.*;
import javax.swing.JTable.*;
import javax.swing.table.*;
import java.awt.event.*;
* @author yccheok
public class NewJFrame extends javax.swing.JFrame {
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
/* Combo Box Added In JFrame. Work as expected. */
final JComboBox comboBox = new JComboBox();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Chasing toddlers");
comboBox.setEditable(true);
comboBox.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
System.out.println("is enter");
return;
System.out.println("typed");
comboBox.setSelectedIndex(0);
comboBox.showPopup();
getContentPane().add(comboBox, java.awt.BorderLayout.SOUTH);
public JTable getMyTable() {
return new JTable() {
Combo Box Added In JTable as cell editor. Didn't work as expected:
1. Exception thrown when show pop up.
2. Unable to capture enter key event.
public TableCellEditor getCellEditor(int row, int column) {
final JComboBox comboBox = new JComboBox();
comboBox.addItem("Snowboarding");
comboBox.addItem("Rowing");
comboBox.addItem("Chasing toddlers");
comboBox.setEditable(true);
comboBox.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
System.out.println("is enter");
return;
System.out.println("typed");
comboBox.setSelectedIndex(0);
comboBox.showPopup();
return new DefaultCellEditor(comboBox);
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = getMyTable();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
jScrollPane1.setViewportView(jTable1);
getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>
* @param args the command line arguments
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}You need to create a custom CellEditor which will prevent these problems from occurring. The explanation behind the problem and source code for the new editor can be found at Thomas Bierhance's site http://www.orbital-computer.de/JComboBox/. The description of the problem and the workaround are at the bottom of the page.
Maybe you are looking for
-
i have a iphone 3gs that every time i try restore my iphone a error message come up it said unable to restore iphone because of error (1) what dose this mean pls help me i have try every thing.and it get all the way to the point of restoring firmware
-
Will there ever be an update for the 80 gig that will allow it to have the genius?
I really think all ipods dhould have it.
-
Time machine password not working
Turned on my Mac this AM and the encrypted time machine partition asked for a password and the usual one does not work- it is correct on keychain.
-
Can not get Software Update to work in Mountain Lion, switches to App Store
Can not get Software Update to work in Mountain Lion, switches to App Store
-
Hi All Our company is in the process of doing a POC warehouse where we are using Oracle OLAP extensively for summary management. I have been tasked with porting all our existing reports (Cognos) from using an Informix backend to start using Oracle. T