• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

basic graphics interface

Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I am trying to create a basic graphical user interface for sequence translation (including a JTextField for the description of a sequence and status of function button pressed e.g. “simple” translation and input and output TextFields). This involves a number of different class files. I cannot get my user interface to do what I want and I think I have problems with my "actionPerformed" method. Can anybody please point me in the right direction of where I might be going wrong? It could be that I’m missing something in how the code should be linked together?
Thank you in advance for any advice offered.

public void actionPerformed(ActionEvent event) {
try {
// Get the description, content and result
String d = tool.getDescription();
String input = tool.getInputText();
Stringr = translation.getResult();

// Translator uses getActionCommand() method to get a String indicating which button was pressed
// getActionCommand() returns a string which provides a simple way of identifying the button which was pressed.
String cmd = event.getActionCommand();
if (cmd.equals("Simple")) {

else if (cmd.equals("RevComp")) {
// A - Fetch description String (Translator asks for input text from TranlationTool)
// B - Fetch content string (Dna sequence) from input area
// C - Use to create a new DnaSequence
// Translator class calls a method (created by the programmer) in the TranslationTool
// which in turn fetches the description & content Strings & returns a new DnaSequence
DNASequence seq1 = new DNASequence(d, input);
// D - Otherwise perform the action & display the results in the (non-editable) output box
// call reverseComplement() on the DnaSequence object
// returns reverse complement version of DNA object used to call method
// Assign the returned revComp DNA object to a variable (another DnaSequence object)
DNASequence seq1RevComp = seq1.reverseComplement();
// Then get the content string from it
// & pass it to display in the output area (of TranslationPanel)
translation.getResult(); }
// Catch the exception which occurs if the text field contains an invalid sequence.
catch (InvalidSequenceException e) {
tool.tellUser("Invalid Sequence entered: " + e.getMessage());
Posts: 76807
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Good grief! What illegible code! Please post it again with decent line lengths and indentation and code tags. I tried adding code tags, but it didn't make it any more legible, partially because of the comments. Move the comments out of the code because they obviously form part of the question rather than the code.
Do you mean you are having difficulties with the GUI or with the action performed method?

Using action commands usually means you are moving out of the realms of object‑oriented programming, I am afraid. (Or, if you use Java8, out of the realms of functional programming.) You end up with a method full of if-elses which is difficult to understand and difficult to maintain. The OO way to do it is to add an object to each button object. (The functional way to do it is to add a λ for each button object.) And make the action performed methods short. If you can get them down to one line, so much the better; as you will see you can simply call another method. I have a rule of thumb about listeners, which you can read about here. You need to be careful not to create two anonymous classes whose methods do similar things.

You will end up with something like this:-So what the button does is call that method. Start that method with something like this:-
System.out.println("myMethod started.");
…and you will know that you have called the method. When you see that output you know that the button and the Listener are working. Do that for each button in turn, test it, and don't move onto the next button and listener until you know it is working.
Simon Cookridge
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thank you for your advice.
I have already registered the translator class (code above) as a listener for the buttons (the code for this is in a separate control panel class):

When I use the following code I get the appropriate message when I press any of the buttons.

What I am struggling with is that I cannot get the buttons to use a method to perform a function when pressed. Namely, I cannot get the reversecomplent button to reverse a dna sequence. In the first instance I would be happy for it to do this just by what is typed in a dialogue box in the GUI, I would then like to load from files.
In the translator class I create a translator object, providing references to a translation tool frame class (tool), information panel class (entries) and translation panel class (translation). This communicates back to the translation tool frame to fetch information to carry out actions. An instance variable is used to call methods “backward” into the translation tool class.

Advice much appreciated.
Those cherries would go best on cherry cheesecake. Don't put those cherries on this tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic