Results 1 to 9 of 9
- 12-06-2010, 05:18 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
how to choose the field where last JTextField
i have a problem in first Java application ; there is an image for my calculator
how can i set the number on the field i choose when i press on it by using mouse , is there an option to get the right field .....i just build zero buttons for test
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class calc extends JFrame implements ActionListener {
JTextField num1 = new JTextField(" 1 ",3);
JTextField num3 = new JTextField(" 1 ",3);
JTextField num2 = new JTextField(" 1 ",3);
JTextField num4 = new JTextField(" 1 ",3);
JTextField num = new JTextField(" 1 ");
JLabel answer = new JLabel("0.22");
JLabel symbol = new JLabel("+");
JLabel equals = new JLabel("=");
JLabel simplification = new JLabel(" ");
JLabel status = new JLabel(" ");
JButton addButton = new JButton(" + ");
JButton subButton = new JButton(" - ");
JButton multButton = new JButton(" X ");
JButton divButton = new JButton(" ÷ ");
JButton zero = new JButton(" 0 ");
JButton one = new JButton(" 1 ");
JButton two = new JButton(" 2 ");
JButton three = new JButton(" 3 ");
JButton four = new JButton(" 4 ");
JButton five = new JButton(" 5 ");
JButton sex = new JButton(" 6 ");
JButton seven = new JButton(" 7 ");
JButton eight = new JButton(" 8 ");
JButton nine = new JButton(" 9 ");
public calc()
{
JPanel fields = new JPanel(new FlowLayout());
fields.add(num1);
fields.add(num4);
fields.add(symbol);
fields.add(num2);
fields.add(num3);
fields.add(equals);
fields.add(answer);
fields.add(status);
fields.add(simplification);
JPanel buttons = new JPanel(new FlowLayout());
buttons.setLayout(new GridLayout(0, 4, 5, 5));// array of buttons
// (row,cols,v-space,h_space);
buttons.add(divButton);
buttons.add(one);
buttons.add(two);
buttons.add(three);
buttons.add(multButton);
buttons.add(four);
buttons.add(five);
buttons.add(sex);
buttons.add(subButton);
buttons.add(seven);
buttons.add(eight);
buttons.add(nine);
//num1.setBounds(23,344, 200,28);//hadool men el calcolator el tanyeh
//num1.setBackground(Color.black);
//num1.setForeground(Color.green);
JPanel numbers = new JPanel(new GridLayout(2,1,2,2)); //FlowLayout
numbers.add(addButton);
numbers.add(zero);
// num1.setPreferredSize(new Dimension(50, 50));
getContentPane().setLayout(new BorderLayout(2,2));
getContentPane().add("North", fields);
getContentPane().add("Center", buttons);
getContentPane().add("South", numbers);
zero.addActionListener(this);
addButton.addActionListener(this);
subButton.addActionListener(this);
multButton.addActionListener(this);
divButton.addActionListener(this);
num.requestFocus(true);
num.requestFocus();
pack();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
System.gc();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource().equals(zero))
{
}
}
public static void main(String args[]) {
new calc();
}
}
-
I suppose you want to add a FocusListener to your JTextFields and use it to keep track of which field was last one to have focus. This can be done in the focusGained method where you set a reference to the current JTextField (obtained by calling getSource on the FocusEvent object) to a class variable that may hold a reference to this field, but I'm not quite sure of the overall gist of your GUI.
Much luck!Last edited by Fubarable; 12-06-2010 at 05:45 PM.
- 12-06-2010, 05:45 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
thank you but how
i dont know how to add FocusListener please help me
-
The tutorials can show you. Please have a look and give it a try.
How to Write a Focus Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
Luck!
- 12-06-2010, 05:56 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
thank you
thank you man you are great i will try
- 12-06-2010, 07:06 PM #6
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
am trying but there is no response
this the code i try to add many thing but nothing success help me
Java Code:package sallal.com.java.array; import java.awt.*; import java.awt.event.*; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; public class calc extends JFrame implements ActionListener ,FocusListener { JTextArea display = new JTextArea(); JTextField num1 = new JTextField(" 1 ",3); JTextField num3 = new JTextField(" 1 ",3); JTextField num2 = new JTextField(" 1 ",3); JTextField num4 = new JTextField(" 1 ",3); JTextField num = new JTextField(" 1 "); JLabel answer = new JLabel("0.22"); JLabel symbol = new JLabel("+"); JLabel equals = new JLabel("="); JLabel simplification = new JLabel(" "); JLabel status = new JLabel(" "); JButton addButton = new JButton(" + "); JButton subButton = new JButton(" - "); JButton multButton = new JButton(" X "); JButton divButton = new JButton(" ÷ "); JButton zero = new JButton(" 0 "); JButton one = new JButton(" 1 "); JButton two = new JButton(" 2 "); JButton three = new JButton(" 3 "); JButton four = new JButton(" 4 "); JButton five = new JButton(" 5 "); JButton sex = new JButton(" 6 "); JButton seven = new JButton(" 7 "); JButton eight = new JButton(" 8 "); JButton nine = new JButton(" 9 "); public calc() { JPanel fields = new JPanel(new FlowLayout()); fields.add(num1); fields.add(num4); fields.add(symbol); fields.add(num2); fields.add(num3); fields.add(equals); fields.add(answer); fields.add(status); fields.add(simplification); JPanel buttons = new JPanel(new FlowLayout()); buttons.setLayout(new GridLayout(0, 4, 5, 5));// array of buttons // (row,cols,v-space,h_space); buttons.add(divButton); buttons.add(one); buttons.add(two); buttons.add(three); buttons.add(multButton); buttons.add(four); buttons.add(five); buttons.add(sex); buttons.add(subButton); buttons.add(seven); buttons.add(eight); buttons.add(nine); //num1.setBounds(23,344, 200,28);//hadool men el calcolator el tanyeh //num1.setBackground(Color.black); //num1.setForeground(Color.green); JPanel numbers = new JPanel(new GridLayout(2,1,2,2)); //FlowLayout numbers.add(addButton); numbers.add(zero); getContentPane().setLayout(new BorderLayout(2,2)); getContentPane().add("North", fields); getContentPane().add("Center", buttons); getContentPane().add("South", numbers); zero.addActionListener(this); addButton.addActionListener(this); subButton.addActionListener(this); multButton.addActionListener(this); divButton.addActionListener(this); num1.addFocusListener(this); num2.addFocusListener(this); num3.addFocusListener(this); num4.addFocusListener(this); display.setEditable(false); display.setRequestFocusEnabled(false); display.addFocusListener(this); pack(); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); System.gc(); } public void actionPerformed(ActionEvent e) { if (e.getSource().equals(zero)) { //num=JTextField.mouseClicked(); //num2.setText(" 0 "); // num.setText(" 78 "); } } public void focusGained(FocusEvent e) { } public void focusLost(FocusEvent e) { ; } public static void main(String args[]) { new calc(); } }Last edited by Fubarable; 12-06-2010 at 07:09 PM. Reason: Moderator Edit: Code tags added
-
Again, I'm not 100% sure I understand what you're trying to do, but if it is to place button press text into the JTextField with the most recent focus, then give your class a JTextField variable that will hold a reference to the last focused JTextField:
And then give the class a FocusListener, and in the focusGained method set the variable above to refer to the JTextField that just gained focus. I prefer to use a private inner class for this:Java Code:public class FocusTest { //...... private JTextField fieldWithLastFocus = null;
Then create an instance of the focus listener and add it to your JTextFields.Java Code:private class MyFocusListener extends FocusAdapter { @Override public void focusGained(FocusEvent e) { fieldWithLastFocus = (JTextField) e.getSource(); } }
For example:
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class FocusTest { public static final String[][] BUTTON_STRS = { {"7", "8", "9", "/"}, {"4", "5", "6", "*"}, {"1", "2", "3", "-"}, {"0", ".", "+", "="}}; private JPanel mainPanel = new JPanel(); private JTextField[] textFields = new JTextField[4]; private JButton[][] buttons = new JButton[BUTTON_STRS.length][BUTTON_STRS[0].length]; private JTextField fieldWithLastFocus = null; public FocusTest() { JPanel topPanel = new JPanel(new GridLayout(1, 0, 5, 0)); FocusListener myFocusListener = new MyFocusListener(); for (int i = 0; i < textFields.length; i++) { textFields[i] = new JTextField(5); textFields[i].addFocusListener(myFocusListener); topPanel.add(textFields[i]); } JPanel buttonPanel = new JPanel(new GridLayout(4, 4, 5, 5)); ActionListener myBtnListener = new MyButtonListener(); for (int row = 0; row < buttons.length; row++) { for (int col = 0; col < buttons[row].length; col++) { buttons[row][col] = new JButton(BUTTON_STRS[row][col]); buttons[row][col].addActionListener(myBtnListener); buttons[row][col].setFocusable(false); buttonPanel.add(buttons[row][col]); } } mainPanel.setLayout(new BorderLayout(5, 5)); mainPanel.add(topPanel, BorderLayout.NORTH); mainPanel.add(buttonPanel, BorderLayout.CENTER); int eb = 5; mainPanel.setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb)); } public JPanel getMainPanel() { return mainPanel; } private class MyFocusListener extends FocusAdapter { @Override public void focusGained(FocusEvent e) { fieldWithLastFocus = (JTextField) e.getSource(); } } private class MyButtonListener implements ActionListener { public void actionPerformed(ActionEvent ae) { String actCommand = ae.getActionCommand(); // if a number or . key pressed if ("1234567890.".contains(actCommand)) { // if a text field has had focus ever if (fieldWithLastFocus != null) { // get jtextfield's string String text = fieldWithLastFocus.getText(); // append the button's text to it: text += actCommand; // and insert it into the JTextField fieldWithLastFocus.setText(text); } } } } private static void createAndShowUI() { JFrame frame = new JFrame("FocusTest"); frame.getContentPane().add(new FocusTest().getMainPanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
Also, please read my first signature link on how to use code tags as they make your posted code a lot more readable. I've added them to your post above so you can see the difference they make.
Luck.
- 12-07-2010, 06:15 PM #8
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
thank you so so much
thats work fine no word can describe how can i thank you
-
Glad it helped. Good luck with your project.
Similar Threads
-
front end display of field description when clicking the field name
By neils in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 10-29-2010, 11:47 AM -
Help with bcc field and cc field in java code
By eel in forum NetworkingReplies: 1Last Post: 10-25-2010, 12:20 PM -
how from an Access Currency field I populate a hidden field
By lse123 in forum Java ServletReplies: 4Last Post: 01-17-2010, 11:13 PM -
what to choose???(awt or swing)
By moshe12007 in forum AWT / SwingReplies: 11Last Post: 08-14-2009, 11:10 AM -
how to access jTextField of one JFrame1 from JFrame2 & Modify JTextField contents
By sumit1mca in forum AWT / SwingReplies: 1Last Post: 01-30-2009, 06:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks