Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何给JTable上的最小值单元格着色?_Java_Jtable_Tablecellrenderer_Tablemodel_Abstracttablemodel - Fatal编程技术网

Java 如何给JTable上的最小值单元格着色?

Java 如何给JTable上的最小值单元格着色?,java,jtable,tablecellrenderer,tablemodel,abstracttablemodel,Java,Jtable,Tablecellrenderer,Tablemodel,Abstracttablemodel,我正在Java上开发一个小应用程序。我为jtable创建了一个自定义模型。模型是这样的: package tienda.funcionalidad; import java.awt.Component; import java.util.ArrayList; import javax.swing.JTable; import javax.swing.table.AbstractTableModel; import javax.swing.table.TableCellRenderer; i

我正在Java上开发一个小应用程序。我为jtable创建了一个自定义模型。模型是这样的:

package tienda.funcionalidad;

import java.awt.Component;
import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;

import tienda.funcionalidad.excepciones.NombreNoValidoException;
import tienda.funcionalidad.excepciones.PrecioNoValidoException;
import tienda.funcionalidad.excepciones.ProductoNoExisteException;

public class ProductTableModel extends AbstractTableModel implements TableCellRenderer {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    final String[] columns = { "Producto", "Serodys", "Ramírez", "Entrada", "MercaSur" };
    final ArrayList registros = GestionTienda.getProductos();

    @Override
    public int getColumnCount() {
        return columns.length;
    }

    @Override
    public String getColumnName(int column) {
        return columns[column];
    }

    @Override
    public int getRowCount() {
        if (registros.isEmpty())
            return 0;
        return registros.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Product product = (Product) registros.get(rowIndex);
        switch (columnIndex) {
        case 0:
            return product.getName();
        case 1:
            return product.getPriceSerodys();
        case 2:
            return product.getPriceRamirez();
        case 3:
            return product.getPriceEntrada();
        case 4:
            return product.getPriceMercasur();
        }
        return null;
    }

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

    public Class getColumnClass(int col) {
        switch (col) {
        case 0: // Name
            return String.class;
        case 1: // value
            return Double.class;
        case 2: // location
            return Double.class;
        case 3: // quantity
            return Double.class;
        case 4:
            return Double.class;
        }
        return null;
    }

