java swing 表格相关操作

java swing 表格相关操作

(1)设置表格数据

public static void setTableData3(JTable parameterTable_1,Map requestMap,boolean hasTextField,boolean isTF_table_cell,String[]columnNames) {
    	System.out.println("setTableData3");
        int length = requestMap.size();
        if (length > 0) {
            Object[][] datas = new Object[length][];
            int count = 0;
            for (Object obj : requestMap.keySet()) {
                Object val = requestMap.get(obj);
                Object[] objs = new Object[3];
                RadioButtonPanel panel = new RadioButtonPanel();
                panel.init(hasTextField);
                objs[2] = panel;
                Color backColor=CustomColor.getMoreLightColor();
//		    		objs[2]="c"+i;
                if(isTF_table_cell){
                	JTextArea keyTA=new AssistPopupTextArea(String.valueOf(obj));
                	keyTA.setBackground(backColor);
                	objs[0] = new JScrollPane(keyTA);
                }else{
                	objs[0] =obj;
                }
                
                if (ValueWidget.isNullOrEmpty(val) || val.equals("null")
                		|| val.equals("undefined")) {//配置文件中保存的是"null",而不是null
                    val = SystemHWUtil.EMPTY;
                }
                if(isTF_table_cell){
                	JTextArea valTA=new GenerateJsonTextArea(String.valueOf(val));
                	valTA.setBackground(backColor);
                	objs[1] = new JScrollPane(valTA);
                }else{
                	objs[1] = val;
                }
                
                datas[count] = objs;
                count++;
            }//for
//            setTableData2(parameterTable_1,datas,columnNames);
            appendTableData(parameterTable_1, datas, columnNames);
        }//if
    }
    private static void setTableData2(JTable parameterTable_1,Object[][] datas,String[]columnNames) {
        DefaultTableModel model = new DefaultTableModel(datas, columnNames);
        parameterTable_1.setModel(model);
        parameterTable_1.setRowHeight(30);
//        rendTable();
    }
    /***
     * 追加,原有输入框仍保留,所以可以使用Ctrl+Z
     * @param datas
     */
    public static void appendTableData(JTable parameterTable_1,Object[][] datas,String[]columnNames){
    	int rowCount=parameterTable_1.getRowCount();
    	if(rowCount<1){//如果表格本来为空
    		setTableData2(parameterTable_1,datas,columnNames);
    	}else{//表格不为空
    		for(int i=0;i<rowCount;i++){
    			setTableValueAt(parameterTable_1,i, 0, datas);
    			setTableValueAt(parameterTable_1,i, 1, datas);
    		}
    		DefaultTableModel tableModel = (DefaultTableModel) parameterTable_1.getModel();
    		for(int i=rowCount;i<datas.length;i++){
    			tableModel.addRow(datas[i]);
    		}
    	}
    }
    /***
     * 设置表格单元格的值
     * @param rowIndex
     * @param columnIndex
     * @param datas
     */
    private static void setTableValueAt(JTable parameterTable_1,int rowIndex,int columnIndex,Object[][] datas){
    	Object keyObj=datas[rowIndex][columnIndex];
		if(keyObj instanceof JScrollPane){
			JScrollPane js=(JScrollPane)keyObj;
        	JTextComponent tf=(JTextComponent)js.getViewport().getComponent(0);
        	keyObj = tf.getText();
		}
		String key=null;
		if(keyObj instanceof String){
			key=(String)keyObj;
		}else{
			key=String.valueOf(keyObj);
		}
    	Object valueAtObj=parameterTable_1.getValueAt(rowIndex, columnIndex);
    	if(valueAtObj instanceof JScrollPane){
    		JScrollPane keyScrollPane=(JScrollPane)valueAtObj;
    		JTextArea keyTA=(JTextArea)keyScrollPane.getViewport().getComponent(0);
    		keyTA.setText(key);
    	}else{
    		parameterTable_1.setValueAt(keyObj, rowIndex, columnIndex);
    	}
		
    }

 应用:

 if(requestInfoBean.isRequestBodyIsJson()){//"请求参数"表格不可用
        	requestBodyDataTA.setText(requestInfoBean.getRequestBodyData());
        }else{
        	TableUtil3.setTableData3(parameterTable_1,requestInfoBean.getRequestParameters(),true,true,columnNames);
        	rendTable();
        }
        
