-1

So I'm missing something here, but I can't get my setAttributes method to update the agility text field like it's supposed to. The class that calls the method and the call works fine passing in the needed values as seen by the system.out.println(agility). However in just the line below I try to set the agilityAmount txtfield and it doesn't display. What am I doing wrong?

    class GraphicalInterface extends JFrame implements Runnable, KeyListener
{


    GraphicalInterface() {
    }
    JFrame mainwindow;

    //declare panels
    JPanel panel1=new JPanel();
    JPanel panel2=new JPanel();
    JPanel invictusWelcome=new JPanel();
    JPanel gameStartQuit=new JPanel();
    JPanel panel3=new JPanel();
    JPanel panel4=new JPanel();
    JPanel panel5=new JPanel();
    JPanel panel6=new JPanel();
    JPanel panel7=new JPanel(new GridLayout(7, 1));
    JPanel mappanel=new JPanel();
    JPanel attribpanel=new JPanel(new GridLayout(12,0));

    //declare buttons
    JButton startGame;
    JButton quitButton;
    JButton mapButton;
    JButton townButton;
    JButton castleButton;
    JButton forestButton;
    JButton exitButton;
    JButton fighterSelect;
    JButton hybridSelect;
    JButton magicSelect;
    JButton characterConfirm;

    //declare labels
    JLabel agility=new JLabel("Agility:");
    JLabel health=new JLabel("Health: ");
    JLabel mana=new JLabel("Mana: ");
    JLabel strength=new JLabel("Strength: ");
    JLabel intelligence=new JLabel("Intelligence: ");
    JLabel charNameLabel=new JLabel("Name: ");
    JTextField charNameLabelField=new JTextField();
    JTextField healthAmount=new JTextField();
    JTextField manaAmount=new JTextField();
    JTextField currentHealthAmount=new JTextField();
    JTextField currentManaAmount=new JTextField();
    JTextField strengthAmount=new JTextField();
    JTextField agilityAmount=new JTextField();
    JTextField intelligenceAmount=new JTextField();


    JTextField description;

    //declares attribute values
    String name="";
    int agi=0;
    int str=0;
    int intel=0;
    int currentHealth=0;
    int maxHealth=0;
    int currentMana=0;
    int maxMana=0;
    int location=0;


    //Initialize Listener Object
    Listener aListener= new Listener();
    Location locationObj= new Location();


    //images
    private Image map;
    private Image attribBackground;
    private Image charMagic;
    private Image charHybrid;
    private Image charFighter;
    private Image town;
    private Image cave;
    private Image forest;
    private Image castle;
    private Image characterIcon;

