Question

With the Code below, how would i add a "Back" Button to the bottom of the...

With the Code below, how would i add a "Back" Button to the bottom of the history page so that it takes me back to the main function and continues to work?

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.GridLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

import javax.swing.BoxLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextField;

public class RecipeFinder {

public static void main(String[] args) {

//JFrame

JFrame frame = new JFrame("The Recipe Finder");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setPreferredSize(new Dimension(400,400));

//JPanel

JPanel entry = new JPanel();

entry.setLayout(new BoxLayout(entry, BoxLayout.Y_AXIS));

//JLabel

JLabel insert = new JLabel("Insert Food/Ingredients");

entry.add(insert);

entry.setBackground(Color.red);

//JTextField(s)

JTextField foodOne = new JTextField();

foodOne.setMaximumSize(new Dimension(400,48));

foodOne.setMinimumSize(new Dimension(400,48));

entry.add(foodOne);

JTextField foodTwo = new JTextField();

foodTwo.setMaximumSize(new Dimension(400,48));

foodTwo.setMinimumSize(new Dimension(400,48));

entry.add(foodTwo);

JTextField foodThree = new JTextField();

foodThree.setMaximumSize(new Dimension(400,48));

foodThree.setMinimumSize(new Dimension(400,48));

entry.add(foodThree);

//JButton(s)

JButton addTextBox = new JButton("Add");

List<JTextField> listOfTextFields = new ArrayList<JTextField>();

addTextBox.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

JTextField newBox = new JTextField();

newBox.setMaximumSize(new Dimension(400,48));

newBox.setMinimumSize(new Dimension(400,48));

entry.add(newBox);

entry.revalidate();

frame.invalidate();

listOfTextFields.add(newBox);

}

});

JButton search = new JButton("Search Recipes");

search.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

try {

String search = foodOne.getText().toString().trim();

String search2 = foodTwo.getText().toString().trim();

String search3 = foodThree.getText().toString().trim();

search = search.replaceAll(" ", "+");

search2 = search2.replaceAll(" ", "+");

search3 = search3.replaceAll(" ", "+");

String searchFull = "Recipes+with+" + search + "+" + search2 + "+" + search3;

for(JTextField x:listOfTextFields) {

searchFull=searchFull + "+" + x.getText().toString().trim();

}

System.out.println(searchFull);

addRecipeToFile(searchFull);

String url = createURL(searchFull);

//String url = "https://www.google.com/search?source=hp&ei=LBOYXNDCJIO35gKnjafACw&q="+searchFull+"&btnK=Google+Search&oq="+searchFull+"&gs_l=psy-ab.3..35i39j0i20i263j0l8.1444.2813..2967...2.0..0.82.591.9......0....1..gws-wiz.....0..0i131i67j0i67j0i131.EWAGiq0bzRM#btnK=Google%20Search";

java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));;

}

catch (java.io.IOException ex) {

System.out.println(ex.getMessage());

}

}

});

JButton recents = new JButton("History");

recents.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

JFrame history = new JFrame("Recipe History");

history.setLayout( new GridLayout(0, 1));

ArrayList<String> recipHistory = loadHistory();

for(String str : recipHistory) {

JButton button = new JButton(str.replace("+"," "));

button.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

String url = createURL(str);

try {

java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));

} catch (IOException e1) {

}

}

});

JPanel panel = new JPanel();

panel.add(button);

panel.setBackground(Color.red);

history.add(panel);

}

history.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

history.setPreferredSize(new Dimension(400,400));

history.pack();

history.setVisible(true);

}

});

JPanel buttonPanel = new JPanel();

buttonPanel.add(addTextBox);

buttonPanel.add(search);

buttonPanel.add(recents);

buttonPanel.setBackground(Color.blue);

JPanel rootPanel = new JPanel();

rootPanel.setLayout(new BorderLayout());

rootPanel.add(entry, BorderLayout.CENTER);

rootPanel.add(buttonPanel, BorderLayout.SOUTH);

JScrollPane rootEntry = new JScrollPane(rootPanel);

rootEntry.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

rootEntry.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

//FrameWork

frame.add(rootEntry);

frame.pack();

frame.setVisible(true);

}

