GUI 2

GUI 2

How can I modify this code? Please give me a GUI Example.
Thanks!!

View Answers

April 8, 2008 at 5:26 PM

This can be done using KeyEvent in the following way... written comments above the newly added code

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JOptionPane;

class GUIExample1 extends JFrame implements ActionListener
{
JLabel numberLabel;
JTextField numberTextField;
JButton displayButton;
public GUIExample1()
{
Container pane = getContentPane();
pane.setLayout(new FlowLayout());
numberLabel = new JLabel("Enter a number:");
numberTextField = new JTextField(4);

//calling the keyEvent
numberTextField.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent evt){
numberTextField_KeyTyped(evt);
}
});

displayButton = new JButton("Enter");
pane.add(numberLabel);
pane.add(numberTextField);
pane.add(displayButton);
displayButton.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("GUI Example");
pack();
show();
}
public void actionPerformed(ActionEvent event)
{
Object source = event.getSource();
if (source == displayButton)
{
double result = Integer.parseInt(numberTextField.getText());
JOptionPane.showMessageDialog(null, "you entered " + (int)(result), "Hello", JOptionPane.PLAIN_MESSAGE);
System.exit(0);
}
}


//newly added code
//KeyEvent function
public void numberTextField_KeyTyped(KeyEvent evt)
{
try{
char dotChar=evt.getKeyChar();
int dot=(int)dotChar;
char c = evt.getKeyChar(); //character object to hold the typed character
if (!(Character.isDigit(c)))
{
getToolkit().beep();
evt.consume();
JOptionPane.showMessageDialog(this,"characters are not allowed");
numberTextField.requestFocus(true);

}
}
catch(Exception e){
System.out.println("---error in validation---"+e);
}
}//end of the method

public static void main(String[] args)
{
new GUIExample1();
}
}

This will help you out..

April 8, 2008 at 9:00 PM

Brilliant! The keyEvent works, but the modified code extends the text field to type numbers in to more than 4. I'll study the code, but what will be the reason for this? (The question is for the answer above).

April 8, 2008 at 10:56 PM

I'm the one who began this question. I'm new to the programming world and like it alot. I was wondering where can I go to learn more about java and C++.

April 10, 2008 at 3:39 PM

Hi
You need not worry of where to go and learn about java..Jus search on google and try to find the books on java. Then select which you can be comfortable with and be sure that all the topics are covered..Then start practising the programs.Practice will definitely make you perfect


Thanks
pinky

April 10, 2008 at 3:39 PM

Hi
You need not worry of where to go and learn about java..Jus search on google and try to find the books on java. Then select which you can be comfortable with and be sure that all the topics are covered..Then start practising the programs.Practice will definitely make you perfect


Thanks
pinky









