I’m working on a simple tool where a user inputs their string or RNA or DNA and clicks a button which then transcribes it. When I click my transcribe or reverse transcribe JButton nothing happens! Please help! Any direction, hints or help would be great! Thank you! Here is my code:
import java.awt.BorderLayout;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.event.*;
public class TranslationGUI extends javax.swing.JFrame implements ActionListener
{
public static final int WIDTH = 1500;
public static final int HEIGHT = 500;
TextArea DNATextArea, RNATextArea, proteinTextArea;
JButton transcribe, translate, reverseTranscribe;
String DNA;
String RNA;
/** Creates new form TranslationGUI */
public TranslationGUI()
{
super("Transcription and Translation Tool");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout());
JPanel DNAPanel = new JPanel();
DNAPanel.setPreferredSize(new Dimension(440,400));
DNAPanel.add(new JLabel("DNA Sequence"));
TextArea DNATextArea = new TextArea();
DNAPanel.add(DNATextArea);
DNATextArea.setEditable(true);
DNAPanel.add(new JLabel("DNA (deoxyribonucleic acid)"));
JButton transcribe = new JButton();
DNAPanel.add(new JButton("Transcribe"));
transcribe.addActionListener(this);
add(DNAPanel, BorderLayout.WEST);
DNAPanel.setBackground(Color.WHITE);
JPanel RNAPanel = new JPanel();
RNAPanel.setPreferredSize(new Dimension(440,400));
RNAPanel.add(new JLabel("RNA Sequence"));
TextArea RNATextArea = new TextArea();
RNATextArea.setEditable(true);
RNAPanel.add(RNATextArea);
RNAPanel.add(new JLabel("RNA (ribonucleic acid)"));
JButton translate = new JButton();
RNAPanel.add(new JButton("Translate"));
translate.addActionListener(this);
JButton reverseTranscribe = new JButton();
RNAPanel.add(new JButton("Reverse Transcribe"));
reverseTranscribe.addActionListener(this);
add(RNAPanel, BorderLayout.CENTER);
RNAPanel.setBackground(Color.LIGHT_GRAY);
JPanel proteinPanel = new JPanel();
proteinPanel.setPreferredSize(new Dimension(440,400));
proteinPanel.add(new JLabel("Protein Sequence"));
TextArea proteinTextArea = new TextArea();
proteinPanel.add(proteinTextArea);
proteinPanel.add(new JLabel("Protein"));
add(proteinPanel, BorderLayout.EAST);
proteinPanel.setBackground(Color.WHITE);
pack();
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource()== transcribe)
{
DNATranscription();
}
else if(event.getSource()== reverseTranscribe)
{
RNATranscription();
}
else if(event.getSource()== translate)
{
Translate();
}
}
public void DNATranscription()
{
String DNA = DNATextArea.getText();
char[] reverse = new char[DNA.length()];
for (int i = 0; i < reverse.length; i++)
{
switch(DNA.charAt(i))
{
case 'A': reverse[i] = 'T';break;
case 'T': reverse[i] = 'A';break;
case 'C': reverse[i] = 'G';break;
case 'G': reverse[i] = 'C';break;
default:
System.out.println("Not a DNA code");
}
}
DNA = new String(reverse);
RNATextArea.append(DNA);
}
public void RNATranscription()
{
String RNA = RNATextArea.getText();
char[] reverse = new char[RNA.length()];
for (int i = 0; i < reverse.length; i++)
{
switch(RNA.charAt(i))
{
case 'T': reverse[i] = 'A';break;
case 'A': reverse[i] = 'T';break;
case 'G': reverse[i] = 'C';break;
case 'C': reverse[i] = 'G';break;
default:
System.out.println("Not a RNA code");
}
}
RNA = new String(reverse);
DNATextArea.append(RNA);
}
public void Translate()
{
//do something
}
private static final String[][] CODON_AMINO = //table for the codon
//that corresponds to a protein
{
{"att", "i"}, {"atc", "i"}, {"ata", "i"}, {"ctt", "l"},
{"ctc", "l"}, {"cta", "l"}, {"ctg", "l"}, {"tta", "l"},
{"ttg", "l"}, {"gtt", "v"}, {"gtc", "v"}, {"gta", "v"},
{"gtg", "v"}, {"ttt", "f"}, {"ttc", "f"}, {"atg", "m"},
{"tgt", "c"}, {"tgc", "c"}, {"gct", "a"}, {"gcc", "a"},
{"gca", "a"}, {"gcg", "a"}, {"ggt", "g"}, {"ggc", "g"},
{"gga", "g"}, {"ggg", "g"}, {"cct", "p"}, {"ccc", "p"},
{"cca", "p"}, {"ccg", "p"}, {"act", "t"}, {"acc", "t"},
{"aca", "t"}, {"acg", "t"}, {"tct", "s"}, {"tcc", "s"},
{"tca", "s"}, {"tcg", "s"}, {"agt", "s"}, {"agc", "s"},
{"tat", "y"}, {"tac", "y"}, {"tgg", "w"}, {"caa", "q"},
{"cag", "q"}, {"aat", "n"}, {"aac", "n"}, {"cat", "h"},
{"cac", "h"}, {"gaa", "e"}, {"gag", "e"}, {"gat", "d"},
{"gac", "d"}, {"aaa", "k"}, {"aag", "k"}, {"cgt", "r"},
{"cgc", "r"}, {"cga", "r"}, {"cgg", "r"}, {"aga", "r"},
{"agg", "r"}
};
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-416)/2, (screenSize.height-338)/2, 416, 338);
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
TranslationGUI frame = new TranslationGUI();
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TranslationGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TranslationGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TranslationGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TranslationGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TranslationGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
// End of variables declaration
}
Your problem is that you’re shadowing variables.
You declare your
JButtonvariables in the class, but then re-declare them in the constructor, and so theJButtonobject that gets the listener attached is not referred to by theJButtonvariable in the class. So when youractionPerformedmethod checks to see if theJButtonpressed is the transcribeJButton, it doesn’t match, and in fact transcribe is actually null at that point (test it and see). The solution: don’t re-declare your variable in the constructor.In other words, in your constructor change this:
to this:
Edit
Heck, you’re adding yet another JButton to the GUI — don’t do this!
Add the JButton that you’ve created just above this.
e.g.,
It’s almost as if you are trying extremely hard to make sure that the
JButtonvariable in the class won’t work. I’m kidding of course, but you’re blocking it in 2 different ways, something I’ve never seen before — and I thought that I’ve seen everything.