I have a probelm with my program. I need the \"generateField\" JTextField in the
ID: 3692342 • Letter: I
Question
I have a probelm with my program. I need the "generateField" JTextField in the print () method to print each loop itration but instead it will freeze and then print only the final answer. How can I make it print each loop iteration?
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.*;
import java.awt.Panel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.util.Arrays;
import java.util.List;
public class BF implements ActionListener {
private JPanel contentPane;
private JTextField password;
private JTextField length;
public JTextField generateField;
JRadioButton numbers;
JRadioButton capital;
JRadioButton small;
JButton generate;
JLabel passwordfound;
JLabel timelabel;
int min;
int max;
int stringLength;
long time;
Timer timer1;
private int[] chars;
String n="";
long start;
long end;
char[] passCom;
JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
BF frame = new BF();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public BF() {
Thread thread;
frame = new JFrame ("Brute Force Attack Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 754, 665);
frame.setResizable(false);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
frame.setLocationRelativeTo(null);
frame.setContentPane(contentPane);
contentPane.setLayout(null);
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch(Exception e) {
}
backgroundPanel panel = new backgroundPanel();
panel.setBounds(0, 0, 749, 633);
contentPane.add(panel);
panel.setLayout(null);
JLabel enter = new JLabel("What is your password?");
enter.setForeground(Color.WHITE);
enter.setFont(new Font("Times New Roman", Font.BOLD, 17));
enter.setBounds(141, 151, 193, 36);
panel.add(enter);
password = new JTextField();
password.setFont(new Font("Tahoma", Font.BOLD, 15));
password.setBounds(333, 149, 208, 43);
panel.add(password);
password.setColumns(10);
JLabel lblChooseTheBrute = new JLabel("Choose the Brute Force Settings:");
lblChooseTheBrute.setForeground(Color.WHITE);
lblChooseTheBrute.setFont(new Font("Tekton Pro", Font.PLAIN, 20));
lblChooseTheBrute.setBounds(208, 226, 293, 35);
panel.add(lblChooseTheBrute);
numbers = new JRadioButton("Digits from 0 to 9");
numbers.setForeground(Color.WHITE);
numbers.setFont(new Font("Tahoma", Font.PLAIN, 17));
numbers.setBounds(76, 291, 161, 25);
panel.add(numbers);
capital = new JRadioButton("Capital Letters Only (A-Z)");
capital.setForeground(Color.WHITE);
capital.setFont(new Font("Tahoma", Font.PLAIN, 17));
capital.setBounds(257, 291, 223, 25);
panel.add(capital);
small = new JRadioButton("Small Letters Only (a-z)");
small.setForeground(Color.WHITE);
small.setFont(new Font("Tahoma", Font.PLAIN, 17));
small.setBounds(492, 292, 225, 24);
panel.add(small);
ButtonGroup group = new ButtonGroup();
group.add(numbers);
group.add(capital);
group.add(small);
JLabel lblMaximumLengthOf = new JLabel("Maximum Length of String:");
lblMaximumLengthOf.setForeground(Color.WHITE);
lblMaximumLengthOf.setFont(new Font("Tahoma", Font.BOLD, 16));
lblMaximumLengthOf.setBounds(141, 339, 259, 28);
panel.add(lblMaximumLengthOf);
length = new JTextField();
length.setFont(new Font("Tahoma", Font.BOLD, 15));
length.setBounds(384, 336, 77, 35);
panel.add(length);
length.setColumns(10);
generate = new JButton("Find Password");
generate.setFont(new Font("Tahoma", Font.BOLD, 15));
generate.setBounds(292, 401, 145, 35);
generate.addActionListener(this);
panel.add(generate);
generateField = new JTextField();
generateField.setForeground(Color.WHITE);
generateField.setFont(new Font("Tahoma", Font.BOLD, 24));
generateField.setEditable(false);
generateField.setBounds(315, 449, 186, 54);
generateField.setBackground(new Color(0,0,0,0));
generateField.setBorder(javax.swing.BorderFactory.createEmptyBorder());
panel.add(generateField);
generateField.setColumns(10);
passwordfound = new JLabel("Password Found");
passwordfound.setForeground(Color.RED);
passwordfound.setFont(new Font("Tahoma", Font.BOLD, 18));
passwordfound.setBounds(242, 493, 259, 43);
panel.add(passwordfound);
passwordfound.setVisible(false);
timelabel = new JLabel("The search took seconds");
timelabel.setForeground(Color.WHITE);
timelabel.setFont(new Font("Times New Roman", Font.BOLD, 22));
timelabel.setBounds(51, 535, 386, 36);
panel.add(timelabel);
timelabel.setVisible(false);
frame.setVisible(true);
}
public void run(char min, char max, int len)
{
this.min = min;
this.max = max;
this.stringLength = len;
chars = new int[stringLength + 1];
Arrays.fill(chars, 1, chars.length, min);
start = System.currentTimeMillis();
while (chars[0] == 0&&!n.equals(password.getText()))
{
print();
increment();
n=String.valueOf(passCom);
passwordfound.setText("Searching..");
passwordfound.setVisible(true);
}
end = System.currentTimeMillis();
time = (end-start)/1000;
System.out.println(time);
passwordfound.setText("We found your password !");
//passwordfound.setVisible(true);
}
public void increment()
{
for (int i = chars.length - 1; i >= 0; i--)
{
if (chars[i] < max)
{
chars[i]++; return;
}
chars[i] = min;
}
}
public void print()
{
int x=0;
for (int i = 1; i < chars.length; i++)
{
passCom[x]=(char)chars[i];
x++;
String txt = new String (passCom);
generateField.setText(txt);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (!password.getText().equals("") && e.getSource() == generate && !length.getText().equals("")&&numbers.isSelected()){
int count = Integer.parseInt(length.getText());
passCom = new char [count];
passwordfound.setText("Searching..");
passwordfound.setVisible(true);
run('0','9',count);
if (time>=60)
{
timelabel.setText("The search took "+time+ " minute (s)");
timelabel.setVisible(true);
}
else
{
timelabel.setText("The search took "+time+ " second (s)");
timelabel.setVisible(true);
}
}
}
}
Explanation / Answer
you can use Print method as follows :-
public void print(){
String txt = "";
int x=0;
for (int i = 1; i < chars.length; i++){
passCom[x]=(char)chars[i];
x++;
txt.concat(passCom.toString());
}
generateField.setText(txt);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.