    public void setValueAt(Object value, int row, int col) {
        try {
            Product product = (Product) registros.get(row);
            switch (col) {
            case 0: // Name
                product.setName(value.toString());
                break;
            case 1: // value
                Double priceSerodys = (Double) value;
                product.setPriceSerodys(priceSerodys);
                break;
            case 2: // location
                Double priceRamirez = (Double) value;
                product.setPriceRamirez(priceRamirez);
                break;
            case 3: // quantity
                Double priceEntrada = (Double) value;
                product.setPriceEntrada(priceEntrada);
                break;
            case 4: // quantity
                Double priceMercasur = (Double) value;
                product.setPriceMercasur(priceMercasur);
                break;
            }
        } catch (NombreNoValidoException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (PrecioNoValidoException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public Component getTableCellRendererComponent(JTable arg0, Object arg1, boolean arg2, boolean arg3, int arg4,
            int arg5) {

        return null;
    }

}
这类产品是:

package tienda.funcionalidad;

import java.io.Serializable;

import tienda.funcionalidad.excepciones.NombreNoValidoException;
import tienda.funcionalidad.excepciones.PrecioNoValidoException;

public class Product implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private int id;
    private String name;
    private double priceSerodys;
    private double priceRamirez;
    private double priceEntrada;
    private double priceMercasur;
    private double priceAux = 0;


    public Product(int id,String name, double priceSerodys, double priceRamirez, double priceEntrada, double priceMercasur) throws PrecioNoValidoException, NombreNoValidoException {       
        setName(name);
        setPriceSerodys(priceSerodys);
        setPriceRamirez(priceRamirez);
        setPriceEntrada(priceEntrada);
        setPriceMercasur(priceMercasur);
        setId(id);
    }

    public Product(int id,String nombre) throws NombreNoValidoException {       
        setName(nombre);
        setId(id);
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    protected void setPriceSerodys(Double priceSerodys) throws PrecioNoValidoException {
        if(priceSerodys<0)
            throw new PrecioNoValidoException("Debes introducir un precio superior a 0");
        this.priceSerodys=priceSerodys;
    }

    public double getPriceSerodys() {
        return priceSerodys;
    }

    protected void setPriceRamirez(Double priceRamirez) throws PrecioNoValidoException {
        if(priceRamirez<0)
            throw new PrecioNoValidoException("Debes introducir un precio superior a 0");
        this.priceRamirez=priceRamirez;
    }

    public double getPriceRamirez() {
        return priceRamirez;
    }

    protected void setPriceEntrada(Double priceEntrada) throws PrecioNoValidoException {
        if(priceEntrada<0)
            throw new PrecioNoValidoException("Debes introducir un precio superior a 0");
        this.priceEntrada=priceEntrada;
    }

    public double getPriceEntrada() {
        return priceEntrada;
    }

    protected void setPriceMercasur(Double priceMercasur) throws PrecioNoValidoException {
        if(priceMercasur<0)
            throw new PrecioNoValidoException("Debes introducir un precio superior a 0");
        this.priceMercasur=priceMercasur;
    }

    public double getPriceMercasur() {
        return priceMercasur;
    }

    protected void setName(String nombre) throws NombreNoValidoException {
        if(nombre.equals("") || nombre==null)
            throw new NombreNoValidoException("Debes introducir un nombre para el producto");
        this.name=nombre;
    }

    public String getName() {
        return name;
    }

    public double getPrecio() {
        return priceSerodys;
    }

    @Override
    public String toString() {  
        return getName();
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Product other = (Product) obj;
        if (name == null) {
            if (other.name != null) {
                return false;
            }
        } else if (!name.equals(other.name)) {
            return false;
        }
        return true;
    }



}
package tienda.functionalidad;
导入java.io.Serializable;
导入tienda.funcionalidad.excepciones.NombreNoValidoException;
导入tienda.funcionalidad.excepciones.PRECIONOVILIDEOException;
公共类产品实现可序列化{
/**
* 
*/
私有静态最终长serialVersionUID=1L;
私有int-id;
私有字符串名称;
私人双重价格;
私人双价拉米雷斯;
私人双重价格;
私人双价市场;
私人双倍价格AUX=0;
公共产品(int id、字符串名称、double priceSerodys、double priceRamirez、double priceEntrada、double priceMercasur)抛出PrecionValidoException、NombreNoValidoException{
集合名(名称);
setPriceSerodys(priceSerodys);
setPriceRamirez(priceRamirez);
setpricentrada(pricentrada);
setPriceMercasur(普莱斯麦卡索);
setId(id);
}
公共产品(int-id,字符串nombre)抛出NombreNoValidoException{
集合名(nombre);
setId(id);
}
公共无效集合id(内部id){
this.id=id;
}
公共int getId(){
返回id;
}
受保护的void setPriceSerodys(Double priceSerodys)抛出PrecioNoValidoException{

如果(priceSerodys要获得您想要的结果,可以重写JTable prepareRenderer方法。 请参考下面的代码。这只是一个示例代码,不符合java编码标准。 给你留了那份工作:)

JTable table=新的JTable(新的ProductTableModel()){
@凌驾
公共组件prepareRenderer(TableCellRenderer渲染器、int行索引、,
int列(索引){
JComponent组件=(JComponent)super.prepareRenderer(渲染器、行索引、列索引);
int columnCount=getColumnCount();
如果(列索引!=0){
double firstVal=double.parseDouble(getValueAt(rowIndex,1.toString());
对于(int i=2;i
Tkan各位,这段代码很有用。现在,我尝试在我的代码中编写它。
   JTable table = new JTable(new ProductTableModel()){
        @Override
        public Component prepareRenderer(TableCellRenderer renderer, int rowIndex,
                int columnIndex) {

            JComponent component = (JComponent) super.prepareRenderer(renderer, rowIndex, columnIndex);  

            int columnCount = getColumnCount();

            if(columnIndex != 0){

                double firstVal = Double.parseDouble(getValueAt(rowIndex, 1).toString());
                for (int i = 2; i < columnCount; i++) {
                    Double cellValue = Double.valueOf(getValueAt(rowIndex, i).toString());
                    if(cellValue < firstVal ){
                        firstVal = cellValue;
                    }
                }                    

                if(firstVal == Double.valueOf(getValueAt(rowIndex, columnIndex).toString()).doubleValue()) {
                    component.setBackground(Color.GREEN);
                } else{
                    component.setBackground(Color.GRAY);
                }
            }

            return component;
        }
    };