Vector object[]

Ads

 
 

Share on Google+Share on Google+

Pushpa Sheela
Vector object[]
4 Answer(s)      6 years and 7 months ago
Posted in : Java Beginners

I want to create payslip in java

public Vector createData()
{
    Vector result = new Vector();
    Vector rec = new Vector();
    Object[] value = new Object[] {"Employee Name",f.ename.getSelectedItem()};
    rec.add(value);
    value = new Object[] {"Mary-Kate Olsen", "Ashley Olsen"};
    rec.add(value);
    result.add(rec);
    rec = new Vector();
    value = new Object[] {"Charlie Read", "Craig Read"};
    rec.add(value);
    value = new Object[] {"Tegan Quin", "Sara Quin"};
    rec.add(value);
    result.add(rec);
    return result;
}

I have exception in this line

Object[] value = new Object[] {"Employee Name",f.ename.getSelectedItem()};

I cant display the employee name. The employee name changed at any time. Its depend upon the user selection. But exception thrown. Please help me.

Ads
View Answers

October 29, 2010 at 3:42 PM


Hello Friend,

Please post your full code.

Thanks



October 29, 2010 at 4:00 PM


This is my full code.

import javax.swing.table.*;

import javax.swing.*; import java.util.*; import java.awt.*;

public class Salary_report extends JFrame {

Vector data;
JTable table;
JLabel banner;  
ImageIcon images;
Container c;
Employee_report f;
public Salary_report(Employee_report f)
{
    c=getContentPane();
c.setLayout(null);
setResizable(false);
setTitle("Employee Salary Report");
setBounds(250,50,700,750);

    data = createData();

    SplitCellTableModel model = new SplitCellTableModel();
    table = new JTable(model);
    table.getColumnModel().getColumn(0).setCellEditor(new SplitTableCellEditor());
    table.getColumnModel().getColumn(1).setCellEditor(new SplitTableCellEditor());
    table.getColumnModel().getColumn(0).setCellRenderer(new SplitTableCellRenderer());
    table.getColumnModel().getColumn(1).setCellRenderer(new SplitTableCellRenderer());
    JScrollPane scroller = new JScrollPane(table);


    images=new ImageIcon("TWA.jpg");
banner=new JLabel();
banner.setIcon(images);
banner.setBounds(60,50,600,150);
c.add(banner,BorderLayout.NORTH);
System.out.println(f.ename.getSelectedItem().toString());
scroller.setBounds(60,250,600,100);
c.add(scroller);    
}

public Vector createData()
{
    Vector result = new Vector();
    Vector rec = new Vector();
    Object[] value = new Object[] {"Employee Name",f.ename.getSelectedItem().toString()};
    rec.add(value);
    value = new Object[] {"Total Days",f.wdays.getText() };
    rec.add(value);
    result.add(rec);

    rec = new Vector();
    value = new Object[] {"Bank A/C No", f.bankac.getText()};
    rec.add(value);
    value = new Object[] {"Basic", f.txtbasic.getText()};
    rec.add(value);
    result.add(rec);
return result;
}

public class SplitCellTableModel extends DefaultTableModel { public SplitCellTableModel() { }

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

public int getColumnCount()
{
    return 2;
}

public String getColumnName(int col)
{
    if (col == 0)
        return " ";
    else
        return "Date";
}

public Object getValueAt(int row, int col)
{
    Vector rec = (Vector)data.get(row);
    return rec.get(col);
}

public Vector getDataVector()
{
    return data;
}

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

public void setValueAt(Object value, int row, int col)
{
    Vector rec = (Vector)data.get(row);
    rec.setElementAt(value, col);
}

}

public class SplitTableCellEditor extends AbstractCellEditor implements TableCellEditor { JPanel editingPanel; JTextField leftTextField; JTextField rightTextField;

public SplitTableCellEditor()
{
    super();
    editingPanel = new JPanel();
    editingPanel.setLayout(new GridLayout(1, 2));
    leftTextField = new JTextField();
    editingPanel.add(leftTextField);
    rightTextField = new JTextField();
    editingPanel.add(rightTextField);
}

public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int rowIndex, int colIndex)
{
    Object[] splitVal = (Object[])value; // cast to array of Object
    if (splitVal != null)
    {
        String leftStr = (String)splitVal[0];
        String rightStr = (String)splitVal[1];
        leftTextField.setText(leftStr == null ? "" : leftStr);
        rightTextField.setText(rightStr == null ? "" : rightStr);
    }
    else
    {
        leftTextField.setText("");
        rightTextField.setText("");
    }
    return editingPanel;
}

public Object getCellEditorValue()
{
    Object[] value = new Object[] {leftTextField.getText(), rightTextField.getText()};
    return value;
}

}

public class SplitTableCellRenderer implements TableCellRenderer { JPanel rendererPanel; JLabel leftLabel; JLabel rightLabel;

public SplitTableCellRenderer()
{
    super();

    rendererPanel = new JPanel();
    rendererPanel.setLayout(new GridLayout(1, 2));
    leftLabel = new JLabel();
    leftLabel.setOpaque(true);
    leftLabel.setBackground(Color.white);
    rendererPanel.add(leftLabel);
    rightLabel = new JLabel();
    rightLabel.setOpaque(true);
    rightLabel.setBackground(Color.white);
    rendererPanel.add(rightLabel);
}

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col)
{
    Object[] splitVal = (Object[])value; // cast to array of Object
    if (splitVal != null)
    {
        String leftStr = (String)splitVal[0];
        String rightStr = (String)splitVal[1];
        leftLabel.setText(leftStr == null ? "" : leftStr);
        rightLabel.setText(rightStr == null ? "" : rightStr);
    }
    else
    {
        leftLabel.setText("");
        rightLabel.setText("");
    }
    return rendererPanel;
}

}

}

