Hi there
I have started to create a very simple web browser, without any images or content except html text.
I have a Jbutton which will save a bookmark into an arraylist and I wounder if it is possible to get strings from the arraylist
into a JMenu for an example and make the strings clickable so when the user clicks the bookmark- the page is opened in the Jtextarea.
Is this possible or is there any other way to save and access bookmarks in a web browser?
Thanks for any help I can get =)
6 replies to this topic
#1
Posted 16 December 2011 - 05:02 AM
|
|
|
#2
Posted 16 December 2011 - 05:25 AM
You can create a JPanel which will hold the bookmakrs.
Give it a FlowLayout, so it will just fill up horizontally.
You can loop trough the list, and add JLabels to this jpanel.
To make them clickable you can add a MouseListener to each JLabel.
in the mouseclicked event you can do event.getSource() to retrieve the JLabel. And from that label you must get the URL and redirect ur browser.
If you want the user to let them display custom text on each bookmark instead of having the link on the jlabel you should create your own class which extends it.
An alternative is to use JButtons instead of labels - This may be easier to get the clicking code done... imo not much difference though.
The problem with a JButton is that it looks like a JButton :P with about 3-4 setters you can remove the borders and everything to not make it look like a JButton though.
Give it a FlowLayout, so it will just fill up horizontally.
You can loop trough the list, and add JLabels to this jpanel.
To make them clickable you can add a MouseListener to each JLabel.
in the mouseclicked event you can do event.getSource() to retrieve the JLabel. And from that label you must get the URL and redirect ur browser.
If you want the user to let them display custom text on each bookmark instead of having the link on the jlabel you should create your own class which extends it.
An alternative is to use JButtons instead of labels - This may be easier to get the clicking code done... imo not much difference though.
The problem with a JButton is that it looks like a JButton :P with about 3-4 setters you can remove the borders and everything to not make it look like a JButton though.
#3
Posted 16 December 2011 - 07:16 AM
okey thanks, will try that right now and will come back with results =)
---------- Post added at 04:16 PM ---------- Previous post was at 02:33 PM ----------
Okey been trying it and it (hopefully will work) but i feel kinda bad but i have totaly forgotten how to hava a JPanel into a message dialog.
I am typing the following:
My code is for the listener (which i guessed should call the popup):
[CODE public static class BookmarkLyss implements ActionListener
{
public void actionPerformed(ActionEvent ave)
{
JOptionPane.showConfirmDialog(bookmarkHolder, "Bookmarks:");
//Opens up a JPanel
bookmarkHolder = new JPanel();
bookmarkHolder.setLayout(new FlowLayout());
bookmarkHolder.setSize(500, 500);
// Iterate through the arraylist.
for (int x = 0; x < bookmarks.size(); x++)
{
//Set each string as a JLabel.
JLabel temp = new JLabel(bookmarks.get(x));
//Add a mouseListener to each label.
temp.addMouseListener(new MouseLyss());
//Add it onto the JPanel.
bookmarkHolder.add(temp);
}
}
}][/CODE]
---------- Post added at 04:16 PM ---------- Previous post was at 02:33 PM ----------
Okey been trying it and it (hopefully will work) but i feel kinda bad but i have totaly forgotten how to hava a JPanel into a message dialog.
I am typing the following:
JOptionPane.showConfirmDialog(bookmarkHolder, "Bookmarks:");also tried:
JOptionPane.showConfirmDialog(null, bookmarkHolder);
My code is for the listener (which i guessed should call the popup):
[CODE public static class BookmarkLyss implements ActionListener
{
public void actionPerformed(ActionEvent ave)
{
JOptionPane.showConfirmDialog(bookmarkHolder, "Bookmarks:");
//Opens up a JPanel
bookmarkHolder = new JPanel();
bookmarkHolder.setLayout(new FlowLayout());
bookmarkHolder.setSize(500, 500);
// Iterate through the arraylist.
for (int x = 0; x < bookmarks.size(); x++)
{
//Set each string as a JLabel.
JLabel temp = new JLabel(bookmarks.get(x));
//Add a mouseListener to each label.
temp.addMouseListener(new MouseLyss());
//Add it onto the JPanel.
bookmarkHolder.add(temp);
}
}
}][/CODE]
#4
Posted 16 December 2011 - 09:17 AM
Wow back up a bit. Your browser is not a (J)Frame, but only exists out of JOptionPanes?
#5
Posted 16 December 2011 - 12:09 PM
No it is a JFRame from the start heres the full program:
The problem is I dont remember if that (what i did) is the correct way since it didnt show anything except a empty Message Dialog
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class Browser {
static String urlAdress, text;
static JTextArea jta;
static JTextField jtf;
static ArrayList<String> bookmarks, history;
static int response;
static JMenuBar menuBar;
static JMenu bookmark, menuhistory, about;
static JMenuItem savebookmark;
static JPanel bookmarkHolder;
public static void main(String[]args)
{
bookmarks = new ArrayList<String>();
history = new ArrayList<String>();
urlAdress = "http://www.google.com";
//Set up the menu
/////////////////////////////////////////////////
menuBar = new JMenuBar(); //Create the menu bar.
bookmark = new JMenu("Bookmark");
menuhistory = new JMenu("History");
about = new JMenu("About");
savebookmark = new JMenuItem("Save bookmark");
savebookmark.addActionListener(new BookmarkLyss());
menuhistory.addActionListener(new BookmarkLyss());
bookmark.add(savebookmark);
menuBar.add(bookmark);
menuBar.add(menuhistory);
menuBar.add(about);
////////////////////////////////////////////////
//Set up the window
/////////////////////////////////////////////////
JFrame frame = new JFrame("Text Browser");
frame.setLayout(new BorderLayout());
//Set the content
JPanel jp = new JPanel();
jta = new JTextArea();
jtf = new JTextField( 20 );
JScrollPane jsp = new JScrollPane(jta);
JButton btnSearch = new JButton("Search");
JButton btnBookmarks = new JButton("Save Bookmark");
JButton btnHistory = new JButton("History");
JLabel lbl = new JLabel("URL");
//Add Content to the frame
jp.add(lbl);
jp.add(jtf);
jp.add(btnSearch);
jp.add(btnBookmarks);
jp.add(btnHistory);
frame.add(jp, BorderLayout.NORTH);
frame.add(jsp, BorderLayout.CENTER);
//Change content properties and listeners
jta.setEditable(false);
jtf.setFocusable(true);
btnSearch.addActionListener(new SearchLyss());
btnBookmarks.addActionListener(new BookmarksLyss());
btnHistory.addActionListener(new HistoryLyss());
//Window properties
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
///////////////////////////////////////////
frame.setJMenuBar(menuBar);
}
public static class SearchLyss implements ActionListener
{
public void actionPerformed(ActionEvent ave)
{
if (!jtf.getText().equals(""))
{
urlAdress = jtf.getText();
}
else
urlAdress = "http://www.google.com";
try
{
StringBuffer buffer = new StringBuffer();
URL url = new URL(urlAdress);
String line = "";
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
while ((line = br.readLine()) != null)
{
buffer.append(line + "\n");
}
jta.setText(buffer.toString());
} catch(MalformedURLException ex)
{
System.err.println(urlAdress + " is not a pareable URL");
} catch(IOException ioe)
{
System.err.println(ioe);
}
}
}
public static class BookmarksLyss implements ActionListener
{
public void actionPerformed(ActionEvent ave)
{
String input = jtf.getText();
int confirmbox = JOptionPane.showConfirmDialog(null, "Do you want to set: " + input + " as a bookmark?",
"Bookmark question", JOptionPane.YES_NO_OPTION);
if(confirmbox == JOptionPane.YES_OPTION)
{
bookmarks.add(input);
}
for(int x = 0; x < bookmarks.size(); x++)
System.out.println(bookmarks.get(x) + "\n");
}
}
public static class BookmarkLyss implements ActionListener
{
public void actionPerformed(ActionEvent ave)
{
JOptionPane.showConfirmDialog(bookmarkHolder, "Bookmarks:");
//Opens up a JPanel
bookmarkHolder = new JPanel();
bookmarkHolder.setLayout(new FlowLayout());
bookmarkHolder.setSize(500, 500);
// Iterate through the arraylist.
for (int x = 0; x < bookmarks.size(); x++)
{
//Set each string as a JLabel.
JLabel temp = new JLabel(bookmarks.get(x));
//Add a mouseListener to each label.
temp.addMouseListener(new MouseLyss());
//Add it onto the JPanel.
bookmarkHolder.add(temp);
}
}
}
public static class HistoryLyss implements ActionListener
{
public void actionPerformed(ActionEvent ave)
{
}
}
public static class MouseLyss extends MouseAdapter
{
public void mouseClicked(MouseEvent mev)
{
// Get the source label and gets the text.
JLabel tmp = (JLabel)mev.getSource();
String str = tmp.getText();
// Sets the saved text into the text field.
jtf.setText(str);
System.out.println("str: "+str); //Debugging purpose.
}
}
//Eventually a home button listener,
//which will let the user go to a specific home page.
}
The problem is I dont remember if that (what i did) is the correct way since it didnt show anything except a empty Message Dialog
#6
Posted 17 December 2011 - 04:02 AM
This is a simplified version of how I would do this:
I only made the bookmarks work, any other button or inputfield has no effect, but you get the idea.
I only made the bookmarks work, any other button or inputfield has no effect, but you get the idea.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;
public class Browser extends JFrame {
private JEditorPane htmlPane;
public Browser() throws IOException {
super("browser");
htmlPane = new JEditorPane(new URL("http://en.wikipedia.org/wiki/Main_Page"));
JScrollPane scrollPane = new JScrollPane(htmlPane);
add(scrollPane);
BookmarksMouseListener bookmarksMouseListener = new BookmarksMouseListener();
Bookmark googleBookmark = new Bookmark("Google", "http://www.google.com", bookmarksMouseListener);
Bookmark translateBookmark = new Bookmark("Translate", "http://en.wikipedia.org/wiki/Main_Page", bookmarksMouseListener);
Bookmark newsBookmark = new Bookmark("w3schools", "http://www.w3schools.com/", bookmarksMouseListener);
JPanel bookmarksPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
bookmarksPanel.add(googleBookmark);
bookmarksPanel.add(translateBookmark);
bookmarksPanel.add(newsBookmark);
JPanel commandPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
commandPanel.add(new JButton("previous"));
commandPanel.add(new JButton("next"));
commandPanel.add(new JButton("refresh"));
commandPanel.add(new JTextField(35));
commandPanel.add(new JButton("go"));
JPanel northPanel = new JPanel(new GridLayout(0, 1));
northPanel.add(commandPanel);
northPanel.add(bookmarksPanel);
add(northPanel, BorderLayout.NORTH);
setSize(800, 800);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
private class BookmarksMouseListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
try {
htmlPane.setPage(((Bookmark) e.getSource()).getUrl());
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException {
Browser browser = new Browser();
}
}
import javax.swing.*;
import java.awt.event.ActionListener;
public class Bookmark extends JButton {
private String url;
public Bookmark(String text, String url, ActionListener actionListener) {
super(text);
this.url = url;
setContentAreaFilled(false);
addActionListener(actionListener);
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
#7
Posted 17 December 2011 - 08:19 AM
Okey =) well i did solve it somewhat with JDialog instead of using a JPanel so when i click the button the JDialog pops up and the user sees the bookmarks, and when the user clicks it the dialog hides and the url is inputted into the url textfield so the only thing the user need to do is press search =). didnt now how to actually load the page aswell so :)
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









