java


Java Numbers not showing up in Text Field, I want to be read in as a String


I am new to Java. I wrote a similar app that puts text into a JTextField when I click a button, the text associated with the button would show up in the JTextField. Now I am doing it will numbers (attempting to read a String) but when I click the button, nothing is showing up. I know it is probably something small I am missing. If you can help I would really appreciate it! :)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleButtons_Wallace extends JPanel implements ActionListener {
private JButton button0;
private JButton button1;
private JButton button2;
private JButton button3;
private JTextField Textfield;
public SimpleButtons_Wallace() {
GridLayout experimentLayout = new GridLayout(0, 2);
JPanel MiniME = new JPanel();
button0 = new JButton("zero");
button0.addActionListener(this);
button1 = new JButton("1");
button1.addActionListener(this);
button2 = new JButton("2");
button2.addActionListener(this);
button3 = new JButton("3");
button3.addActionListener(this);
Textfield = new JTextField(10);
Textfield.addActionListener(this);
MiniME.setLayout(experimentLayout);
add(MiniME);
MiniME.add(button0);
MiniME.add(button1);
MiniME.add(button2);
MiniME.add(button3);
MiniME.add(Textfield);
}
public void actionPerformed(ActionEvent e) {
JButton b = (JButton) e.getSource();
Textfield.setText(b.getText());
}
}
import javax.swing.JFrame;
import javax.swing.*;
public class GuiMain_Wallace extends JFrame {
public GuiMain_Wallace() {
getContentPane().add(new SimpleButtons_Wallace());
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String args[]) {
GuiMain_Wallace aframe = new GuiMain_Wallace();
aframe.setSize(225, 230);
aframe.setVisible(true);
}
}
//Here are the two classes I attempted you use. Is there anything I can do to //make the JTextField to be populated.
but when I click the button, nothing is showing up.
The problem is that you are attempting to manage the size of the frame (you mad the frame size too small so the text of the text field is truncated). Don't do that. Use the pack() method so the components can be displayed at their preferred sizes:
//aframe.setSize(225, 230);
aframe.pack();
It is always a good idea to create a JTextField with a number. This number is used for the text field to determine its preferred size. In your case you specified 10, which means it will display a minimum of 10 characters before you need to scroll the text.
It's because you're limiting the Textfield with JTextField(10).
Remove the limit:
Textfield = new JTextField();

Related Links

Is it possible to make an array with dynamic memory using while loop without asking the size of memory from user?
Set a Value for JAXBElement<String>
what is the logic of the term 'object' in certain situations
Strange outOfBoundsException when iterating through lists
Error pages in Grails app hosted using IIS 7 / Tomcat connector
Java generics: Calling generic Method “…is not applicable for the arguments…”
Getting hWnd from current process
Move a value from one key to another in HashMap
InputMismatchException in Java
EventListener without a Component
How to create a json tree with one root node with couple of child nodes
How to write IOException handler
Accessing J2EE Session Beans in a GWT Service Implementation
How to handle urn:ietf:wg:oauth:2.0:oob redirect in Google Calendar API Authorization
How do you design a new graphic to add to a JFrame Form?
How can I use keyboard shortcuts to do a git commit and push from IntelliJ IDEA?

Categories

HOME
log4j
winforms
testng
amazon-ec2
image-processing
tinymce
stock
spring-cloud-stream
sql-server-2016
fingerprint
networkx
directx
cloudkit
export-to-csv
fortumo
ibm-odm
after-effects
reactcsstransitiongroup
reactive-cocoa
cx-freeze
mmap
centos6.5
url-scheme
internet-explorer-9
web-api-testing
wtx
windows-server-2000
c++-amp
quote
procdump
bcd
binary-data
contact-form
commit
bower-install
appfabric
vxworks
simplexml
gesture
neuroscience
unspecified
impersonation
lumberjack
squib
sfdc
isbn
password-encryption
sas-jmp
angular2-meteor
angular-resource
flickr-api
multipeer-connectivity
alphabet
togetherjs
cubes
heidisql
skobbler-maps
player
multiple-regression
bgp
php-internals
unity-networking
qtableview
execute
cartesian-product
ora-00900
django-unittest
pundit
security-testing
deis
unity5.2.3
inmobi
nsmutabledictionary
qpainter
knuth
android-imagebutton
mdt
code-access-security
onactivityresult
has-many-through
contenttype
navigationservice
mechanize-ruby
cloud-connect
mcts
mysqltuner
access-rights
dexterity
gridcontrol
html4
orchardcms-1.7
mqx
hamiltonian-cycle
libstdc++
transactionscope
pydot
rfc1123
propagation
sortable-tables
web-architecture
sector
ugc
document-conversion

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App