vendredi 25 mars 2016

In a java Jframe can I add/remove a Jlabel from view using a checkbox?

Im making an application that lets me perform hashes, at the top I want to have a group of check boxes basically saying the four different hash types I have. When the check boxes are selected I want the labels to appear showing the hashed entered text.

I've attached an image to hopefully make it easier to understand what I mean. The reason I'm doing this is so that when the final program is made with almost 10 rows of text boxes and labels it can be reduced only to show the ones that the user wishes to see.This hopefully should explain what I mean.

I've been able to get it so the checkboxes make it visible or not visible but that also then just leaves a blank space where one row of labels used to be rather than moving everything up a row

I've now added my coding so people can see how I'm doing it currently and help define where needs to be modified

import java.awt.*;
import java.awt.event.*;
import java.security.*;
import javax.swing.*;

public class Hasher extends JFrame implements ActionListener {
    String UserInput;
    private JTextField textInputField;
    private static JLabel MD5Hashed,MD5Label;
    private static JCheckBox MD5Check, SHA1Check, SHA256Check, FileCheck;
    private JFrame contentPane;

public Hasher() {
    this.setTitle("Hasher");
    Container contentPane = this.getContentPane();
    contentPane.setLayout(new GridLayout(0,1) );
    contentPane.setBackground(new Color(88,148,202));

    //CheckBoxes
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());
        JPanel checkBoxPanel = new JPanel();
        MD5Check = new JCheckBox("MD5");
        MD5Check.addActionListener(new ActionListener() {
@Override
            public void actionPerformed(ActionEvent e) {
    boolean Visible = MD5Check.isSelected();
    MD5Hashed.setVisible(Visible);
    MD5Label.setVisible(Visible);
            }
        });
        checkBoxPanel.add(MD5Check);
        SHA1Check = new JCheckBox("SHA-1");
        checkBoxPanel.add(SHA1Check);
        SHA256Check = new JCheckBox("SHA-256");
        checkBoxPanel.add(SHA256Check);
        FileCheck = new JCheckBox("File Hashing");
        checkBoxPanel.add(FileCheck);
        mainPanel.add(checkBoxPanel);
        contentPane.add(mainPanel);


//Entered data to perform hash on
    contentPane.add(new JLabel ("   Enter text to hash"));
    textInputField = new JTextField();
    //HashingProcess inputListener = new HashingProcess( );
    //textInputField.addActionListener(inputListener);
    contentPane.add( textInputField);

//MD5 hash is completed 
    MD5Label = new JLabel( "   Using MD5 the hash is: " );
    contentPane.add( MD5Label);
    MD5Hashed = new JLabel( "??") ;
    contentPane.add( MD5Hashed );
    MD5Hashed.setVisible(false);
    MD5Label.setVisible(false);

}

public static void main(String[] args) {
    Hasher theWindow = new Hasher( );
    theWindow.setSize(400, 400 );
    theWindow.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    theWindow.setVisible(true);

}

}




Aucun commentaire:

Enregistrer un commentaire