同时小弟也想知道怎样让文本居中对齐?
7 个解决方案
#1
ding
#2
mark
#3
写一个类继承 javax.swing.table.DefaultTAbleCellRenderer
public class XxxRenderer extends DefaultTableCellRenderer {
public XxxRenderer (){
super();
setHorizentalAlignment(javax.swing.SwingConstants.RIGHT);
}
public void setValue(Object value){
//这里格式化你的数据
super.setValue(value);
}
}
.......
然后 table.setDefaultRenderer(Float.class,new XxxRenderer ());
public class XxxRenderer extends DefaultTableCellRenderer {
public XxxRenderer (){
super();
setHorizentalAlignment(javax.swing.SwingConstants.RIGHT);
}
public void setValue(Object value){
//这里格式化你的数据
super.setValue(value);
}
}
.......
然后 table.setDefaultRenderer(Float.class,new XxxRenderer ());
#4
好像不关用啊
还有别的什么要求吗
比如说对Jtable的构造方式了。
还有别的什么要求吗
比如说对Jtable的构造方式了。
#5
http://community.csdn.net/Expert/TopicView.asp?id=4407145
#6
谢了楼上的各位。但小弟还是没有实现。请各位帮我看看是怎么回事
package java试验1;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.*;
import java.awt.*;
import javax.swing.table.DefaultTableModel;
public class JtableFrame extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
JTable jTable1 = new JTable();
DefaultTableModel dm=new DefaultTableModel();
String[] columname={"aa","bb","cc"};
Object [][] data={{"11","22"},{"33","44"}};
public JtableFrame() {
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
void jbInit() throws Exception {
jTable1.setDefaultRenderer(Float.class,new Test ());
dm = new DefaultTableModel(data, columname);
dm.setValueAt("111", 1, 1);
jTable1 = new JTable(dm);
jTable1.setEnabled(true);
jTable1.editCellAt(1, 1);
System.out.println(jTable1.getTableHeader());
System.out.println(jTable1.getValueAt(1, 1));
// jTable1.setDefaultRenderer(Float.class,new NumberRenderer ());//为什么不其作用呢?
jTable1.setDefaultRenderer(Float.class,new Test());//为什么不其作用呢?
this.setBounds(100,100,400,400);
this.getContentPane().setLayout(borderLayout1);
this.getContentPane().add(jTable1, BorderLayout.CENTER);
}
public static void main(String[] args) {
JtableFrame jtable = new JtableFrame();
jtable.setVisible(true);
}
}
class Test extends DefaultTableCellRenderer{
/* (non-Javadoc)
* @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
*/
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
JLabel label = (JLabel)super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
if (value instanceof Integer){
label.setHorizontalAlignment(SwingConstants.RIGHT);
}else{
setHorizontalAlignment(SwingConstants.CENTER);
}
return label;
}
}
class NumberRenderer extends DefaultTableCellRenderer {
public NumberRenderer (){
super();
setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
}
public void setValue(Object value){
//这里格式化你的数据
super.setValue("666");
}
}
package java试验1;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.*;
import java.awt.*;
import javax.swing.table.DefaultTableModel;
public class JtableFrame extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
JTable jTable1 = new JTable();
DefaultTableModel dm=new DefaultTableModel();
String[] columname={"aa","bb","cc"};
Object [][] data={{"11","22"},{"33","44"}};
public JtableFrame() {
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
void jbInit() throws Exception {
jTable1.setDefaultRenderer(Float.class,new Test ());
dm = new DefaultTableModel(data, columname);
dm.setValueAt("111", 1, 1);
jTable1 = new JTable(dm);
jTable1.setEnabled(true);
jTable1.editCellAt(1, 1);
System.out.println(jTable1.getTableHeader());
System.out.println(jTable1.getValueAt(1, 1));
// jTable1.setDefaultRenderer(Float.class,new NumberRenderer ());//为什么不其作用呢?
jTable1.setDefaultRenderer(Float.class,new Test());//为什么不其作用呢?
this.setBounds(100,100,400,400);
this.getContentPane().setLayout(borderLayout1);
this.getContentPane().add(jTable1, BorderLayout.CENTER);
}
public static void main(String[] args) {
JtableFrame jtable = new JtableFrame();
jtable.setVisible(true);
}
}
class Test extends DefaultTableCellRenderer{
/* (non-Javadoc)
* @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
*/
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
JLabel label = (JLabel)super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
if (value instanceof Integer){
label.setHorizontalAlignment(SwingConstants.RIGHT);
}else{
setHorizontalAlignment(SwingConstants.CENTER);
}
return label;
}
}
class NumberRenderer extends DefaultTableCellRenderer {
public NumberRenderer (){
super();
setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
}
public void setValue(Object value){
//这里格式化你的数据
super.setValue("666");
}
}
#7
jTable1.setDefaultRenderer(Float.class,new Test ());
dm = new DefaultTableModel(data, columname);
dm.setValueAt("111", 1, 1);
jTable1 = new JTable(dm);
=======================================================
这段代码有问题。
jTable1.setDefaultRenderer(Float.class,new Test ());
放到jTable1 = new JTable(dm);后面
而且:如果你要使用Float.class,下面的
Object [][] data={{"11","22"},{"33","44"}};也不对。
Object [][] data={{Float.valueof("11"),Float.valueof("11")}};
而render里头的instanceof Integer应该改成instanceof Float
dm = new DefaultTableModel(data, columname);
dm.setValueAt("111", 1, 1);
jTable1 = new JTable(dm);
=======================================================
这段代码有问题。
jTable1.setDefaultRenderer(Float.class,new Test ());
放到jTable1 = new JTable(dm);后面
而且:如果你要使用Float.class,下面的
Object [][] data={{"11","22"},{"33","44"}};也不对。
Object [][] data={{Float.valueof("11"),Float.valueof("11")}};
而render里头的instanceof Integer应该改成instanceof Float
#1
ding
#2
mark
#3
写一个类继承 javax.swing.table.DefaultTAbleCellRenderer
public class XxxRenderer extends DefaultTableCellRenderer {
public XxxRenderer (){
super();
setHorizentalAlignment(javax.swing.SwingConstants.RIGHT);
}
public void setValue(Object value){
//这里格式化你的数据
super.setValue(value);
}
}
.......
然后 table.setDefaultRenderer(Float.class,new XxxRenderer ());
public class XxxRenderer extends DefaultTableCellRenderer {
public XxxRenderer (){
super();
setHorizentalAlignment(javax.swing.SwingConstants.RIGHT);
}
public void setValue(Object value){
//这里格式化你的数据
super.setValue(value);
}
}
.......
然后 table.setDefaultRenderer(Float.class,new XxxRenderer ());
#4
好像不关用啊
还有别的什么要求吗
比如说对Jtable的构造方式了。
还有别的什么要求吗
比如说对Jtable的构造方式了。
#5
http://community.csdn.net/Expert/TopicView.asp?id=4407145
#6
谢了楼上的各位。但小弟还是没有实现。请各位帮我看看是怎么回事
package java试验1;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.*;
import java.awt.*;
import javax.swing.table.DefaultTableModel;
public class JtableFrame extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
JTable jTable1 = new JTable();
DefaultTableModel dm=new DefaultTableModel();
String[] columname={"aa","bb","cc"};
Object [][] data={{"11","22"},{"33","44"}};
public JtableFrame() {
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
void jbInit() throws Exception {
jTable1.setDefaultRenderer(Float.class,new Test ());
dm = new DefaultTableModel(data, columname);
dm.setValueAt("111", 1, 1);
jTable1 = new JTable(dm);
jTable1.setEnabled(true);
jTable1.editCellAt(1, 1);
System.out.println(jTable1.getTableHeader());
System.out.println(jTable1.getValueAt(1, 1));
// jTable1.setDefaultRenderer(Float.class,new NumberRenderer ());//为什么不其作用呢?
jTable1.setDefaultRenderer(Float.class,new Test());//为什么不其作用呢?
this.setBounds(100,100,400,400);
this.getContentPane().setLayout(borderLayout1);
this.getContentPane().add(jTable1, BorderLayout.CENTER);
}
public static void main(String[] args) {
JtableFrame jtable = new JtableFrame();
jtable.setVisible(true);
}
}
class Test extends DefaultTableCellRenderer{
/* (non-Javadoc)
* @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
*/
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
JLabel label = (JLabel)super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
if (value instanceof Integer){
label.setHorizontalAlignment(SwingConstants.RIGHT);
}else{
setHorizontalAlignment(SwingConstants.CENTER);
}
return label;
}
}
class NumberRenderer extends DefaultTableCellRenderer {
public NumberRenderer (){
super();
setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
}
public void setValue(Object value){
//这里格式化你的数据
super.setValue("666");
}
}
package java试验1;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.*;
import java.awt.*;
import javax.swing.table.DefaultTableModel;
public class JtableFrame extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
JTable jTable1 = new JTable();
DefaultTableModel dm=new DefaultTableModel();
String[] columname={"aa","bb","cc"};
Object [][] data={{"11","22"},{"33","44"}};
public JtableFrame() {
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
void jbInit() throws Exception {
jTable1.setDefaultRenderer(Float.class,new Test ());
dm = new DefaultTableModel(data, columname);
dm.setValueAt("111", 1, 1);
jTable1 = new JTable(dm);
jTable1.setEnabled(true);
jTable1.editCellAt(1, 1);
System.out.println(jTable1.getTableHeader());
System.out.println(jTable1.getValueAt(1, 1));
// jTable1.setDefaultRenderer(Float.class,new NumberRenderer ());//为什么不其作用呢?
jTable1.setDefaultRenderer(Float.class,new Test());//为什么不其作用呢?
this.setBounds(100,100,400,400);
this.getContentPane().setLayout(borderLayout1);
this.getContentPane().add(jTable1, BorderLayout.CENTER);
}
public static void main(String[] args) {
JtableFrame jtable = new JtableFrame();
jtable.setVisible(true);
}
}
class Test extends DefaultTableCellRenderer{
/* (non-Javadoc)
* @see javax.swing.table.TableCellRenderer#getTableCellRendererComponent(javax.swing.JTable, java.lang.Object, boolean, boolean, int, int)
*/
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
JLabel label = (JLabel)super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
if (value instanceof Integer){
label.setHorizontalAlignment(SwingConstants.RIGHT);
}else{
setHorizontalAlignment(SwingConstants.CENTER);
}
return label;
}
}
class NumberRenderer extends DefaultTableCellRenderer {
public NumberRenderer (){
super();
setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
}
public void setValue(Object value){
//这里格式化你的数据
super.setValue("666");
}
}
#7
jTable1.setDefaultRenderer(Float.class,new Test ());
dm = new DefaultTableModel(data, columname);
dm.setValueAt("111", 1, 1);
jTable1 = new JTable(dm);
=======================================================
这段代码有问题。
jTable1.setDefaultRenderer(Float.class,new Test ());
放到jTable1 = new JTable(dm);后面
而且:如果你要使用Float.class,下面的
Object [][] data={{"11","22"},{"33","44"}};也不对。
Object [][] data={{Float.valueof("11"),Float.valueof("11")}};
而render里头的instanceof Integer应该改成instanceof Float
dm = new DefaultTableModel(data, columname);
dm.setValueAt("111", 1, 1);
jTable1 = new JTable(dm);
=======================================================
这段代码有问题。
jTable1.setDefaultRenderer(Float.class,new Test ());
放到jTable1 = new JTable(dm);后面
而且:如果你要使用Float.class,下面的
Object [][] data={{"11","22"},{"33","44"}};也不对。
Object [][] data={{Float.valueof("11"),Float.valueof("11")}};
而render里头的instanceof Integer应该改成instanceof Float