Back to Menu Button JAVA

greg6614 Posted messages 629 Status Member -  
greg6614 Posted messages 629 Status Member -
Bonjour,
I am currently in TS SI with a specialization in ISN and I am working on a small Java program to convert decimal to binary or hexadecimal and vice versa. The only issue is that I have set up Menu buttons to go back, but I don't know how to return to the start of the code because if I instantiate my main method, it opens a whole new window.

Here is a snippet of the code; if you have any ideas, I am open to suggestions ;)

import java.awt.Color; 
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Transtypage_Bin_Dec extends JPanel implements ActionListener{

//VARIABLE DECLARATION

private JButton BD = new JButton ("Bin / Dec");
private JButton DB = new JButton ("Dec / Bin");
private JButton BH = new JButton ("Bin / Hex");
private JButton HB = new JButton ("Hex / Bin");
private JButton HD = new JButton ("Hex / Dec");
private JButton DH = new JButton ("Dec / Hex");
private JButton OK1 = new JButton (" OK ");
private JButton OK2 = new JButton (" OK ");
private JButton OK3 = new JButton (" OK ");
private JButton OK4 = new JButton (" OK ");
private JButton OK5 = new JButton (" OK ");
private JButton OK6 = new JButton (" OK ");
private JButton Menu = new JButton (" Menu ");
private JLabel Texte = new JLabel(" Choose the conversion to perform ");
private JLabel convert = new JLabel("");
private JPanel Panel;
private JTextField number = new JTextField();

//CONSTRUCTOR----------------------------------------------------------------------------------//

public Transtypage_Bin_Dec() {
//
//
Panel = new JPanel(); //
//
// Format of panel elements: button listeners //
//
BD.addActionListener(this);
DB.addActionListener(this);
BH.addActionListener(this);
HB.addActionListener(this);
HD.addActionListener(this);
DH.addActionListener(this);

//
//
// Add elements to the panel //
//
Panel.add(BD);
Panel.add(DB);
Panel.add(BH);
Panel.add(HB);
Panel.add(HD);
Panel.add(DH);
Panel.add(Texte); //
} //
//----------------------------------------------------------------------------------------------//

//METHOD TO GET THE INPUT PANEL--------//
//
public JPanel getPanneau(){ //
return Panel; //
} //
//----------------------------------------------//

//METHOD LISTENING TO THE OK BUTTON-------------------------------------------------------------------------//

public void actionPerformed(ActionEvent evt) {

if(evt.getSource() == BD){
Panel.removeAll();
Panel.setBackground(Color.WHITE);
number.setPreferredSize(new Dimension(150, 30));
Panel.add(Texte);
Panel.add(number);
Panel.add(convert);
Texte.setText("Enter your number :");
convert.setText("Binary to Decimal Conversion");
Panel.add(OK1);
Panel.add(Menu);
OK1.addActionListener(this);
Menu.addActionListener(this); }

if(evt.getSource() == OK1) {
String NumberSaisie = number.getText();
Panel.removeAll();
Panel.setBackground(Color.WHITE);
Panel.updateUI();
Panel.add(Texte);
Panel.add(convert);
Texte.setText("Binary number : "+NumberSaisie);
convert.setText("Decimal number : "+NumberSaisie);
Panel.add(Menu);
Menu.addActionListener(this);}

if(evt.getSource() == Menu) {

}

if(evt.getSource() == DB){
Panel.removeAll();
Panel.setBackground(Color.WHITE);
number.setPreferredSize(new Dimension(150, 30));
Panel.add(Texte);
Panel.add(number);
Panel.add(convert);
Texte.setText("Enter your number :");
convert.setText("Decimal to Binary Conversion");
Panel.add(OK2);
Panel.add(Menu);
OK2.addActionListener(this);}

if(evt.getSource() == OK2) {
String NumberSaisie = number.getText();
Panel.removeAll();
Panel.setBackground(Color.WHITE);
Panel.updateUI();
Panel.add(Texte);
Panel.add(convert);
Texte.setText("Decimal number : "+NumberSaisie);
convert.setText("Binary number : "+NumberSaisie);
Panel.add(Menu);}

if(evt.getSource() == BH){
Panel.removeAll();
Panel.setBackground(Color.WHITE);
number.setPreferredSize(new Dimension(150, 30));
Panel.add(Texte);
Panel.add(number);
Panel.add(convert);
Texte.setText("Enter your number :");
convert.setText("Binary to Hexadecimal Conversion");
Panel.add(OK3);
Panel.add(Menu);
OK3.addActionListener(this);}

if(evt.getSource() == OK3) {
String NumberSaisie = number.getText();
Panel.removeAll();
Panel.setBackground(Color.WHITE);
Panel.updateUI();
Panel.add(Texte);
Panel.add(convert);
Texte.setText("Binary number : "+NumberSaisie);
convert.setText("Hexadecimal number : "+NumberSaisie);
Panel.add(Menu);}

if(evt.getSource() == HB){
Panel.removeAll();
Panel.setBackground(Color.WHITE);
number.setPreferredSize(new Dimension(150, 30));
Panel.add(Texte);
Panel.add(number);
Panel.add(convert);
Texte.setText("Enter your number :");
convert.setText("Hexadecimal to Binary Conversion");
Panel.add(OK4);
Panel.add(Menu);
OK4.addActionListener(this);}

if(evt.getSource() == OK4) {
String NumberSaisie = number.getText();
Panel.removeAll();
Panel.setBackground(Color.WHITE);
Panel.updateUI();
Panel.add(Texte);
Panel.add(convert);
Texte.setText("Hexadecimal number : "+NumberSaisie);
convert.setText("Binary number : "+NumberSaisie);
Panel.add(Menu);}

if(evt.getSource() == HD){
Panel.removeAll();
Panel.setBackground(Color.WHITE);
number.setPreferredSize(new Dimension(150, 30));
Panel.add(Texte);
Panel.add(number);
Panel.add(convert);
Texte.setText("Enter your number :");
convert.setText("Hexadecimal to Decimal Conversion");
Panel.add(OK5);
Panel.add(Menu);
OK5.addActionListener(this);}

if(evt.getSource() == OK5) {
String NumberSaisie = number.getText();
Panel.removeAll();
Panel.setBackground(Color.WHITE);
Panel.updateUI();
Panel.add(Texte);
Panel.add(convert);
Texte.setText("Hexadecimal number : "+NumberSaisie);
convert.setText("Decimal number : "+NumberSaisie);
Panel.add(Menu);}

if(evt.getSource() == DH){
Panel.removeAll();
Panel.setBackground(Color.WHITE);
number.setPreferredSize(new Dimension(150, 30));
Panel.add(Texte);
Panel.add(number);
Panel.add(convert);
Texte.setText("Enter your number :");
convert.setText("Decimal to Hexadecimal Conversion");
Panel.add(OK6);
Panel.add(Menu);
OK6.addActionListener(this);}

if(evt.getSource() == OK6) {
String NumberSaisie = number.getText();
Panel.removeAll();
Panel.setBackground(Color.WHITE);
Panel.updateUI();
Panel.add(Texte);
Panel.add(convert);
Texte.setText("Decimal number : "+NumberSaisie);
convert.setText("Hexadecimal number : "+NumberSaisie);
Panel.add(Menu);}

}
}


Configuration: Windows 7 / Firefox 25.0

--
Hoping to have helped you
Greg

2 answers

  1. KX Posted messages 19031 Status Moderator 3 020
     
    Ouch !

    public class Transtypage_Bin_Dec extends JPanel implements ActionListener { 
    private JPanel Panel;
    public Transtypage_Bin_Dec() {
    Panel = new JPanel()
    }
    public JPanel getPanneau() {
    return Panel;
    }
    }

    Do you understand the inheritance mechanisms well?
    If you extend JPanel, you don't have to manipulate a JPanel object and do a get, you should manipulate this directly...
    Moreover, creating a Container class where its Components' Listener is itself is not logical; rather, each component should be associated with the listener that concerns it, instead of having one big global listener.

    Finally, and not least, it is better to have several JPanels and switch from one to another in your JFrame, than to have a single JPanel and constantly clear all its content. If you had one JPanel for your Menu and one JPanel for each of your conversions, your issue of returning to the menu would not be one; you would just need to switch from JPanel to JPanel.
    --
    Trust does not exclude control.
    0
  2. greg6614 Posted messages 629 Status Member 107
     
    Thank you for your response, but I only started programming in JAVA in September, so I don't understand everything, and I admit I find your solution a bit complicated for my level.

    --
    Hoping to have helped you
    Greg
    0
    1. greg6614 Posted messages 629 Status Member 107
       
      I couldn't find how to edit my post, but anyway, I looked it up online and I decided to do this by making a listener for my Menu button that would create a new Panel(); however, it opens a new window, so to avoid having multiple ones, I want to close the other one first, but the dispose() method isn't working. I can't interact with my JFrame that is defined in the Panel class.
      0