I've created a Browser
class that's working as it should, with 4 JButton
s.
What I'm working on now is understanding object-oriented programming a bit more (mostly been working with Python), so I thought it would be good practice to try and split up my Browser
class into 2-3 classes.
For example, I would like someone to be able to change the JPanel
and JFrame
without having to look through methods like newURL
that has nothing to do with the GUI.
I've tried splitting it up, but I just don't understand OOP well enough to make the code work with more than one class. Any ideas on where I can read up on this, or maybe you could give me a few tips on what I can/can't move around in the code?
import java.awt.BorderLayout;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.*;
@SuppressWarnings("serial")
public class Browser extends JFrame {
private JTextField addressBar;
private JEditorPane display;
private JButton button;
private JPanel panel;
private ArrayList<String> list = new ArrayList<>();
private ArrayList<String> history = new ArrayList<>();
private String URL = new String();
private int i = -1;
private int j = -1;
private JButton backward;
private JButton forward;
private JFrame frame;
public Browser() {
frame = new JFrame("My Browser");
panel = new JPanel();
addressBar = new JTextField("Enter a URL");
addressBar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
URL = e.getActionCommand().toString();
newURL(URL);
}
});
display = new JEditorPane();
display.setEditable(false);
display.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
URL = e.getURL().toString();
newURL(URL);
}
}
});
addButton("Close").addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() != null) {
closeAction();
}
}
});
forward = addButton("Forward");
forward.setEnabled(false);
forward.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() != null) {
forwardAction();
}
}
});
backward = addButton("Back");
backward.setEnabled(false);
backward.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() != null) {
backwardAction();
}
}
});
addButton("History").addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() != null) {
historyAction();
}
}
});
setframe();
}
private void setframe() {
frame.add(addressBar, BorderLayout.NORTH);
frame.add(panel, BorderLayout.SOUTH);
frame.add(new JScrollPane(display), BorderLayout.CENTER);
frame.setSize(400, 400);
frame.setVisible(true);
}
private JButton addButton(String name) {
button = new JButton(name);
panel.add(button);
return button;
}
private void setBackward(JButton button) {
if (i > 0) {
button.setEnabled(true);
} else {
button.setEnabled(false);
}
}
private void setForward(JButton button) {
if (i < list.size() - 1) {
button.setEnabled(true);
} else {
button.setEnabled(false);
}
}
private void loadURL(String URL) {
try {
display.setPage(URL);
addressBar.setText(URL);
} catch (Exception e) {
list.remove(list.size() - 1);
history.remove(history.size()-1);
JOptionPane.showMessageDialog(null, "fel länk");
}
}
private void checklist() {
if (i != j) {
list.subList(i, list.size()).clear();
j = i;
}
}
private void newURL(String URL) {
i++;
j++;
checklist();
loadURL(URL);
history.add(URL);
list.add(URL);
setBackward(backward);
setForward(forward);
}
private void backwardAction() {
i--;
setForward(forward);
setBackward(backward);
loadURL(list.get(i));
}
private void forwardAction() {
i++;
setBackward(backward);
setForward(forward);
loadURL(list.get(i));
}
private void closeAction() {
System.exit(0);
}
private void historyAction() {
String html = new String();
for (String link : history) {
html = html + "<a href=\"" + link + "\">" + link + "</a>\n";
}
html = "<html><body" + html + "</body></html>";
JEditorPane ep = new JEditorPane("text/html", html);
ep.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) {
loadURL(e.getURL().toString());
}
}
});
ep.setEditable(false);
JOptionPane.showMessageDialog(null, ep);
}
public static void main(String[] args) {
Browser chrome = new Browser();
chrome.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Some things to know if you've copied the code: The functionality of the buttons are in the methods jbuttonAction
. setJButton
helps decide when the forward
and backwards
buttons should be activated, the JButton
history
should have clickable links and if you want to visit a website, write: https://www.website.com
.
I've thought about maybe splitting it up like this: 1 class with: setframe()
and addbutton()
. 1 class with: buttonAction()
(all 4 methods), setButton()
(both methods) and 1 class with: main
, newURL()
, loadURL()
.
Any help on how this structure could be implemented in my code would be greatly appreciated. Should the constructor still look the same? Which class should extend which?
EDIT: Follow-up: Follow up: utilizing OOP, is the use of static OK here?