    protected void initializeWindows()
    {
        mainwindow=new JFrame("Invictus Maneo");
        mainwindow.setVisible(true);
        mainwindow.setPreferredSize(new Dimension(900, 700));
        mainwindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainwindow.setResizable(false);

        panel1.setVisible(true);
        panel1.setPreferredSize(new Dimension(800, 550));
        mainwindow.getContentPane().add(panel1, BorderLayout.NORTH);

        panel2.setVisible(true);
        panel2.setPreferredSize(new Dimension(800, 100));

        //Initializes buttons
        startGame=new JButton("Begin Adventure");
        panel2.add(startGame, BorderLayout.SOUTH);
        startGame.addActionListener(aListener);

        quitButton=new JButton("Quit");
        panel2.add(quitButton, BorderLayout.SOUTH);
        quitButton.addActionListener(aListener);

        mapButton=new JButton("Map");
        mapButton.addActionListener(aListener);

        townButton=new JButton("Town");
        townButton.addActionListener(aListener);

        castleButton=new JButton("Castle");
        castleButton.addActionListener(aListener);

        exitButton=new JButton("Exit Map");
        exitButton.addActionListener(aListener);

        forestButton=new JButton("" +"Forest");
        forestButton.addActionListener(aListener);

        fighterSelect=new JButton("Fighter");
        fighterSelect.addActionListener(aListener);
        hybridSelect=new JButton("Hybrid");
        hybridSelect.addActionListener(aListener);
        magicSelect=new JButton("Magic");
        magicSelect.addActionListener(aListener);

        //initialize attribute panel
        //attribpanel=new JPanel(new GridLayout(12,0));
        attribpanel.setVisible(false);
        attribpanel.setPreferredSize(new Dimension(200, 500));
        attribpanel.setBackground(null);


        attribpanel.add(charNameLabel);
        attribpanel.add(charNameLabelField);
        attribpanel.add(health);
        attribpanel.add(healthAmount);
        attribpanel.add(mana);
        attribpanel.add(manaAmount);
        attribpanel.add(strength);
        attribpanel.add(strengthAmount);
        attribpanel.add(agility);
        attribpanel.add(agilityAmount);
        attribpanel.add(intelligence);
        attribpanel.add(intelligenceAmount);

        mainwindow.getContentPane().add(panel2, BorderLayout.SOUTH);

        panel3.setVisible(true);
        panel3.setPreferredSize(new Dimension(200, 600));

        panel4.setVisible(true);
        panel4.setPreferredSize(new Dimension(650, 500));


        panel5.setVisible(true);
        panel5.setPreferredSize(new Dimension(600, 600));
        panel5.setBackground(getBackground());

        panel6.setVisible(false);
        panel6.setPreferredSize(new Dimension(500, 500));

        panel7.setVisible(false);
        panel7.setPreferredSize(new Dimension(100, 200));


        //Adds sub panels to panel 1
        panel1.add(panel3, BorderLayout.NORTH);
        panel1.add(panel4, BorderLayout.NORTH);

        //panel5.setBorder(BorderFactory.createLineBorder (Color.black, 2));
        //panel6.setBorder(BorderFactory.createLineBorder (Color.black, 2));
        panel2.setBorder(BorderFactory.createLineBorder (Color.black, 2));
        attribpanel.setBorder(BorderFactory.createLineBorder (Color.black, 2));
        //panel4.setBorder(BorderFactory.createLineBorder (Color.black, 2));

        panel4.add(panel5, BorderLayout.NORTH);

        loadPictures();

        BufferedImage myPicture;
        try {
            myPicture = ImageIO.read(new File("C:\\test\\GameTitle.jpg"));
            JLabel gameTitle = new JLabel(new ImageIcon( myPicture ));
            panel5.add( gameTitle );
            panel5.repaint();
            panel1.repaint();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        BufferedImage map;
        try {
            map = ImageIO.read(new File("C:\\test\\map.jpg"));
            JLabel mapLabel = new JLabel(new ImageIcon( map ));
            panel6.add( mapLabel );
            panel1.repaint();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        mainwindow.pack();


        }   

    void setAttributePanel(int s, int a, int i, int h, int m)
    {

         agi=a;
         str=s;
         intel=i;
         currentHealth=h;
         maxHealth=h;
         currentMana=m;
         maxMana=m;

         System.out.println(agi+": agility");
         agilityAmount.setText(agi+"");
         attribpanel.repaint();
         panel3.repaint();
         panel1.repaint();


    }




private class Listener implements ActionListener
 {

    public void actionPerformed(ActionEvent a) 
    {
        if (a.getSource()==quitButton)
        {
            System.exit(0);
        }

        else if (a.getSource()==startGame)
        {

            startGame.setVisible(false);
            panel2.add(mapButton);
            mapButton.setVisible(true);
            panel2.repaint();
            panel5.setVisible(false);
            panel3.add(attribpanel);
            attribpanel.setVisible(true);
            Character charChosenObj= new Character();
            charChosenObj.characterCreation();
            attribpanel.repaint();
            panel3.repaint();
        }

        else if (a.getSource()==mapButton)
        {
            System.out.println("Where are you going?");
            panel4.add(panel6, BorderLayout.CENTER);
            panel4.add(panel7);
            panel7.add(townButton);
            panel7.add(forestButton);
            panel7.add(castleButton);
            panel7.add(exitButton);
            panel5.setVisible(false);
            panel6.setVisible(true);
            panel7.setVisible(true);
            panel4.repaint();


        }

        else if (a.getSource()==exitButton)
        {
            panel5.setVisible(true);
            panel6.setVisible(false);
            panel7.setVisible(false);

        }

        else if (a.getSource()==townButton)
        {
            System.out.println("Pick up some milk will ya?");
            locationObj.setLocation(1);

        }

        else if (a.getSource()==forestButton)
        {
            System.out.println("Watch for falling trees.");
            locationObj.setLocation(2);
            Combat combatObj=new Combat();
            combatObj.startCombat();

        }

        else if (a.getSource()==castleButton)
        {
            System.out.println("Are you sure... it's kinda dark in there?");
            locationObj.setLocation(3);


        }

    }

 }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • If you do a getText, does it give the right value? – Thomas Mar 16 '12 at 19:42
  • 1
    Where are you calling `setAttributePanel` ?That is the method that is doing `setText` and you don't seem to call it from anywhere? – ring bearer Mar 16 '12 at 20:13
  • I'm with @ringbearer -- you're not showing enough information to make this question solvable. For all we know you could have more than one GraphicalInterface, one displayed, and the other non-displayed one getting its text set. – Hovercraft Full Of Eels Mar 16 '12 at 22:39
  • @ringbearer The setAttributePanel() method is called from my Character class after they have made their selection on what class(fighter, hybrid, magician) they want. Then it assigns all of the attributes to the right values. After which the method is called and passed all the attributes. I have searched my code for a possible duplicate of the gui but I'm not finding any. I would post my whole program, but that would involve at least 5 classes. **EDIT** Now I do create a new object by GraphicalInterface guiObject= new GraphicalInterface() in each of my other classes that needs it. – James Whorley Mar 18 '12 at 01:52
  • I also read in other problems like mine about passing the object through the classes constructor using "this" but I just couldn't quite understand what they were saying to use. I don't know if that's something that could possibly be my issue, by not passing my gui to the other classes to use. – James Whorley Mar 18 '12 at 02:25
  • Wow! Over 300 lines of uncompilable code. For better help sooner, post an [SSCCE](http://sscce.org/). BTW `Character charChosenObj= new Character();` it is a ***very bad idea*** to be making custom classes with the same name as ones in the J2SE, especially if they are in `java.lang`! – Andrew Thompson Mar 18 '12 at 04:32
  • **Solution**: So my problem was that I was trying to alter a different GUI than what was being displayed. This was easily remedied by passing this GUI to each class' constructor EX: Character charChosenObj= new Character(GraphicalInterface.this);. Thank you all for the helpful tips. – James Whorley Mar 22 '12 at 18:44
  • @mtraut will look into the GridBagLayout a little deeper to see if I can make my program more efficient that way =D. – James Whorley Mar 22 '12 at 18:44
  • @Thompson I do apologize about my bad posting, will change that in the future. Also I will change that class name. I totally spaced it when I created that class =D Thank you for showing me that. I would have hated for something not to work because of that misnamed class. – James Whorley Mar 22 '12 at 18:46

2 Answers2

1

mainwindow.setVisible(true); must be last line in the GUI comstructor

mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

If you have a component laid out and visible and change the preferred size afterwards (by adding text, for example) you must force a new layout cycle, fore example by "invalidate " the component or "validate" the container.

There are other issues, either

  • Why is your GraphicalInterface a JFrame when you dont inend to use it as such
  • When adding components in the listener, you should be prepared to remove them somewhere else or handle the visibility only by "setVisible"
  • Maybe better use some layout that resizes its children to the size available, like GridBagLayout
  • As mentioned by mKorbel you should "setVisible" only when your container is completely configured. You will improve the users experience :-)
mtraut
  • 4,720
  • 3
  • 24
  • 33