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

Java static main getting error when compile [duplicate]
Migration Jersey 2.22.1 to Jersey 2.22.2
hql - how to select conditional query in hibernate
How to create launcher script through gradle
finally { if (inputStream != null) { inputStream.close();
Change format of DateUtils.Format_ABBREV_RELATIVE
custom JMenuBar added to JFrame but does not display
How to update a database record using hibernate with no primary key
Setting Button text programmatically is automatically going to last Index
How to check Spatial Relations between Two Rectangle Geometries using Java
View packages of a Maven artifact
I get the following error when trying to set a wallpaper [duplicate]
Preorder traversal of ternary tree
Problems with Logger in JUnit test case (Eclipse)
Cannot get Apache Tomcat/8.5.4 working on Ubuntu 16.04
Stop notification manager - Android

Categories

HOME
testing
arduino-uno
proxy
netbeans
cookies
azure-data-factory
dot
razor
elm
jsrender
win32gui
cross-browser
flyway4
gnupg
metatrader4
leiningen
hex-editors
visual-studio-cordova
quartz-scheduler
clearcase-ucm
graphlab
solaris-10
reverse-proxy
kryo
dcevm
java-7
emgucv
shopware
microsoft-sync-framework
rst2pdf
greendao
windows-error-reporting
typo3-6.2.x
elasticsearch-net
log4js-node
objectanimator
quote
librato
language-concepts
bytecode-manipulation
objectlistview
quadratic-programming
netcdf4
acoustics
node-sass
scorm
jedis
dropbox-php
brightcove
knpmenubundle
mcafee
spring-mongodb
gnome-shell-extensions
watchconnectivity
firmata
spring-android
multiple-regression
disque
nessus
comobject
qdialog
browser-bugs
tableau-online
mono-embedding
truevault
modalpopup
knuth
contenttype
rebol3
reactfx
kgdb
datagridviewcolumn
meteor-velocity
pyhdf
ms-project-server-2010
cos
gridcontrol
trusted
parametric-equations
ticoredatasync
buildr
amazon-appstore
boost-filesystem
gnustep
django-tagging
xmemcached
appendto
disclosure
imac
nsviewanimation
perfect-hash
compiler-specific
temporal-database
zune
grid-system
dbisam
data-acquisition
wsdl.exe

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