Results 1 to 2 of 2
  1. #1
    2o2
    2o2 is offline Member
    Join Date
    Sep 2008
    Posts
    31
    Rep Power
    0

    Default Simple Question Making a Variable Accessable throught the code

    at the bottom in the public void run, i declare a jtextfield called screen. how would i then allow this variable to be used in my actionlistner class where i am trying to set the text using screen.setText?

    Java Code:
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JComponent;
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Dimension;
    import javax.swing.*; 
    import java.awt.*; 
    import java.awt.event.*; 
    public class Simplebuttongrid
    {
      private JPanel mainPanel = new JPanel(); // panel holds the button grid
      String[][] buttonStrings = // use these Strings to create a JButton grid
      {
          {"7", "8", "9", "/"},
          {"4", "5", "6", "*"},
          {"1", "2", "3", "-"},
          {"0", ".", "=", "+"},
      };
    
      public Simplebuttongrid()
      {
        int rowLength = buttonStrings.length;
        int colLength = buttonStrings[0].length;
        mainPanel.setLayout(new GridLayout(rowLength, colLength));
        ButtonListener buttonListener = new ButtonListener(); // create our ActionListener
        for (int row = 0; row < rowLength; row++)
        {
          for (int col = 0; col < colLength; col++)
          {
            // for each String in the array, create a button that holds the string
            String buttonString = buttonStrings[row][col];
            JButton button = new JButton(buttonString); // this button here
            button.addActionListener(buttonListener); // add the listener
            mainPanel.add(button); // and add it to the panel
          }
        }
      }
    
      public JComponent getComponent()
      {
        return mainPanel;
      }
      //public JComponent getComponent2()
      //{
      //	return screenPanel;
     // }
      
      // here's the button listener class that of course implements action listener
      private class ButtonListener implements ActionListener
      {
        public void actionPerformed(ActionEvent e)
        {
          // we can easily extract the String held by the button pressed 
          String actionCommand = e.getActionCommand(); // here's the string
          if (actionCommand == "5"){
          	 screen.setText("grr");  // heres where i want to be able to access the jtextfield screen
          }
         
        }
      }
    
      
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
                JFrame frame = new JFrame("Calculator"); 
       	frame.setLayout(new BorderLayout());
        //frame.getContentPane().add(new Simplebuttongrid().getComponent2());
        frame.getContentPane().add(new Simplebuttongrid().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        //heres where i set the screen
        JTextField screen = new JTextField(13);
        JPanel screenPanel = new JPanel(); //panel to hold screen
        screenPanel.setLayout(new GridLayout(1,1));
        screenPanel.add(screen);
        frame.add(screenPanel, BorderLayout.NORTH);
    
          }
        });
      }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    JTextField screen
    Put this part outside of a method as a class variable.

Similar Threads

  1. Simple question about access
    By tortelini in forum New To Java
    Replies: 6
    Last Post: 09-06-2008, 06:41 PM
  2. Simple Method Question
    By Froz3n777 in forum New To Java
    Replies: 2
    Last Post: 02-13-2008, 03:39 AM
  3. code for making a java swing program a demo verson
    By fakhruddin in forum AWT / Swing
    Replies: 1
    Last Post: 11-27-2007, 09:54 PM
  4. Probably a really simple question...
    By ibanez270dx in forum New To Java
    Replies: 0
    Last Post: 11-16-2007, 02:27 AM
  5. Simple question of JTable
    By carl in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 08:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •