Reputation: 45
Hi I'm working on a program for my java class and am running into some problems. The assignment was to edit the professor's existing code to make it so that when a button is clicked in the GUI it takes the value of the current JTextField and adds it to the value of the next field, and puts that value in that field. I have managed to edit his to the point of having the correct GUI layout, and have attempted to get the computational side to work but am at a loss of ideas. I need to find out how to display the values in the JTextFields immediately at launch, and how to go about adding the contents. ANy help is greatly appreciated!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Graphics2D;
public class U27
{
public static void main(String[] args)
{
MiscFocusFrame myframe = new MiscFocusFrame();
myframe.setVisible(true);
}
}
class MiscFocusFrame extends JFrame
{
private JTextField myField;
private MiscFocusPanel myPanel;
private final int FRAMEW = 900;
private final int FRAMEH = 200;
public MiscFocusFrame()
{
setTitle("U27");
setSize(FRAMEW, FRAMEH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myPanel = new MiscFocusPanel();
Container contentPane = getContentPane();
contentPane.add(myPanel, "Center");
}
}
class MiscFocusPanel extends JPanel
{
private MiscFocusRegister register1;
private MiscFocusRegister register2;
private MiscFocusRegister register3;
private MiscFocusRegister register4;
public MiscFocusPanel()
{
register1 = new MiscFocusRegister(4);
register2 = new MiscFocusRegister(4);
register3 = new MiscFocusRegister(4);
register4 = new MiscFocusRegister(4);
register1.setRegisterToPassFocusTo(register2);
register2.setRegisterToPassFocusTo(register3);
register3.setRegisterToPassFocusTo(register4);
register4.setRegisterToPassFocusTo(register1);
setLayout(new GridLayout(2, 6));
JLabel labelA = new JLabel("Cup 1", JLabel.LEFT);
JPanel labelPanelA = new JPanel();
labelPanelA.add(labelA);
add(labelPanelA);
JPanel regPanelA = new JPanel();
regPanelA.add(register1.getMyField());
add(regPanelA);
JButton myButton1 = new JButton("Add To Next Cup");
MiscButtonListener myButtonListener1 = new MiscButtonListener();
myButton1.addActionListener(myButtonListener1);
JPanel buttonPanel1 = new JPanel();
buttonPanel1.add(myButton1);
add(buttonPanel1);
JLabel labelB = new JLabel("Cup 2", JLabel.LEFT);
JPanel labelPanelB = new JPanel();
labelPanelB.add(labelB);
add(labelPanelB);
JPanel regPanelB = new JPanel();
regPanelB.add(register2.getMyField());
add(regPanelB);
JButton myButton2 = new JButton("Add To Next Cup");
MiscButtonListener myButtonListener2 = new MiscButtonListener();
myButton2.addActionListener(myButtonListener2);
JPanel buttonPanel2 = new JPanel();
buttonPanel2.add(myButton2);
add(buttonPanel2);
JLabel labelC = new JLabel("Cup 3", JLabel.LEFT);
JPanel labelPanelC = new JPanel();
labelPanelC.add(labelC);
add(labelPanelC);
JPanel regPanelC = new JPanel();
regPanelC.add(register3.getMyField());
add(regPanelC);
JButton myButton3 = new JButton("Add To Next Cup");
MiscButtonListener myButtonListener3 = new MiscButtonListener();
myButton3.addActionListener(myButtonListener3);
JPanel buttonPanel3 = new JPanel();
buttonPanel3.add(myButton3);
add(buttonPanel3);
JLabel labelD = new JLabel("Cup 4", JLabel.LEFT);
JPanel labelPanelD = new JPanel();
labelPanelD.add(labelD);
add(labelPanelD);
JPanel regPanelD = new JPanel();
regPanelD.add(register4.getMyField());
add(regPanelD);
JButton myButton4 = new JButton("Add To Next Cup");
MiscButtonListener myButtonListener4 = new MiscButtonListener();
myButton4.addActionListener(myButtonListener4);
JPanel buttonPanel4 = new JPanel();
buttonPanel4.add(myButton4);
add(buttonPanel4);
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
super.paintComponent(g2);
}
private class MiscButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
register1.swapRegisterContents(register2);
}
}
}
/*
This class contains a new method, setRegisterToPassFocusTo(), and in
the listener focus is passed to that register after the new value for
the text field is taken in.
*/
class MiscFocusRegister
{
private MiscFocusByte registerValue;
private JTextField myField;
private MiscFocusRegister registerToPassFocusTo;
public MiscFocusRegister()
{
}
public MiscFocusRegister(int stringIn)
{
registerValue = new MiscFocusByte(stringIn);
myField = new JTextField(stringIn);
TextFieldListener myListener = new TextFieldListener();
myField.addActionListener(myListener);
}
public void setRegisterValue(MiscFocusByte byteIn)
{
registerValue = byteIn;
}
public MiscFocusByte getRegisterValue()
{
return registerValue;
}
public void setMyField(JTextField fieldIn)
{
myField = fieldIn;
}
public JTextField getMyField()
{
return myField;
}
public void setRegisterToPassFocusTo(MiscFocusRegister registerIn)
{
registerToPassFocusTo = registerIn;
}
public void swapRegisterContents(MiscFocusRegister source)
{
MiscFocusByte tempValue;
tempValue = source.getRegisterValue();
source.setRegisterValue(this.getRegisterValue());
source.myField.setText(source.getRegisterValue().getStringFromByte());
this.setRegisterValue(tempValue);
this.myField.setText(this.getRegisterValue().getStringFromByte());
}
private class TextFieldListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
String inputString = myField.getText();
registerValue.setByteToThisString(inputString);
registerToPassFocusTo.getMyField().requestFocusInWindow();
}
}
}
class MiscFocusByte
{
public static int bitsinbyte = 8;
private static final String junk = "00000000";
private char bytearray[] = new char[bitsinbyte];
public String astring;
public MiscFocusByte()
{
junk.getChars(0, bitsinbyte, bytearray, 0);
}
public MiscFocusByte(int stringIn)
{
//junk.getChars(0, bitsinbyte, bytearray, 0);
//int stringlength = stringIn.length();
//if(stringlength > bitsinbyte)
// stringlength = bitsinbyte;
//stringIn.getChars(0, stringlength, bytearray, 0);
stringIn = stringIn + 4;
}
public void setByteToThisString(String astring)
{
//junk.getChars(0, bitsinbyte, bytearray, 0);
//int stringlength = astring.length();
//if(stringlength > bitsinbyte)
// stringlength = bitsinbyte;
//astring.getChars(0, stringlength, bytearray, 0);
astring = astring + 4;
}
public String getStringFromByte()
{
//return String.copyValueOf(bytearray);
return String.valueOf(astring);
}
}
Upvotes: 0
Views: 167
Reputation: 3
Try something like this..
String text=jTextField1.getText();
text+="1";
jTextField1.setText(text);
jTextField1.requestFocus();
Upvotes: 1