Justify text in JTable Cell

I have to justify the text in each cells of a JTable.Any help is welcome and source code more!!Thx
[126 byte] By [truongY] at [2007-9-26 4:29:42]
# 1
To set the alignment for all the cells;table.getRenderer().setHorizontalAlignment(SwingConstants.CENTER);
cazza at 2007-6-29 17:41:20 > top of Java-index,Archived Forums,Swing...
# 2
I've no method getRenderer() in JTable but in JComboBox.
truongY at 2007-6-29 17:41:20 > top of Java-index,Archived Forums,Swing...
# 3

Try this hackish utility method I wrote awhile back. Pass it the table, the column index, and the alignment setting you want for that column. It should give you the basic idea of how you can do it using the default cell renderers.

private void _alignTableCells( JTable table, int column, int align )

{

DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();

dtcr.setHorizontalAlignment( align );

TableColumnModel tcm = table.getColumnModel();

TableColumn tc = tcm.getColumn( column );

tc.setCellRenderer( dtcr );

}

Using that method, you could set up your table with something like this:

_alignTableCells( _myTable, 2, JLabel.CENTER );

_alignTableCells( _myTable, 3, JLabel.RIGHT );

_alignTableCells( _myTable, 4, JLabel.RIGHT );

gweedo at 2007-6-29 17:41:20 > top of Java-index,Archived Forums,Swing...
# 4
Try:JLabel label = (JLabel)table.getDefaultRenderer(String.class);label.setHorizontalAligment(SwingConstants.CENTER);
tjwojo at 2007-6-29 17:41:20 > top of Java-index,Archived Forums,Swing...
# 5
thx it works.but i need to justify the text:when the text is smaller than cell, space sizes must be expanded.
truongY at 2007-6-29 17:41:20 > top of Java-index,Archived Forums,Swing...
# 6

There is a code for a Jtable with Justify some text and more....

import javax.swing.*;

import javax.swing.table.*;

import javax.swing.border.*;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Component;

import java.awt.event.*;

public class comboTable {

public comboTable() {

javax.swing.JFrame frame = new javax.swing.JFrame("Table");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {System.exit(0);}});

// Take the dummy data from SwingSet.

final String[] names = {"Nombre", "Edad", "Comida Favorita", "Color Favorito"};

final Object[][] data = {

{"David",new Integer(33), "HotDog", "Verde"},

{"Juan",new Integer(30), "Tacos", "Verde"},

{"Pedro",new Integer(27), "Hamburger", "Azul"},

{"Carlos",new Integer(23), "Vegetables", "Azul"},

{"Matha",new Integer(28), "HotDog", "Amarillo"},

{"Claudia",new Integer(23), "Tacos", "Verde"},

{"Alma",new Integer(22), "Tacos", "Rojo"},

{"Chayito",new Integer(20), "Tacos", "Rojo"}

};

// Create a model of the data.

TableModel dataModel = new AbstractTableModel() {

// These methods always need to be implemented.

public int getColumnCount() { return names.length; }

public int getRowCount() { return data.length;}

public Object getValueAt(int row, int col) {return data[row][col];}

// The default implementations of these methods in

// AbstractTableModel would work, but we can refine them.

public String getColumnName(int column) {return names[column];}

public Class getColumnClass(int c) {return getValueAt(0, c).getClass();}

public boolean isCellEditable(int row, int col) {return true;}

public void setValueAt(Object aValue, int row, int column) {

System.out.println("Setting value to: " + aValue);

data[row][column] = aValue;

}

};

// Create the table

JTable tableView = new JTable(dataModel);

// Turn off auto-resizing so that we can set column sizes programmatically.

// In this mode, all columns will get their preferred widths, as set blow.

tableView.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

// Jcombo Box para dos clumnas diferentes

JComboBox comboBox = new JComboBox();

comboBox.addItem("Rojo");

comboBox.addItem("Azul");

comboBox.addItem("Amarillo");

comboBox.addItem("Verde");

comboBox.addItem("Rojo");

JComboBox foods = new JComboBox();

foods.addItem("HotDog");

foods.addItem("Tacos");

foods.addItem("Hamburger");

foods.addItem("Vegetables");

//El modelo de la tabla

TableColumnModel columnModel = tableView.getColumnModel();

TableColumn colorColumn = columnModel.getColumn(0);

DefaultTableCellRenderer colorColumnRenderer = new DefaultTableCellRenderer();

MyColorRenderer colorColumnRenderer2 = new MyColorRenderer();

//Ponerle color a una columna

//colorColumnRenderer2.setForeground(Color.blue);

colorColumn.setCellRenderer(colorColumnRenderer2);

colorColumn = columnModel.getColumn(1);

JTextField texto= new JTextField();

texto.setHorizontalAlignment(JTextField.RIGHT);

colorColumn.setCellEditor(new customEditor(texto));

colorColumnRenderer = new DefaultTableCellRenderer();

//Ponerle color a una columna

colorColumnRenderer.setForeground(Color.red);

colorColumnRenderer.setBackground(Color.lightGray);

colorColumnRenderer.setHorizontalAlignment(JLabel.RIGHT);

colorColumn.setCellRenderer(colorColumnRenderer);

colorColumn = columnModel.getColumn(2);

// Uso del editor de combo box.

colorColumn.setCellEditor(new DefaultCellEditor(foods));

// Set a foods.

colorColumnRenderer = new DefaultTableCellRenderer();

colorColumnRenderer.setToolTipText("Click para cambiar");

colorColumn.setCellRenderer(colorColumnRenderer);

//colorColumn = tableView.getColumn("Favorite Color");

colorColumn = columnModel.getColumn(3);

colorColumn.setCellEditor(new DefaultCellEditor(comboBox));

colorColumnRenderer = new DefaultTableCellRenderer();

colorColumnRenderer.setBackground(Color.pink);

colorColumnRenderer.setToolTipText("Click for combo box");

colorColumn.setCellRenderer(colorColumnRenderer);

tableView.requestFocus();

tableView.editCellAt(2, 1);

tableView.setRowSelectionInterval(2, 2);

// Finish setting up the table.

JScrollPane scrollpane = new JScrollPane(tableView);

scrollpane.setBorder(new BevelBorder(BevelBorder.LOWERED));

scrollpane.setPreferredSize(new Dimension(430, 200));

frame.getContentPane().add(scrollpane);

frame.pack();

frame.setVisible(true);

}

public class customEditor extends DefaultCellEditor {

public Component editorComponent;

public customEditor(JTextField t) {

super(t);

editorComponent=t;

}

public Component getTableCellEditorComponent

(JTable table, Object value, boolean isSelected, int row, int col) {

((JTextField)editorComponent).setText(value.toString());

table.repaint();

//Esto es para sobreescribir.

((JTextField)editorComponent).selectAll();

return editorComponent;

}

}//close customEditor class.

class MyColorRenderer extends DefaultTableCellRenderer {

public Component getTableCellRendererComponent(

JTable myTable, Object value, boolean isSelected,

boolean hasFocus, int row, int column) {

super.getTableCellRendererComponent(myTable,

value, isSelected, hasFocus, row, column);

setOpaque(true);

if (isSelected) {

setForeground(myTable.getSelectionForeground());

setBackground(myTable.getSelectionBackground());

} else {

setBackground((row == 1?(new Color(24,35,87,128)): Color.white));

setForeground((row == 1? Color.white: (new Color(24,35,87,128))));

}

return this;

}

}

public static void main(String[] args) {

new comboTable();

}

}

Hope this helps

pedro.garcia at 2007-6-29 17:41:20 > top of Java-index,Archived Forums,Swing...