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 the element at the specific index.
Create a 'Show Element' Button to cause the array element to be displayed. If the specified index is out of bounds, display the message "Out of Bounds"
Use Try Catch to catch an out of bounds index and show the appropriate message.
View Answers
December 1, 2012 at 5:35 PM
Here is a code that finds the element from the given index of array.
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
class ShowElement extends JFrame{
JButton ADD;
JPanel panel;
JLabel label1,label2;
final JTextField text1,text2;
ShowElement(){
label1 = new JLabel();
label1.setText("Enter Index:");
text1 = new JTextField(20);
label2 = new JLabel();
label2.setText("Element at specific Index:");
text2 = new JTextField(20);
ADD=new JButton("Show Element");
panel=new JPanel(new GridLayout(3,2));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(ADD);
add(panel,BorderLayout.CENTER);
Random r=new Random();
final int arr[]=new int[100];
for(int i=0;i<arr.length;i++){
int x=r.nextInt(100)+1;
arr[i]=x;
}
ADD.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
try{
String value=text1.getText();
int in=Integer.parseInt(value);
if(in>=100){
JOptionPane.showMessageDialog(null,"Out of Bounds");
}
else{
int ele=0;
for(int i=0;i<arr.length;i++){
if(i==in){
ele=arr[i];
}
}
text2.setText(Integer.toString(ele));
}
}
catch(Exception e){}
}
});
}
public static void main(String arg[])
{
try
{
ShowElement frame=new ShowElement();
frame.setSize(300,300);
frame.setVisible(true);
}
catch(Exception e){
}
}
}
Ads
Related Tutorials/Questions & Answers:
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
Write a GUI application for the WebBuy Company
Write a
GUI application for the WebBuy Company Write a
GUI application for the WebBuy Company that allows a user to compose the three parts... the
application starts, the text area will display a message listing the options
Advertisements
Write a java application program........?
Write a java
application program........? Welcome every One :
I have
Q in Java?
Q : Write a java
application program that reads 20 integer numbers input by the user and prints how many numbers > 100 and how many numbers
College Application Program
College
Application Program Please read the following carefully and submit the
program code.
Question # 144
A college is good if it satisfies the following conditions:
Its number of students is greater than 1000.
If the name
Java GUI to build a Student Registration Program
Java
GUI to build a Student Registration
Program Write a
program... in sorted order by last name.
Demonstrate the correct operation of your
program by creating a driver
program to load the student data base and the course data
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
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:â??, â... the
application starts, the text area will display a message listing the options which can
write a program for complete JDBC based application
write a
program for complete JDBC based
application thank you sir... based
application where in the user supplies the following table as input and the
program should have the facilities of Adding, Modifying, Viewing and Deleting
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 of the digits of the no. should be displayed. hint: suppose the user enters 12
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
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
program
program write a javascript
program to create a
application form with validation
program
program write a html
program in show table and this word
in box
program
program write a
program different between to dates in terms of days in java
program
program write a
program reversee a string.
input-hi this is roseindia
output-roseindia is this hi
program
program develop a servlet to insert the data in the database from our
program in the table stored in the database
program
program WAP a java
program to form 1/2+3/4+5/6+7/8 series