private static void addRecipeToFile(String query) {

try {

FileWriter writer = new FileWriter(new File("history.txt"), true);

writer.write(query + "\n");

writer.close();

}catch (IOException e) {

e.printStackTrace();

}

}

private static String createURL(String searchFull) {

String url = "https://www.google.com/search?source=hp&ei=LBOYXNDCJIO35gKnjafACw&q="+searchFull+"&btnK=Google+Search&oq="+searchFull+"&gs_l=psy-ab.3..35i39j0i20i263j0l8.1444.2813..2967...2.0..0.82.591.9......0....1..gws-wiz.....0..0i131i67j0i67j0i131.EWAGiq0bzRM#btnK=Google%20Search";

return url;

}

private static ArrayList<String> loadHistory(){

ArrayList<String> history = new ArrayList<String>();

try {

Scanner scanner = new Scanner(new File("history.txt"));

while (scanner.hasNext()) {

history.add(scanner.nextLine());

}

scanner.close();

}catch (FileNotFoundException e) {

}

return history;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Check the bold mark for the change

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

import javax.swing.BoxLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextField;

public class RecipeFinder

{

static JPanel entry;

static JFrame frame;

static List<JTextField> listOfTextFields;

static JTextField foodOne, foodTwo, foodThree;

public static void main(String[] args)

{

//JFrame

frame = new JFrame("The Recipe Finder");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setPreferredSize(new Dimension(400,400));

//JPanel

entry = new JPanel();

entry.setLayout(new BoxLayout(entry, BoxLayout.Y_AXIS));

//JLabel

JLabel insert = new JLabel("Insert Food/Ingredients");

entry.add(insert);

entry.setBackground(Color.red);

//JTextField(s)

foodOne = new JTextField();

foodOne.setMaximumSize(new Dimension(400,48));

foodOne.setMinimumSize(new Dimension(400,48));

entry.add(foodOne);

foodTwo = new JTextField();

foodTwo.setMaximumSize(new Dimension(400,48));

foodTwo.setMinimumSize(new Dimension(400,48));

entry.add(foodTwo);

foodThree = new JTextField();

foodThree.setMaximumSize(new Dimension(400,48));

foodThree.setMinimumSize(new Dimension(400,48));

entry.add(foodThree);

//JButton(s)

JButton addTextBox = new JButton("Add");

listOfTextFields = new ArrayList<JTextField>();

addTextBox.addActionListener(new ActionListener()

{

@Override

public void actionPerformed(ActionEvent e)

{

JTextField newBox = new JTextField();

newBox.setMaximumSize(new Dimension(400,48));

newBox.setMinimumSize(new Dimension(400,48));

entry.add(newBox);

entry.revalidate();

frame.invalidate();

listOfTextFields.add(newBox);

}

});

JButton search = new JButton("Search Recipes");

search.addActionListener(new ActionListener()

{

@Override

public void actionPerformed(ActionEvent e)

{

try

{

String search = foodOne.getText().toString().trim();

String search2 = foodTwo.getText().toString().trim();

String search3 = foodThree.getText().toString().trim();

search = search.replaceAll(" ", "+");

search2 = search2.replaceAll(" ", "+");

search3 = search3.replaceAll(" ", "+");

String searchFull = "Recipes+with+" + search + "+" + search2 + "+" + search3;

for(JTextField x:listOfTextFields)

{

searchFull=searchFull + "+" + x.getText().toString().trim();

}

System.out.println(searchFull);

addRecipeToFile(searchFull);

String url = createURL(searchFull);

//String url = "https://www.google.com/search?source=hp&ei=LBOYXNDCJIO35gKnjafACw&q="+searchFull+"&btnK=Google+Search&oq="+searchFull+"&gs_l=psy-ab.3..35i39j0i20i263j0l8.1444.2813..2967...2.0..0.82.591.9......0....1..gws-wiz.....0..0i131i67j0i67j0i131.EWAGiq0bzRM#btnK=Google%20Search";

java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));;

}

catch (java.io.IOException ex)

{

System.out.println(ex.getMessage());

}

}

});

JButton recents = new JButton("History");

recents.addActionListener(new ActionListener()

{

String str = "";

JFrame history;

@Override

public void actionPerformed(ActionEvent e)

{

history = new JFrame("Recipe History");

history.setLayout(new BorderLayout());

ArrayList<String> recipHistory = loadHistory();

//for(str : recipHistory)

for(int x = 0; x < recipHistory.size(); x++)

{

str = recipHistory.get(x);

JButton button = new JButton(str.replace("+"," "));

JButton back = new JButton("BACK");

button.addActionListener(new ActionListener()

{

@Override

public void actionPerformed(ActionEvent e)

{

String url = createURL(str);

try

{

java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));

}

catch (IOException e1)

{

}

}

});

back.addActionListener(new ActionListener()

{

@Override

public void actionPerformed(ActionEvent e)

{

history.setVisible(false); //you can't see me!

history.dispose(); //Destroy the JFrame object

}

});

JPanel panel = new JPanel();

panel.setLayout(new BorderLayout());

panel.add(button, BorderLayout.NORTH);

panel.add(back, BorderLayout.SOUTH);

panel.setBackground(Color.red);

history.add(panel, BorderLayout.CENTER);

}

history.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

history.setPreferredSize(new Dimension(400,400));

history.pack();

history.setVisible(true);

}

});

