Skip to content

Commit

Permalink
Add file chooser to GUI
Browse files Browse the repository at this point in the history
Issue JingMa87#17: A button has been added to the GUI, which when clicked creates a JFileChooser object that allows the user to select a text file from which text is fetched. This fetched text is then used to replace the contents of the input text area.
  • Loading branch information
yashugrankar committed Oct 30, 2016
1 parent f089aa2 commit f7435e6
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/dist/
4 changes: 4 additions & 0 deletions build/built-jar.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#Sun, 30 Oct 2016 13:30:07 +0530


D\:\\OSS\\Hacktoberfest\\Random-Text-Generator=
Binary file modified build/classes/random/text/generator/Gui$1.class
Binary file not shown.
Binary file added build/classes/random/text/generator/Gui$2.class
Binary file not shown.
Binary file modified build/classes/random/text/generator/Gui.class
Binary file not shown.
50 changes: 45 additions & 5 deletions src/random/text/generator/Gui.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;


public class Gui {
private RandomTextGenerator generator;
//private static final int DEFAULT_WORD_AMOUNT;

public Gui() {
init();
}
Expand All @@ -28,23 +34,57 @@ public void init() {
panel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
frame.add(panel);

JLabel label = new JLabel("Click the button on the bottom!");
label.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(label);

JLabel headerLabel = new JLabel("Click the button on the bottom!");
headerLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(headerLabel);

JComboBox<String> generatorSelectBox = new JComboBox<>();
generatorSelectBox.addItem("SpaceEfficientGenerator");
generatorSelectBox.addItem("SpeedEfficientGenerator");
generatorSelectBox.setAlignmentX(Component.RIGHT_ALIGNMENT);
panel.add(generatorSelectBox);

JTextArea inputTextArea = new JTextArea(5, 5);
inputTextArea.setText(RandomTextGenerator.getDefaultText());
inputTextArea.setAlignmentX(Component.CENTER_ALIGNMENT);
inputTextArea.setLineWrap(true);
inputTextArea.setWrapStyleWord(true);
panel.add(inputTextArea);

JTextArea outputTextArea = new JTextArea(5, 5);
outputTextArea.setText("Not randomly generated text.");
outputTextArea.setText("Result");
outputTextArea.setEditable(false);
outputTextArea.setAlignmentX(Component.CENTER_ALIGNMENT);
outputTextArea.setLineWrap(true);
outputTextArea.setWrapStyleWord(true);
panel.add(outputTextArea);

JButton generateTextButton = new JButton("Generate text");
generateTextButton.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(generateTextButton);

JLabel executionTimeLabel = new JLabel();
panel.add(executionTimeLabel);

generateTextButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String generatorType = (String)generatorSelectBox.getSelectedItem();
String inputText = inputTextArea.getText();
switch(generatorType){
case "SpeedEfficientGenerator":
generator = new SpeedEfficientGenerator(inputText);
break;
default:
generator = new SpaceEfficientGenerator(inputText);
}
long startTime = System.currentTimeMillis();
outputTextArea.setText(generator.generateText(200));
long stopTime = System.currentTimeMillis();
long elapsedTime = stopTime - startTime;
executionTimeLabel.setText("Time taken: " + elapsedTime + "ms");
}
});

frame.pack();
frame.setLocationRelativeTo(null);
Expand Down

0 comments on commit f7435e6

Please sign in to comment.