Hello, I'm having an issue with set and get method. When i clicked on a button, i set index as 1 however when i use the get method to get the index, the index is returned as 0. I want to call a method if the index is 1. here is my code:

//class1
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/**
 *
 * @author User
 */
public class Buildings {
    private Image img;
    private String s;
    private int b_index;
    private BufferedImage image;

    private String s_shopping="Shopping Mall";




    public Buildings(){

        try{

            img= ImageIO.read(new File("C:/Users/User/Desktop/R0303/src/Designer/simulation.jpg"));

        }
        catch(IOException e){
        System.out.println("Image not found");
    }



    }

    public void PaintBuilding(Graphics2D g2d){

        g2d.drawImage(img, 175,200,null);
        g2d.drawString(s_shopping, 175+6, 200+6);
    }


    public String getString(){
        return s;
    }

    public void setString(String s){
        this.s=s;
    }

    public void setBIndex(int index){
        this.b_index=index;
    }

    public int getBIndex(){
        return b_index;
    }


}

    //class2

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

/**
 *
 * @author User
 */
public class BuildingOptions  extends JFrame{

    JRadioButton rb1;
    JRadioButton rb2;
    ButtonGroup bgroup;
    private int build_index;
    JPanel panelRbutton;

    Buildings build= new Buildings();

    public BuildingOptions(){

        super("Choose a building");
        build.setBIndex(build_index);
        rb1= new JRadioButton("Add a School");
        rb2=new JRadioButton("Add a Shopping Mall");
        //rb3=new JRadioButton()

        bgroup= new ButtonGroup();
        bgroup.add(rb1);
        bgroup.add(rb2);




        rb1.addItemListener(new ButtonListener());
        rb2.addItemListener(new ButtonListener());


        panelRbutton= new JPanel();

        panelRbutton.add(rb1);
        panelRbutton.add(rb2);

        this.add(panelRbutton);

        this.setSize(250, 300);
        this.setBackground(Color.LIGHT_GRAY);
        this.setVisible(true);

    }

    public class ButtonListener implements ItemListener{
        public void itemStateChanged(ItemEvent e){

            if(e.getSource()==rb1){
                System.out.println("Hello world");
                build_index=1;
                build.setBIndex(build_index);
                //repaint();
                System.out.println("Index here is:"+build.getBIndex());
            }

        }
    }

public static void Main(String [] args){

    BuildingOptions frame1= new BuildingOptions();
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setVisible(true);

}


}

//class3
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *
 * @author User
 */
public class MainDrawing extends  JPanel {
    int id=2;
    int index;
    Buildings b= new Buildings();
    boolean buildingS=false;

    JButton b1;
    BuildingOptions bOpt;

    public MainDrawing(){
        //this.index=index;
        this.setBackground(Color.white);
        this.setSize(300,150);
       // b.setBIndex(index);
        b1=new JButton("Add Buildings");
        b1.setSize(25, 50);
        b1.addActionListener(new ButtonListener());
        add(b1);

    }

    public void paint(Graphics g){
        super.paint(g); 
        Graphics2D g2d=(Graphics2D)g;
        System.out.println("Index is:"+b.getBIndex());
        if(b.getBIndex()==1)

            //if(buildingS==true)
            b.PaintBuilding(g2d);



    }

    public class ButtonListener implements ActionListener{
         public void actionPerformed(ActionEvent e) {
            if(e.getSource()==b1)

                bOpt= new BuildingOptions();
                bOpt.setVisible(true);
                bOpt.setSize(250, 250);

                System.out.println("Index in button listener in class buttonListener:"+b.getBIndex());
                repaint();

                /*index=1;
                // buildingS=true;
                b.setBIndex(index);
                repaint();
                System.out.println("Index:"+b.getBIndex());*/

        }
    }

    public static void main(String args[]){

        MainDrawing md= new MainDrawing();
        JFrame f=new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //f.setBackground(Color.yellow);
        f.setContentPane(md);
        f.setVisible(true);
        f.setSize(350,350);
        f.setVisible(true);

    }




}

Recommended Answers

All 2 Replies

not sure about what the exact code is, but it seems to me you are using the indices of seperate classes or instances thereof. also: your code seems way more complex then it should be.

Like C/C++, Java uses 0-based indexes. Not interested in reading through your 200 lines of code. If you set a member to index 1, it is actually the second member of the array, NOT the first.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.