JPanel buttonPanel = new JPanel();

buttonPanel.add(addTextBox);

buttonPanel.add(search);

buttonPanel.add(recents);

buttonPanel.setBackground(Color.blue);

JPanel rootPanel = new JPanel();

rootPanel.setLayout(new BorderLayout());

rootPanel.add(entry, BorderLayout.CENTER);

rootPanel.add(buttonPanel, BorderLayout.SOUTH);

JScrollPane rootEntry = new JScrollPane(rootPanel);

rootEntry.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

rootEntry.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

//FrameWork

frame.add(rootEntry);

frame.pack();

frame.setVisible(true);

}

private static void addRecipeToFile(String query)

{

try

{

FileWriter writer = new FileWriter(new File("history.txt"), true);

writer.write(query + "\n");

writer.close();

}

catch (IOException e)

{

e.printStackTrace();

}

}

private static String createURL(String searchFull)

{

String url = "https://www.google.com/search?source=hp&ei=LBOYXNDCJIO35gKnjafACw&q="+searchFull+"&btnK=Google+Search&oq="+searchFull+"&gs_l=psy-ab.3..35i39j0i20i263j0l8.1444.2813..2967...2.0..0.82.591.9......0....1..gws-wiz.....0..0i131i67j0i67j0i131.EWAGiq0bzRM#btnK=Google%20Search";

return url;

}

private static ArrayList<String> loadHistory()

{

ArrayList<String> history = new ArrayList<String>();

try

{

Scanner scanner = new Scanner(new File("history.txt"));

while (scanner.hasNext())

{

history.add(scanner.nextLine());

}

scanner.close();

}

catch (FileNotFoundException e)

{

}

return history;

}

}

Sample Output:

The Recipe Finder × E Recipe History Insert FoodIngredients Recipes with Add Search Recipes History BACK

