0

Hello This is my first class where I created a JTable in JFrame.

public class code_selection extends JFrame implements MouseListener {

    public JTable table;
    private Color myback=new Color(0,85,170);
    public int v,row,col;
    public int h,str1;
    public JScrollPane jsp;
    public JPanel jp;
    private int red=10;
    private int green=125;
    private int blue=10;
    private Color mygreen=new Color(red,green,blue);

    conn co=new conn();
    public String str;

    public String st2[]={"NAME","CODE"} ;
    public String st3[][]={{"navneet","321"},
               {"hehehe","123"},
               {"hehehe","456"},
               {"hehehe","789"}};

    public code_selection()
    {

        setSize(620,400);
        setTitle("Navneet Software Solutions");
        Container contentPane=getContentPane();
        contentPane.setLayout(null);
        contentPane.setBackground(mygreen);
        setLocationRelativeTo(null);
        setUndecorated(true);

        table=new JTable(st3,st2);
        table.setBackground(Color.WHITE);
        table.setForeground(Color.BLACK);
        table.setGridColor(Color.BLACK);
        table.setBounds(0, 0, 400, 500);
        table.addMouseListener(this);

        v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
        h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
        jsp=new JScrollPane(table,v,h);
        jsp.setBounds(10,10,600,380);

        add(jsp);

        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
        // TODO Auto-generated method stub
        try {

            row=table.getSelectedRow();
            col=0;
            str=table.getModel().getValueAt(row, col).toString();
            col=1;
            str1=Integer.parseInt(table.getModel().getValueAt(row, col).toString());

            dispose();

        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub
    }   
}

This is my second class where I created frame with text field and button. When I click on the button my table frame open.

public class amt_deposit extends JPanel implements MouseListener {

    private ImageIcon i1,i2,i3,i4;
    private JLabel l1,l2,l3,l4,l5,l6;
    public JTextField t2,t3;
    private JButton b1,b2,b3;
    private DatePicker datePicker=new DatePicker();
    private Color myback=new Color(0,85,170);
    public int st;
    conn con1=new conn();

    public amt_deposit() {

        try {

            setLayout(null);
            setBackground(myback);

            i1=new ImageIcon("images/deposit.png");
            l1=new JLabel(i1);
            l1.setBounds(10,10,70,85);

            l2=new JLabel("Amount Deposit");
            l2.setBounds(83, 50, 200, 50);
            l2.setForeground(Color.WHITE);
            l2.setFont(new Font("arial",Font.BOLD,21));
            l2.setBackground(myback);
            l2.setOpaque(true);

            l5=new JLabel("Date :- ");
            l5.setBounds(100,150,100,50);
            l5.setForeground(Color.WHITE);
            l5.setFont(new Font("arial",Font.BOLD,16));
            l5.setBackground(myback);
            l5.setOpaque(true);

            datePicker.setBounds(230,163,165,23);
            datePicker.setText("Select");

            l3=new JLabel("Patient Code :- ");
            l3.setBounds(100,200,130,50);
            l3.setForeground(Color.WHITE);
            l3.setFont(new Font("arial",Font.BOLD,16));
            l3.setBackground(myback);
            l3.setOpaque(true);

            b3=new JButton("...");
            b3.setBounds(370, 213, 30, 25);
            //b3.addActionListener(this);
            b3.addMouseListener(this);
            //b3.setBackground(myback);
            //b3.setOpaque(true);

            t2=new JTextField();
            t2.setBounds(230,213,140,25);
            t2.addMouseListener(this);

            l4=new JLabel("Amount :- ");
            l4.setBounds(100,250,100,50);
            l4.setForeground(Color.WHITE);
            l4.setFont(new Font("arial",Font.BOLD,16));
            l4.setBackground(myback);
            l4.setOpaque(true);

            t3=new JTextField();
            t3.setBounds(230,263,140,25);

            i2=new ImageIcon("images/ok2.png");
            b1=new JButton(i2);
            b1.setBounds(80,360,110,40);
            b1.setBackground(myback);
            b1.setOpaque(true);


            i3=new ImageIcon("images/cancel2.png");
            b2=new JButton(i3);
            b2.setBounds(220,360,110,40);
            b2.setBackground(myback);
            b2.setOpaque(true);

            i4=new ImageIcon("images/owl.png");
            l6=new JLabel(i4);
            l6.setBounds(800,30,200,120);

            add(l1);
            add(l2);
            add(l3);
            add(l4);
            add(l5);
            add(datePicker);
            add(t2);
            add(t3);
            add(b1);
            add(b2);
            add(l6);
            add(b3);

            setVisible(true);

        }
        catch(Exception e)
        {
            System.out.println("Error in amt_deposit "+e);
        }
    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
        // TODO Auto-generated method stub

        try {

            code_selection cs=new code_selection(); 
            st=cs.table.getRowCount();

            String str5=cs.table.getModel().getValueAt(st, 0).toString();
            String str6=cs.table.getModel().getValueAt(st, 1).toString();

            System.out.println(str5+str6);

            t2.setText(str5);

        }
        catch(Exception e)
        {
            System.out.println(e);  
        }
    }
}

When I click on button then my frame open in which I created a table. I want to get selected value from the table and set on the text field which is on another frame.

I am using right procedure and correct methods but still I am getting error.

java.lang.ArrayIndexOutOfBoundsException:-1

Could you please help me? Could you please tell me where I am wrong?

For your information I am able to get value from the table on same frame on which table is located but I am not able to get selected value from table from frame to another frame.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • You have a number of possible choices, problem what you really want is a modal JDialog, which will block until it's closed, at which time you can get information from the dialog that you want – MadProgrammer Mar 10 '17 at 09:44
  • Could you please give me short example by codes. How can i correct it ? – Navneet Tyagi Mar 10 '17 at 10:19
  • I can see that exact problem is here st=cs.table.getRowCount(); in class amt_deposit. in st i am not able to get the value. I have also used st=cs.table.getSelectedRow(); but it is also not working. – Navneet Tyagi Mar 10 '17 at 10:22
  • 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 3) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Mar 10 '17 at 10:31

0 Answers0