I'm trying to build a web browser in java swing where i added new tab option by adding JTabbedPane. But i want to update my current tab title when i browse another web page or urls etc. I used WebView and WebEngine as html parser. I tried to update title name using WebEngine.getTitle()
it didn't work.
Here is my constructor:
public class Hello extends JFrame {
private JButton backButton = new JButton();
private JButton newPage;
private JButton forwardButton = new JButton();
private JFXPanel jfxPanel = new JFXPanel();
private JTextField locationTextField = new JTextField(100);
private JPanel fPane = new JPanel(new BorderLayout());
private JPanel totalPane = new JPanel(new BorderLayout());
private WebEngine engine;
JPanel buttonPanel = new JPanel(new BorderLayout());
public ArrayList<String> pageList = new ArrayList<>();
public ArrayList<String> historyList = new ArrayList<>();
static int value = 0;
static int inI = 0;
public ArrayList<String> title = new ArrayList<>();
public ArrayList<String> titleUrl = new ArrayList<>();
Hashtable<String, String> table;
History hist;
final JTabbedPane tb;
private GraphicsDevice device;
private JFrame frame;
public Hello() {
currentScene();
table = new Hashtable<>();
setResizable(true);
setTitle("Pooh");
ImageIcon img = new ImageIcon(Hello.class.getResource("/Panda-icon.png"));
setIconImage(img.getImage());
JPanel buttonPanel1 = new JPanel();
buttonPanel1.setBackground(Color.decode("#330300"));
backButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
BorderFactory.createRaisedBevelBorder();
actionBack();
System.out.println(value + " " + pageList.size() + " back");
}
});
backButton.setEnabled(true);
backButton.setBorderPainted(false);
buttonPanel1.add(backButton);
forwardButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (pageList.size() > value) {
value++;
}
System.out.println(value + "forwad");
actionForward();
}
});
forwardButton.setEnabled(true);
locationTextField.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
value++;
String tUrl = toURL(locationTextField.getText());
actionGo(locationTextField.getText());
}
}
});
locationTextField.setDragEnabled(true);
buttonPanel1.add(locationTextField, BorderLayout.EAST);
JPanel buttonPanel2 = new JPanel(new BorderLayout(5, 5));
buttonPanel2.setBackground(Color.decode("#330300"));
newPage = new JButton();
newPage.setIcon(new ImageIcon(Hello.class.getResource("/Plus.png")));
newPage.setPreferredSize(new Dimension(30,30));
newPage.setBorderPainted(false);
newPage.setBackground(Color.decode("#330300"));
newPage.setToolTipText("New Page");
newPage.setEnabled(true);
buttonPanel2.add(newPage,BorderLayout.CENTER);
buttonPanel.setBackground(Color.decode("#330300"));
buttonPanel.add(buttonPanel1, BorderLayout.WEST);
buttonPanel.add(buttonPanel2, BorderLayout.EAST);
JPanel lblBar = new JPanel(new BorderLayout());
lblBar.setPreferredSize(new Dimension(50,10));
JPanel jp = new JPanel();
jp.setLayout(new BorderLayout());
UIManager.put("TabbedPane.background",Color.decode("#330300"));
tb = new JTabbedPane();
tb.setUI(new CustomTabbedPaneUI());
tb.setForeground(Color.decode("#330300"));
**//creating initial tab with default name "Google" which i want to change when new urls are clicked**
tb.addTab("Google",fPane);
jp.add(new JLayer<JTabbedPane>(tb));
**//creating new tabs when button is clicked**
newPage.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
tb.addTab("New Tab",new JPanel());
**//a method which takes index of tabbed pane and returns a tabbed pane**
initTabComponent(inI+1);
inI++;
}
});
tb.setOpaque(true);
fPane.add(jfxPanel,BorderLayout.CENTER);
totalPane.add(buttonPanel, BorderLayout.NORTH);
totalPane.add(jp, BorderLayout.CENTER);
setExtendedState(JFrame.MAXIMIZED_BOTH);
setLayout(new BorderLayout());
getContentPane().add(totalPane,BorderLayout.CENTER);
getContentPane().add(lblStatus,BorderLayout.PAGE_END);
setPreferredSize(new Dimension(1024, 600));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
}
Here is my currentScene()
method:
private void currentScene() {
Platform.runLater(new Runnable() {
@Override
public void run() {
WebView view = new WebView();
engine = view.getEngine();
engine.createPopupHandlerProperty();
engine.titleProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, final String newValue) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Hello.this.setTitle(newValue);
if (newValue != null) {
table.put(newValue, tUrl);
title.add(newValue);
historyList.add(newValue);
pageList.add(tUrl);
}
}
});
}
});
engine.setOnStatusChanged(new EventHandler<WebEvent<String>>() {
@Override
public void handle(final WebEvent<String> event) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
lblStatus.setText(event.getData());
}
});
}
});
engine.locationProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> ov, String oldValue, final String newValue) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
titleUrl.add(newValue);
locationTextField.setText(newValue);
}
});
}
});
engine.getLoadWorker().workDoneProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, final Number newValue) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
progressBar.setValue(newValue.intValue());
}
});
}
});
engine.getLoadWorker()
.exceptionProperty()
.addListener(new ChangeListener<Throwable>() {
public void changed(ObservableValue<? extends Throwable> o, Throwable old, final Throwable value) {
if (engine.getLoadWorker().getState() == FAILED) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JOptionPane.showMessageDialog(
fPane,
(value != null)
? engine.getLocation() + "\n" + value.getMessage()
: engine.getLocation() + "\nUnexpected error.",
"Loading error...",
JOptionPane.ERROR_MESSAGE);
}
});
}
}
});
jfxPanel.setScene(new Scene(view));
}
});
}
You will want to call setTitleAt(...) on the JTabbedPane to change the title of the tab.
I prefer CloseableTabbedPane . It has methid addTab with tab title and icon You can download it here https://github.com/dimensionv/closeabletabbedpane