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;
}
}
// 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:

With the Code below, how would i add a "Back" Button to the bottom of the...
Why when executed my frame does not contain what i have added to it? Here is my code import java.awt.Panel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.JPanel; @SuppressWarnings("serial") public class AddressBook1 extends JFrame implements ActionListener { /** * */ JLabel name = new JLabel("Name: "); JLabel address = new JLabel("Address: "); JLabel phone = new JLabel("Phone: "); JLabel email = new JLabel("Email: ");...
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 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 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 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 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. 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...
Can you fix my error? I created a program that changes labels every second in Java. But, it does not change. And, it will starts with second label. This is also an error. import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; public class Map2 extends JFrame{ JPanel panel; JLabel pic; Timer tm; int x = 0; String ly = "<html> " + "<br> <font size='10' color='red'> <b> 111111111 <b/> </font>...
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 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...