Please give me the idea as soon as possible



October 29, 2010 at 5:18 PM


Hi Friend,

Here is your code:

import javax.swing.table.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;

public class Salary_report extends JFrame {
Vector data;
JTable table;
JLabel banner;  
ImageIcon images;
Container c;
Employee_report f;
public Salary_report(Employee_report f){
    super();
    c=getContentPane();
c.setLayout(null);
setResizable(false);
setTitle("Employee Salary Report");
setBounds(250,50,700,750);
    data = createData();
    SplitCellTableModel model = new SplitCellTableModel();
    table = new JTable(model);
    table.getColumnModel().getColumn(0).setCellEditor(new SplitTableCellEditor());
    table.getColumnModel().getColumn(1).setCellEditor(new SplitTableCellEditor());
    table.getColumnModel().getColumn(0).setCellRenderer(new SplitTableCellRenderer());
    table.getColumnModel().getColumn(1).setCellRenderer(new SplitTableCellRenderer());
    JScrollPane scroller = new JScrollPane(table);
    images=new ImageIcon("TWA.jpg");
banner=new JLabel();
banner.setIcon(images);
banner.setBounds(60,50,600,150);
c.add(banner,BorderLayout.NORTH);
System.out.println(f.ename.getSelectedItem().toString());
scroller.setBounds(60,250,600,100);
c.add(scroller);    
}
public Vector createData(){
    Vector result = new Vector();
    Vector rec = new Vector();
    Object[] value = new Object[] {"Employee Name",f.ename.getSelectedItem().toString()};
    rec.add(value);
    value = new Object[] {"Total Days",f.wdays.getText() };
    rec.add(value);
    result.add(rec);

    rec = new Vector();
    value = new Object[] {"Bank A/C No", f.bankac.getText()};
    rec.add(value);
    value = new Object[] {"Basic", f.txtbasic.getText()};
    rec.add(value);
    result.add(rec);
return result;
}
 class SplitCellTableModel extends DefaultTableModel { public SplitCellTableModel() { }
public int getRowCount(){
    return data.size();
}
public int getColumnCount(){
    return 2;
}
public String getColumnName(int col){
    if (col == 0)
        return " ";
    else
        return "Date";
}
public Object getValueAt(int row, int col){
    Vector rec = (Vector)data.get(row);
    return rec.get(col);
}
public Vector getDataVector(){
    return data;
}
public boolean isCellEditable(int row, int col){
    return true;
}
public void setValueAt(Object value, int row, int col){
    Vector rec = (Vector)data.get(row);
    rec.setElementAt(value, col);
}
}
 class SplitTableCellEditor extends AbstractCellEditor implements TableCellEditor { JPanel editingPanel; JTextField leftTextField; JTextField rightTextField;
public SplitTableCellEditor(){
    super();
    editingPanel = new JPanel();
    editingPanel.setLayout(new GridLayout(1, 2));
    leftTextField = new JTextField();
    editingPanel.add(leftTextField);
    rightTextField = new JTextField();
    editingPanel.add(rightTextField);
}
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int rowIndex, int colIndex)
{
    Object[] splitVal = (Object[])value;
    if (splitVal != null)    {
        String leftStr = (String)splitVal[0];
        String rightStr = (String)splitVal[1];
        leftTextField.setText(leftStr == null ? "" : leftStr);
        rightTextField.setText(rightStr == null ? "" : rightStr);
    }
    else    {
        leftTextField.setText("");
        rightTextField.setText("");
    }
    return editingPanel;
}
public Object getCellEditorValue(){
    Object[] value = new Object[] {leftTextField.getText(), rightTextField.getText()};
    return value;
}
}


October 29, 2010 at 5:19 PM


continue..

class SplitTableCellRenderer implements TableCellRenderer { JPanel rendererPanel; JLabel leftLabel; JLabel rightLabel;
    public SplitTableCellRenderer(){
        super();
        rendererPanel = new JPanel();
        rendererPanel.setLayout(new GridLayout(1, 2));
        leftLabel = new JLabel();
        leftLabel.setOpaque(true);
        leftLabel.setBackground(Color.white);
        rendererPanel.add(leftLabel);
        rightLabel = new JLabel();
        rightLabel.setOpaque(true);
        rightLabel.setBackground(Color.white);
        rendererPanel.add(rightLabel);
    }
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col)
    {
        Object[] splitVal = (Object[])value; 
        if (splitVal != null)    {
            String leftStr = (String)splitVal[0];
            String rightStr = (String)splitVal[1];
            leftLabel.setText(leftStr == null ? "" : leftStr);
            rightLabel.setText(rightStr == null ? "" : rightStr);
        }
        else{
            leftLabel.setText("");
            rightLabel.setText("");
        }
        return rendererPanel;
    }
     }
    }

Put the whole code in a combined way.

Thanks


Ads









Related Tutorials/Questions & Answers:

Ads

 
Advertisement null

Ads