Results 1 to 2 of 2
- 10-02-2008, 03:10 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 31
- Rep Power
- 0
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); } }); } }
- 10-02-2008, 04:06 AM #2
Similar Threads
-
Simple question about access
By tortelini in forum New To JavaReplies: 6Last Post: 09-06-2008, 06:41 PM -
Simple Method Question
By Froz3n777 in forum New To JavaReplies: 2Last Post: 02-13-2008, 03:39 AM -
code for making a java swing program a demo verson
By fakhruddin in forum AWT / SwingReplies: 1Last Post: 11-27-2007, 09:54 PM -
Probably a really simple question...
By ibanez270dx in forum New To JavaReplies: 0Last Post: 11-16-2007, 02:27 AM -
Simple question of JTable
By carl in forum AWT / SwingReplies: 1Last Post: 08-07-2007, 08:07 AM
Bookmarks