public void layoutTable( ) {
//    	Object[][] datas=new Object[1][2];
    	Object[][] datas=new Object[1][3];
    	for(int i=0;i<datas.length;i++){
    		Object[]objs=new Object[3];
    		RadioButtonPanel panel=new RadioButtonPanel();
    		panel.init();
    		objs[2]=panel;
    		if(AutoTestPanel.isTF_table_cell){
    			objs[0]= new JScrollPane(new AssistPopupTextArea()) ;
        		objs[1]=new JScrollPane(new GenerateJsonTextArea());
    		}
    		datas[i]=objs;
    	}
    	TableUtil3.appendTableData(parameterTable_1,datas,columnNames);
//        setTableData2(datas);
//    	repaintTable();
		
	}

 

 

(2)清空表格数据

/***
     * 清空单元格<br>
     * @param parameterTable_1
     * @param rowIndex
     * @param columnIndex
     */
    private static void cleanTableValue(JTable parameterTable_1,int rowIndex,int columnIndex){
    	Object valueAtObj=parameterTable_1.getValueAt(rowIndex, columnIndex);
    	if(valueAtObj instanceof JScrollPane){
    		JScrollPane keyScrollPane=(JScrollPane)valueAtObj;
    		JTextArea keyTA=(JTextArea)keyScrollPane.getViewport().getComponent(0);
    		keyTA.setText(SystemHWUtil.EMPTY);
    	}else{
    		parameterTable_1.setValueAt(null, rowIndex, columnIndex);
    	}
    }
    /***
     * 清空表格数据<br>
     * @param parameterTable_1
     */
    public static void cleanTableData(JTable parameterTable_1){
    	int rowCount=parameterTable_1.getRowCount();
    	if(rowCount>0){
    		for(int i=0;i<rowCount;i++){
    			cleanTableValue(parameterTable_1,i, 0);
    			cleanTableValue(parameterTable_1,i, 1);
    		}
    	}
    }

 应用:

 JButton cleanUpTable_66 = new JButton("清空表格");
        cleanUpTable_66.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		TableUtil3.cleanTableData(parameterTable_1);
        		parameterTable_1.repaint();
        	}
        });

 

 

(3)给表格增加一行

/***
     * 表格增加一行
     */
    public static void addParameter(JTable parameterTable_1,String key,boolean hasTextField,boolean isTF_table_cell) {
        System.out.println("增加一行");
        DefaultTableModel tableModel = (DefaultTableModel) parameterTable_1.getModel();
        RadioButtonPanel panel = new RadioButtonPanel();
        panel.init(hasTextField);
        String val=null;
        if(!ValueWidget.isNullOrEmpty(key)){
        	if(key.contains("=")||key.contains(":")){
            	String[]strs=key.split("[:=]");
            	key=strs[0];
            	val=strs[1];
            }
        }
        
        Object[] rowData =null; 
        if(isTF_table_cell){
        	Color clor=CustomColor.getMoreLightColor();
        	JTextArea keyTA=new AssistPopupTextArea(key);
        	keyTA.setBackground(clor);
        	JComponent keyTA2=new JScrollPane(keyTA);
        	
        	JTextArea valTA=new GenerateJsonTextArea(val);
        	valTA.setBackground(clor);
        	JComponent valScroll=new JScrollPane(valTA);
        	rowData =new Object[]{keyTA2, valScroll, panel};
        }else{
        	rowData =new Object[]{key, val, panel};
        }
        tableModel.addRow(rowData);
    }

 应用:

/***
     * 表格增加一行
     */
    private void addParameter(String key) {
    	TableUtil.addParameter(this.parameterTable_1, key,true,AutoTestPanel.isTF_table_cell);
    }

JButton addRowButton = new JButton("添加");
        addRowButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addParameter(null);
            }
        });

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值