By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

Need Help To Edit Simple Program Thanks

P: 1
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
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
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

Post your reply

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