Question

In: Computer Science

Write a program in Java Swing that can Display all the name list then will generate...

Write a program in Java Swing that can Display all the name list then will generate and display the longest name out of a list of names. Thank you...

Solutions

Expert Solution

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

class LongestName
{
public static void main(String args[])
{
JFrame f1=new JFrame("List of Names and Longest Name");
f1.setLayout(null);
DefaultListModel<String> dl1=new DefaultListModel<String>();

dl1.addElement("Pravin");
dl1.addElement("Samadhan");
dl1.addElement("Nitin");
dl1.addElement("Teerthansh");
dl1.addElement("Firoz");
dl1.addElement("David");

JList<String> l1=new JList<String>(dl1);
l1.setBounds(100,100,100,120);
f1.add(l1);

JButton b1=new JButton("Display Longest Name");
b1.setBounds(100,260,200,30);
f1.add(b1);

b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{

String longName="";
JFrame f2=new JFrame();

int size=l1.getModel().getSize();
int len=0;
for(int i=0;i<size;i++)
{
String nm=l1.getModel().getElementAt(i);
if(nm.length()>len)
{
len=nm.length();
longName=nm;
}
}
JOptionPane.showMessageDialog(f2,"Longest Name is "+longName+" with length "+len);
}
}

);


f1.setVisible(true);
f1.setSize(450,450);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}


Related Solutions

Specifications: Write a Java program called LifeRaft.java that will do all of the following: Display a...
Specifications: Write a Java program called LifeRaft.java that will do all of the following: Display a title Ask the user to enter the following values: The type of plane(Boeing 747 or Airbus A300) The number of passengers on board the Plane The number of crew aboard the plane The maximum number of people that can be carried by one liferaft assuming all the liferafts on the plane are the same size The actual number of liferafts that are available on...
Write a C++ program to display toy name
Write a C++ program to display toy name
write java program that prompt the user to enter two numbers .the program display all numbers...
write java program that prompt the user to enter two numbers .the program display all numbers between that are divisible by 7 and 8 ( the program should swap the numbers in case the secone number id lower than first one please enter two integer number : 900 199 Swapping the numbers 224 280 336 392 448 504 560 616 672 728 784 840 896 i need it eclipse
write java program that prompt the user to enter two numbers .the program display all numbers...
write java program that prompt the user to enter two numbers .the program display all numbers between that are divisible by 7 and 8 ( the program should swap the numbers in case the secone number id lower than first one please enter two integer number : 900 199   Swapping the numbers 224 280 336 392 448 504 560 616 672 728 784 840 896 i need it eclipse
JAVA: Display all even numbers between 1 and 20 --Optional write a program that reads in...
JAVA: Display all even numbers between 1 and 20 --Optional write a program that reads in 20 numbers. A method with an int parameter should display whether the number is odd or even for the number passed
Write a java program that read a line of input as a sentence and display: ...
Write a java program that read a line of input as a sentence and display:  Only the uppercase letters in the sentence.  The sentence, with all lowercase vowels (i.e. “a”, “e”, “i”, “o”, and “u”) replaced by a strike symbol “*”.
Java Program Use for loop 1.) Write a program to display the multiplication table of a...
Java Program Use for loop 1.) Write a program to display the multiplication table of a given integer. Multiplier and number of terms (multiplicand) must be user's input. Sample output: Enter the Multiplier: 5 Enter the number of terms: 3 5x0=0 5x1=5 5x2=10 5x3=15 2 Create a program that will allow the user to input an integer and display the sum of squares from 1 to n. Example, the sum of squares for 10 is as follows: (do not use...
Write a java program which can randomly generate a permutation of the integer {1, 2, 3,...
Write a java program which can randomly generate a permutation of the integer {1, 2, 3, ..., 48,49,50}. Use the most efficient sorting algorithm to sort the list in an acceding order.
Write a PHP Program to display all the $_SERVER elements
Write a PHP Program to display all the $_SERVER elements
Write a program to display privilege list of the roles and role list for privileges using...
Write a program to display privilege list of the roles and role list for privileges using many to many concept. Problem Specification: 1. Prefill the data for roles and privileges. 2. Create a menu 1. Assign privilege 2. Cancel privilege 3. Search privilege by role 4. Search role by privilege. 3. Assign privilege -- To assign specific privilege for role. 4. Cancel Privilege -- To cancel specific privilege for role. 5. Search privilege by role -- To display all the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT