This code is for a GUI Java program that is supposed to convert back and fourth between two numbering systems. For example, binary to decimal or decimal to binary. I have created methods for some of the conversions however, I could successfully develop a method to convert from hex to binary. If you plan to run it to see what happens to the current method that I made please know that in the GUI the north end from left to right is as follows. The textfield is for user entry of any type of data. The first combobox is for the user specifying to the program what type of data he or she entered. The second combobox is for the user to choose what he wants that data converted to. The button is to convert it. The south side has a textarea that gives out the results. Please note that only some of the conversions work so far. There are comments in the code to label which methods do what converisons. Please help me find a method that will convert from hex to binary or please tell me what is wrong with the current method. For more information, please just question me with a post. Thanks guys.
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; import javax.swing.text.Highlighter; public class converter extends JFrame implements ActionListener { private JPanel box; private JPanel flow; private JTextField input; private JComboBox convsel; private JComboBox convoutsel; private JButton subconvert; private JTextArea output; private String[] conversions; private converter() { super("Number Systems Converter"); conversions = new String[]{"Binary" , "Decimal", "Hex"}; box = new JPanel(); box.setLayout(new BoxLayout(box, BoxLayout.PAGE_AXIS)); flow = new JPanel(); flow.setLayout(new FlowLayout()); input = new JTextField(); convsel = new JComboBox(conversions); convoutsel = new JComboBox(conversions); subconvert = new JButton("Convert"); output = new JTextArea(); output.setEditable(false); output.setText("Output"); input.setPreferredSize(new Dimension(200, (int)convsel.getPreferredSize().getHeight())); input.setEditable(true); convoutsel.setPreferredSize(new Dimension(150, (int)convsel.getPreferredSize().getHeight())); convsel.setPreferredSize(new Dimension(150, (int)convsel.getPreferredSize().getHeight())); output.setPreferredSize(new Dimension((int)output.getPreferredSize().getWidth(), 410)); subconvert.addActionListener(this); flow.add(input); flow.add(convsel); flow.add(convoutsel); flow.add(subconvert); JScrollPane scrolloutput = new JScrollPane(output); scrolloutput.setPreferredSize(new Dimension((int)output.getPreferredSize().getWidth() -50, 410 -50)); box.add(flow, BorderLayout.NORTH); box.add(scrolloutput, BorderLayout.SOUTH); add(box, BorderLayout.NORTH); } private boolean binary() { if(convsel.getSelectedItem() != "Binary") return false; return true; } private boolean decimal() { if(convsel.getSelectedItem() != "Decimal") return false; return true; } private boolean hex() { if(convsel.getSelectedItem() != "Hex") return false; return true; } private void convert() { if(convsel.getSelectedItem() == convoutsel.getSelectedItem()) { SwingUtilities.invokeLater( new Runnable() { public void run() { output.append("\nNumbers of the same type do not result in a conversion."); } } ); } //converts from binary to decimal if(binary() == true && convoutsel.getSelectedItem() == "Decimal") { final int decimal = Integer.parseInt(input.getText(), 2); SwingUtilities.invokeLater( new Runnable() { public void run() { output.append("\nThe conversion from " + convsel.getSelectedItem() + " to " + convoutsel.getSelectedItem() + " for the input of " + input.getText().toString() + " resulted in the output of " + Integer.toString(decimal) + "."); } } ); } //converts from decimal to binary if(decimal() == true && convoutsel.getSelectedItem() == "Binary") { final int bin = Integer.parseInt(input.getText()); final String binstring = Integer.toBinaryString(bin); SwingUtilities.invokeLater( new Runnable() { public void run() { output.append("\nThe conversion from " + convsel.getSelectedItem() + " to " + convoutsel.getSelectedItem() + " for the input of " + input.getText().toString() + " resulted in the output of " + binstring + "."); } } ); } //converts from binary to hex if(binary() == true && convoutsel.getSelectedItem() == "Hex") { final int binary = Integer.parseInt(input.getText(), 2); final String hexstring = Integer.toHexString(binary); SwingUtilities.invokeLater( new Runnable() { public void run() { output.append("\nThe conversion from " + convsel.getSelectedItem() + " to " + convoutsel.getSelectedItem() + " for the input of " + input.getText().toString() + " resulted in the output of " + hexstring + "."); } } ); } //converts from hex to binary if(hex() == true && convoutsel.getSelectedItem() == "Binary") { final int hex = Integer.parseInt(input.getText(), 16); final String binary = Integer.toBinaryString(hex); SwingUtilities.invokeLater( new Runnable() { public void run() { output.append("\nThe conversion from " + convsel.getSelectedItem() + " to " + convoutsel.getSelectedItem() + " for the input of " + input.getText().toString() + " resulted in the output of " + binary + "."); } } ); } } public void actionPerformed(ActionEvent event) { if(event.getSource() == subconvert) { convert(); } } public static void main(String[] args) { converter object = new converter(); object.setDefaultCloseOperation(EXIT_ON_CLOSE); object.setVisible(true); object.setSize(625,450); } }