Add a comment
Know the answer?
Add Answer to:
With the Code below, how would i add a "Back" Button to the bottom of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label;...

    I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Fall_2017 {    public TextArea courseInput;    public Label textAreaLabel;    public JButton addData;    public Dialog confirmDialog;    HashMap<Integer, ArrayList<String>> students;    public Fall_2017(){    courseInput = new TextArea(20, 40);    textAreaLabel = new Label("Student's data:");    addData = new JButton("Add...

  • How can i make the java class seperate into an MVC pattern? public class ChatView implements Acti...

    How can i make the java class seperate into an MVC pattern? public class ChatView implements ActionListener {    public static void main(String[] args)    {        JFrame chatFrame = new JFrame();        JFrame chatFrame1 = new JFrame();        JFrame chatFrame2 = new JFrame();               JPanel chatPanel = new JPanel();        JPanel chatPanel1 = new JPanel();        JPanel chatPanel2 = new JPanel();        chatFrame.setContentPane(chatPanel);        chatFrame1.setContentPane(chatPanel1);        chatFrame2.setContentPane(chatPanel2);        chatFrame.setLayout(new FlowLayout());        chatFrame1.setLayout(new FlowLayout());        chatFrame2.setLayout(new FlowLayout());        JTextField chatWrite = new JTextField();        JTextField chatWrite1 = new JTextField();        JTextField chatWrite2 = new JTextField();        JTextArea chatDisplay...

  • Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button...

    Modify Java Code below: - Remove Button Try the Number -Instead of Try the Number button just hit enter on keyboard to try number -Remove Button Quit import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; // Main Class public class GuessGame extends JFrame { // Declare class variables private static final long serialVersionUID = 1L; public static Object prompt1; private JTextField userInput; private JLabel comment = new JLabel(" "); private JLabel comment2 = new JLabel(" "); private int...

  • I have currently a functional Java progam with a gui. Its a simple table of contacts...

    I have currently a functional Java progam with a gui. Its a simple table of contacts with 3 buttons: add, remove, and edit. Right now the buttons are in the program but they do not work yet. I need the buttons to actually be able to add, remove, or edit things on the table. Thanks so much. Here is the working code so far: //PersonTableModel.java import java.util.List; import javax.swing.table.AbstractTableModel; public class PersonTableModel extends AbstractTableModel {     private static final int...

  • I have been messing around with java lately and I have made this calculator. Is there...

    I have been messing around with java lately and I have made this calculator. Is there any way that I would be able to get a different sound to play on each different button 1 - 9 like a nokia phone? Thank you. *SOURCE CODE* import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class Calculator extends JFrame { private final Font BIGGER_FONT = new Font("monspaced",Font.PLAIN, 20); private JTextField textfield; private boolean number = true; private String equalOp = "="; private...

  • Swing File Adder: Build a GUI that contains an input file, text box and Infile button....

    Swing File Adder: Build a GUI that contains an input file, text box and Infile button. It also must contain and output file, text box and Outfile button. Must also have a process button must read the infile and write to the outfile if not already written that is already selected and clear button. It must pull up a JFile chooser that allows us to brows to the file and places the full path name same with the output file.  Program...

  • import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.BorderFactory; import javax.swing.border.Border; public class Q1 extends JFrame {...

    import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.swing.BorderFactory; import javax.swing.border.Border; public class Q1 extends JFrame {    public static void createAndShowGUI() {        JFrame frame = new JFrame("Q1");        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        Font courierFont = new Font("Courier", Font.BOLD, 40);        Font arialFont = new Font("Arial", Font.BOLD, 40);        Font sansFont = new Font("Sans-serif", Font.BOLD, 20);        JLabel label = new JLabel("Enter a word"); label.setFont(sansFont);        Font font1 = new Font("Sans-serif", Font.BOLD, 40);       ...

  • tart from the following code, and add Action Listener to make it functional to do the...

    tart from the following code, and add Action Listener to make it functional to do the temperature conversion in the direction of the arrow that is clicked. . (Need about 10 lines) Note: import javax.swing.*; import java.awt.GridLayout; import java.awt.event.*; import java.text.DecimalFormat; public class temperatureConverter extends JFrame { public static void main(String[] args) {     JFrame frame = new temperatureConverter();     frame.setTitle("Temp");     frame.setSize(200, 100);     frame.setLocationRelativeTo(null);     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     frame.setVisible(true); }    public temperatureConverter() {     JLabel lblC = new...

  • please help me debug this Create a GUI for an application that lets the user calculate...

    please help me debug this Create a GUI for an application that lets the user calculate the hypotenuse of a right triangle. Use the Pythagorean Theorem to calculate the length of the third side. The Pythagorean Theorem states that the square of the hypotenuse of a right-triangle is equal to the sum of the squares of the opposite sides: alidate the user input so that the user must enter a double value for side A and B of the triangle....

  • I want to make the JText area scrollable.. but my result doesn't work in that way....

    I want to make the JText area scrollable.. but my result doesn't work in that way. help me plz. package m5; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class TextAnalyzer extends JFrame implements ActionListener {    //Initializes component variables for the frame       //For text typing area    JLabel type = new JLabel("Type sentences in the box below.", JLabel.CENTER);    JTextArea txt = new JTextArea(10, 10);    JPanel txt1 = new JPanel();       //For Statistic data...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT