473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Need Help To Edit Simple Program Thanks

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)
{
}

}
}
}
}
Dec 29 '06 #1
1 1479
r035198x
13,262 8TB
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)
{
}

}
}
}
}



Expand|Select|Wrap|Line Numbers
  1.  
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import java.util.Scanner;
  7.  
  8. public class DescriptionPanel extends JPanel
  9. {
  10. private JLabel quote;
  11. private JRadioButton Sales, Operations, HR, Technology;
  12. private String title;
  13. public DescriptionPanel()
  14. {
  15. title = " Please click on a job title to get a description of what you are looking for?";
  16. quote = new JLabel (title);
  17. quote.setFont (new Font ("Arial", Font.BOLD, 14));
  18. quote.setForeground (Color.red);
  19.  
  20. //There is also a bit of the same code being written over and over again here.
  21. Sales = new JRadioButton ("Sales");
  22. Sales.setFont (new Font ("Helvetica", Font.ITALIC, 14));
  23. Sales.setActionCommand("sales.txt");
  24. Operations = new JRadioButton ("Operations");
  25. Operations.setActionCommand("operations.txt");
  26. Operations.setFont (new Font ("Helvetica", Font.ITALIC, 14));
  27. HR = new JRadioButton ("Human Resources");
  28. HR.setActionCommand("hr.txt");
  29. HR.setFont (new Font ("Helvetica", Font.ITALIC, 14));
  30. Technology = new JRadioButton ("Technology");
  31. Technology.setFont (new Font ("Helvetica", Font.ITALIC, 14));
  32. Technology.setActionCommand("tech.txt");
  33.  
  34. ButtonGroup group = new ButtonGroup();
  35. group.add (Sales);
  36. group.add (Operations);
  37. group.add (HR);
  38. group.add (Technology);
  39. QuoteListener listener = new QuoteListener();
  40. Sales.addActionListener (listener);
  41. Operations.addActionListener (listener);
  42. HR.addActionListener (listener);
  43. Technology.addActionListener (listener);
  44.  
  45.  
  46.  
  47. add (Sales);
  48. add (Operations);
  49. add (HR);
  50. add (Technology);
  51. add (quote);
  52. }
  53. //************************************************** ***************
  54. // Represents the listener for all radio buttons
  55. //************************************************** ***************
  56. private class QuoteListener implements ActionListener
  57. {
  58. //--------------------------------------------------------------
  59. // Sets the text of the label depending on which radio
  60. // button was pressed.
  61. //--------------------------------------------------------------
  62. public void actionPerformed (ActionEvent event) {
  63.  //No need to use if else because we set the action command 'cleverly'.
  64.  String file = event.getActionCommand();
  65.  String title = "";
  66.  Scanner fileScan = null;
  67.  try {
  68.   fileScan = new Scanner (new File(file));
  69.   while (fileScan.hasNext()) {
  70.    title = title + fileScan.nextLine();
  71.   }
  72.  }
  73.  catch(IOException e) {
  74.   e.printStackTrace();//leaving this blank means the exception willbe ignored!
  75.  }
  76.  quote.setText (title);
  77. }
  78. }
  79. }
  80.  
  81.  
Dec 30 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
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...
19
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...
1
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...
7
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...
4
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...
2
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...
1
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...
12
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...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.