Related Tutorials/Questions & Answers:
GUI 2 - Java Beginners
GUI 2  How can I modify this code? Please give me a GUI Example.Thanks!!  This can be done using KeyEvent in the following way... written...;GUI Example");pack();show();}public void actionPerformed(ActionEvent event
GUI
GUI  How to GUI in Net-beans ... ??   Please visit the following link: http://www.roseindia.net/java/java-tips/background/30java_tools/netbeans.shtml
Advertisements
GUI
GUI  Write a GUI application for the WebBuy Company that allows a user to compose the three parts of a complete email message: the â??To:â??, â??Subject:â?? and â??Message:â?? text. The â??To:â??, and â??Subject:â?? Text areas
gui question
gui question  design a gui application for me and write its code in which the user enters a no. in a textfield and onn clicking the button the sum... the output should be 6(1+2+3
GUI framework
GUI framework  what do u mean by GUI framework
GUI component
GUI component  How can a GUI component handle its own events
Java GUI
Java GUI  1) Using Java GUI, create a rectangular box that changes color each time a user click a change color button. 2) Modify Question 1 to include a new button named insert image, that allow user to insert a bitmap image
Convert the code to GUI
GUI code  GUI code
GUI problem
GUI problem  Create a class called CDProgram and write a GUI program to compute the amount of a certificate of deposit on maturity. The sample data follows: Amount deposited: 80000.00 Years: 15 Interest Rate: 7.75 Hint
java gui
java gui   friends... good day.. i have doubt in java gui. ? i created 1 java gui application. That has two text fields jtext1,jtext2. case: user entered value in first textfield(jtext1) and pressed the enter key . the cursor
Convert the code to GUI
GUI Application example  GUI Application example
Convert the code to GUI
GUI Example  GUI Example code to learn
Struts 2 Ajax
to the Struts 2 framework. Ajax allows the developers to develop GUI like web... Struts 2 Ajax       In this section, we explain you Ajax based development in Struts 2. Struts 2 provides built
Version of gj-gui>gj-gui dependency
List of Version of gj-gui>gj-gui dependency
Convert the code to GUI
Java GUI Class Example  Java GUI Class Example
Convert the code to GUI
GUI Java JSP application  GUI Java JSP application
Convert the code to GUI
GUI Application Development   GUI Application Development
Convert the code to GUI
Java and GUI application Example  Java and GUI application Example
Convert the code to GUI
Write a GUI Application  best way to write a GUI based application
Convert the code to GUI
How to Convert the code to GUI   How to convert a code into GUI
gui question
gui question  design a gui application and write code to accept a string from the user in a textfeild and print using option pane whether it is a palindrome or not. hint: abba is a palindrome   import java.awt.*; import
GUI problem
GUI problem  How do I make a Jbutton which is shaped like a circle. This button needs to be clicked in order to change color.   import java.awt.*; import java.awt.geom.*; import javax.swing.*; public class
Netbeans GUI Ribbon
Netbeans GUI Ribbon  how to create ribbon task in java GUI using netbeans
Convert the code to GUI
How to create GUI application in Java   How to create GUI application in Java
Convert the code to GUI
Java Code to GUI   can any one convert My code to GUI code
ModuleNotFoundError: No module named 'gui'
ModuleNotFoundError: No module named 'gui'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'gui' How to remove the ModuleNotFoundError: No module named 'gui' error
recursions in gui
the initial size: Use recursion to split the region into 2 smaller regions using... than half the initial size: Use recursion to split the region into 2 smaller... to split the region into 2 smaller regions using a vertical line with the split
Java GUI - Java3D
Java GUI  1) Using Java GUI, create a rectangular box that changes color each time a user click a change color button. 2) Modify Question 1 to include a new button named insert image, that allow user to insert a bitmap image
Java GUI - Java Beginners
Java GUI  HOW TO ADD ICONS IN JAVA GUI PROGRAMMES
Convert the code to GUI
Convert the code   How to convert a code to GUI look alike
Convert the code to GUI
Is it possible to convert a code into GUI  Is it possible to convert a code into GUI
how to refresh my GUI page
how to refresh my GUI page  how to refresh a GUI in java
Regarding GUI Applications
Regarding GUI Applications  How to create a save and open jmenu item in java desktop application
Magic Matrix in GUI
Magic Matrix in GUI  I want program in java GUI contain magic matrix for numbers
Artifacts of gj-gui
List of Artifacts of gj-gui maven depenency
Maven Repository/Dependency: gj-gui | gj-gui
Maven Repository/Dependency of Group ID gj-gui and Artifact ID gj-gui. Latest version of gj-gui:gj-gui dependencies. # Version Release Date You can read more at: Maven Tutorials
use is of 2
use is of 2  why we use 2 in "Integer.parseInt(str,2);" for binary to decimal conversion..??   ya i got it... because binary contain only two o and 1...so here use 2...same as octal has 8
Magicbox 2
Magicbox 2  how to build this? import java.util.*; class magicbox...; } } } } v15=a[0][0]+a[0][1]+a[0][2]==15 && a[1][0]+a[1][1]+a[1][2]==15 && a[2][0]+a[2][1]+a[2][2]==15 &&
Struts 2
Struts 2  we can extend DispatchAction class to implement a common session validation in struts 1.x. how to do the same in the struts2
Struts 2
Struts 2   I am just new to struts 2 and need to do the task. I have a requirement like this : when i click on link like Customer , this will display all the customers , address from a database table using jdbc and the screen
Struts 2
Struts 2  I am getting the following error.pls help me out. The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has
new 2
new 2  <%@page import="java.util.ArrayList"%> <%@page...; @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2 sevelet @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2 import java.io.IOException; import java.io.PrintWriter; import
Flex SDK GUI
Flex SDK GUI  Hi....... please give me ans of this question.. What classes do you typically extend in the Flex SDK for creating GUI controls and why? Thanks
GUI application program
GUI application program  Write a GUI application program that meets the following requirements: Create an array with 100 randomly chosen integers. Create a textfield to enter an array index and another textfield to display
Maven Dependency gj-gui >> 0.1
You should include the dependency code given in this page to add Maven Dependency of gj-gui >> gj-gui version0.1 in your project
java gui-with jscroll pane
java gui-with jscroll pane  Dear friends.. I have a doubt in my gui application. I developed 1 application. In this application is 1 Jscrollpane of height 600 and width 400. Normally it is showing 200 height and 400 width
java gui-with jscroll pane
java gui-with jscroll pane  Dear friends.. I have a doubt in my gui application. I developed 1 application. In this application is 1 Jscrollpane of height 600 and width 400. Normally it is showing 200 height and 400 width
ModuleNotFoundError: No module named 'argus-gui'
ModuleNotFoundError: No module named 'argus-gui'  Hi, My Python... 'argus-gui' How to remove the ModuleNotFoundError: No module named 'argus-gui' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'argus-gui'
ModuleNotFoundError: No module named 'argus-gui'  Hi, My Python... 'argus-gui' How to remove the ModuleNotFoundError: No module named 'argus-gui' error? Thanks   Hi, In your python environment you
ModuleNotFoundError: No module named 'argus-gui'
ModuleNotFoundError: No module named 'argus-gui'  Hi, My Python... 'argus-gui' How to remove the ModuleNotFoundError: No module named 'argus-gui' error? Thanks   Hi, In your python environment you

Ads