This part of my program needs to read from 4 parameter files and then displays them on the screen by clicking a different radio button. The program itself works, but my professor says that I have to improve the code in the DescriptionPanel 's listener because there are several lines of code that are identical and I should only have them once. Here is the code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Scanner;
public class DescriptionPanel extends JPanel
{
private JLabel quote;
private JRadioButton Sales, Operations, HR, Technology;
private String title;
public DescriptionPanel()
{
title = " Please click on a job title to get a description of what you are looking for?";
quote = new JLabel (title);
quote.setFont (new Font ("Arial", Font.BOLD, 14));
quote.setForeground (Color.red);
Sales = new JRadioButton ("Sales");
Sales.setFont (new Font ("Helvetica", Font.ITALIC, 14));
Operations = new JRadioButton ("Operations");
Operations.setFont (new Font ("Helvetica", Font.ITALIC, 14));
HR = new JRadioButton ("Human Resources");
HR.setFont (new Font ("Helvetica", Font.ITALIC, 14));
Technology = new JRadioButton ("Technology");
Technology.setFont (new Font ("Helvetica", Font.ITALIC, 14));
ButtonGroup group = new ButtonGroup();
group.add (Sales);
group.add (Operations);
group.add (HR);
group.add (Technology);
QuoteListener listener = new QuoteListener();
Sales.addActionListener (listener);
Operations.addActionListener (listener);
HR.addActionListener (listener);
Technology.addActionListener (listener);
add (Sales);
add (Operations);
add (HR);
add (Technology);
add (quote);
}
//************************************************** ***************
// Represents the listener for all radio buttons
//************************************************** ***************
private class QuoteListener implements ActionListener
{
//--------------------------------------------------------------
// Sets the text of the label depending on which radio
// button was pressed.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
Object source = event.getSource();
if (source == Sales)
{
try
{
String title = "";
Scanner fileScan = null;
fileScan = new Scanner (new File("sales.txt"));
while (fileScan.hasNext())
title= title + fileScan.nextLine();
quote.setText (title);
}
catch(IOException e)
{
}
}
else
if (source == Operations)
{
try
{
String title = "";
Scanner fileScan = null;
fileScan = new Scanner (new File("operations.txt"));
while (fileScan.hasNext())
title = title + fileScan.nextLine();
// quote.setText (title);
}
catch(IOException e)
{
}
}
else
if (source == HR)
{
try
{
String title = "";
Scanner fileScan = null;
fileScan = new Scanner (new File("hr.txt"));
while (fileScan.hasNext())
title = title + fileScan.nextLine();
// quote.setText (title);
}
catch(IOException e)
{
}
}
else
if (source == Technology)
{
try
{
String title = "";
Scanner fileScan = null;
fileScan = new Scanner (new File("tech.txt"));
while (fileScan.hasNext())
title = title + fileScan.nextLine();
// quote.setText (title);
}
catch(IOException e)
{
}
}
}
}
}
1 1479
This part of my program needs to read from 4 parameter files and then displays them on the screen by clicking a different radio button. The program itself works, but my professor says that I have to improve the code in the DescriptionPanel 's listener because there are several lines of code that are identical and I should only have them once. Here is the code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Scanner;
public class DescriptionPanel extends JPanel
{
private JLabel quote;
private JRadioButton Sales, Operations, HR, Technology;
private String title;
public DescriptionPanel()
{
title = " Please click on a job title to get a description of what you are looking for?";
quote = new JLabel (title);
quote.setFont (new Font ("Arial", Font.BOLD, 14));
quote.setForeground (Color.red);
Sales = new JRadioButton ("Sales");
Sales.setFont (new Font ("Helvetica", Font.ITALIC, 14));
Operations = new JRadioButton ("Operations");
Operations.setFont (new Font ("Helvetica", Font.ITALIC, 14));
HR = new JRadioButton ("Human Resources");
HR.setFont (new Font ("Helvetica", Font.ITALIC, 14));
Technology = new JRadioButton ("Technology");
Technology.setFont (new Font ("Helvetica", Font.ITALIC, 14));
ButtonGroup group = new ButtonGroup();
group.add (Sales);
group.add (Operations);
group.add (HR);
group.add (Technology);
QuoteListener listener = new QuoteListener();
Sales.addActionListener (listener);
Operations.addActionListener (listener);
HR.addActionListener (listener);
Technology.addActionListener (listener);
add (Sales);
add (Operations);
add (HR);
add (Technology);
add (quote);
}
//************************************************** ***************
// Represents the listener for all radio buttons
//************************************************** ***************
private class QuoteListener implements ActionListener
{
//--------------------------------------------------------------
// Sets the text of the label depending on which radio
// button was pressed.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
Object source = event.getSource();
if (source == Sales)
{
try
{
String title = "";
Scanner fileScan = null;
fileScan = new Scanner (new File("sales.txt"));
while (fileScan.hasNext())
title= title + fileScan.nextLine();
quote.setText (title);
}
catch(IOException e)
{
}
}
else
if (source == Operations)
{
try
{
String title = "";
Scanner fileScan = null;
fileScan = new Scanner (new File("operations.txt"));
while (fileScan.hasNext())
title = title + fileScan.nextLine();
// quote.setText (title);
}
catch(IOException e)
{
}
}
else
if (source == HR)
{
try
{
String title = "";
Scanner fileScan = null;
fileScan = new Scanner (new File("hr.txt"));
while (fileScan.hasNext())
title = title + fileScan.nextLine();
// quote.setText (title);
}
catch(IOException e)
{
}
}
else
if (source == Technology)
{
try
{
String title = "";
Scanner fileScan = null;
fileScan = new Scanner (new File("tech.txt"));
while (fileScan.hasNext())
title = title + fileScan.nextLine();
// quote.setText (title);
}
catch(IOException e)
{
}
}
}
}
}
-
-
import javax.swing.*;
-
import java.awt.*;
-
import java.awt.event.*;
-
import java.io.*;
-
import java.util.Scanner;
-
-
public class DescriptionPanel extends JPanel
-
{
-
private JLabel quote;
-
private JRadioButton Sales, Operations, HR, Technology;
-
private String title;
-
public DescriptionPanel()
-
{
-
title = " Please click on a job title to get a description of what you are looking for?";
-
quote = new JLabel (title);
-
quote.setFont (new Font ("Arial", Font.BOLD, 14));
-
quote.setForeground (Color.red);
-
-
//There is also a bit of the same code being written over and over again here.
-
Sales = new JRadioButton ("Sales");
-
Sales.setFont (new Font ("Helvetica", Font.ITALIC, 14));
-
Sales.setActionCommand("sales.txt");
-
Operations = new JRadioButton ("Operations");
-
Operations.setActionCommand("operations.txt");
-
Operations.setFont (new Font ("Helvetica", Font.ITALIC, 14));
-
HR = new JRadioButton ("Human Resources");
-
HR.setActionCommand("hr.txt");
-
HR.setFont (new Font ("Helvetica", Font.ITALIC, 14));
-
Technology = new JRadioButton ("Technology");
-
Technology.setFont (new Font ("Helvetica", Font.ITALIC, 14));
-
Technology.setActionCommand("tech.txt");
-
-
ButtonGroup group = new ButtonGroup();
-
group.add (Sales);
-
group.add (Operations);
-
group.add (HR);
-
group.add (Technology);
-
QuoteListener listener = new QuoteListener();
-
Sales.addActionListener (listener);
-
Operations.addActionListener (listener);
-
HR.addActionListener (listener);
-
Technology.addActionListener (listener);
-
-
-
-
add (Sales);
-
add (Operations);
-
add (HR);
-
add (Technology);
-
add (quote);
-
}
-
//************************************************** ***************
-
// Represents the listener for all radio buttons
-
//************************************************** ***************
-
private class QuoteListener implements ActionListener
-
{
-
//--------------------------------------------------------------
-
// Sets the text of the label depending on which radio
-
// button was pressed.
-
//--------------------------------------------------------------
-
public void actionPerformed (ActionEvent event) {
-
//No need to use if else because we set the action command 'cleverly'.
-
String file = event.getActionCommand();
-
String title = "";
-
Scanner fileScan = null;
-
try {
-
fileScan = new Scanner (new File(file));
-
while (fileScan.hasNext()) {
-
title = title + fileScan.nextLine();
-
}
-
}
-
catch(IOException e) {
-
e.printStackTrace();//leaving this blank means the exception willbe ignored!
-
}
-
quote.setText (title);
-
}
-
}
-
}
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Mike Kamermans |
last post by:
I hope someone can help me, because what I'm going through at the moment
trying to edit XML documents is enough to make me want to never edit XML
again.
I'm looking for an XML editor that has a...
|
by: James Fortune |
last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't
understand why people who know how to write code to completely replace
a front end do not write something that will automate...
|
by: Anna |
last post by:
I have a simple DataGrid that displays a list of characteristics and
allows you to edit a description for each characteristic. The query
that feeds this involves a join, as the characteristics and...
|
by: Lorenzino |
last post by:
Hi, I have a problem with bindings in a formview.
I have a formview; in the insert template i've created a wizard control and
inside
it i have an HTML table with some textboxes bound to the...
|
by: nurcan.kurtoglu |
last post by:
Hi,
I have a .php site which uses a mysql database. I would like some info
on how I can download (preferably free and simple) and use a GUI. As
in, I know they exist, but do I need to put it...
|
by: Terry Olsen |
last post by:
Can anyone give me some guidance on installing a local printer with VB 2005?
I'm guessing I would probably do this with WMI, but i'm not finding anything
obvious. I see how to add a networked...
|
by: natwong |
last post by:
Hi All,
I'm hoping that someone could help me out since I'm new with Access.
Background:
Database was set up as a simple data entry and reporting tool for Program Initiatives. The data...
|
by: adamurbas |
last post by:
ya so im pretty much a newb to this whole python thing... its pretty
cool but i just started today and im already having trouble. i
started to use a tutorial that i found somewhere and i followed...
|
by: sukatoa |
last post by:
I currently developing a simple notepad that could compile A86 codes using java.... Im using Windows XP SP2....
When @ compile, i am satisfied with the result...
When i try to invoke the .COM...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |