I'm working on a simple web browser that has tabs. I'm having alot of trouble adding a textfield below the tabbedpane, exactly like it is in chrome. Heres what I have so far: public class Browser { private JFrame frame; private JPanel panelTop; private JEditorPane editor; private JScrollPane scroll; private JTextField field; private JButton button; private JButton home; private URL url; private JMenu file = new JMenu("File"); private JMenuItem exit = new JMenuItem("Exit"); private JMenuBar menuBar = new JMenuBar(); private ImageIcon nt = new ImageIcon("./Icons/newTab.jpg"); private ImageIcon cl = new ImageIcon("./Icons/close.png"); private JButton newTab = new JButton(cl); private JTabbedPane tabbedPane = new JTabbedPane(); private int tabCounter = 0; private Dimension dim = new Dimension(nt.getIconWidth()+2, nt.getIconHeight()+2); public Browser() { initComponents();
frame = new JFrame();
frame.setTitle("TeslaNet Browser");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,600);
frame.setJMenuBar(menuBar);
menuBar.add(file);
file.add(exit);
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});
panelTop = new JPanel();
frame.add(BorderLayout.NORTH, panelTop);
panelTop.add(field);
panelTop.add(button);
panelTop.add(home);
panelTop.add(newTab);
newTab.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
initComponents();
}
});
newTab.setToolTipText("New Tab");
newTab.setPreferredSize(dim);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
frame.add(topPanel);
topPanel.add(tabbedPane, BorderLayout.CENTER);
panelTop.add(scroll, BorderLayout.CENTER);
frame.setVisible(true);
}
private void initComponents()
{
try
{
url = new URL("http://www.reddit.com");
}
catch(MalformedURLException mal)
{
JOptionPane.showMessageDialog(null,mal);
}
try
{
editor = new JEditorPane(url);
editor.setEditable(false);
}
catch(IOException ioe)
{
JOptionPane.showMessageDialog(null,ioe);
}
scroll = new JScrollPane(editor, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
field = new JTextField(14);
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
field.setText(url.toString());
}
});
button = new JButton("Go");
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
editor.setPage(field.getText());
}
catch(IOException ioe)
{
JOptionPane.showMessageDialog(null, ioe);
}
}
});
home = new JButton("Home");
home.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
url = new URL("http://www.thissongissick.com");
field.setText(url.toString());
}
catch(MalformedURLException mal)
{
JOptionPane.showMessageDialog(null,mal);
}
try
{
editor.setPage(field.getText());
}
catch(IOException ioe)
{
JOptionPane.showMessageDialog(null, ioe);
}
}
});
JPanel tab = new JPanel();
JButton closeButton = new JButton(nt);
closeButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
tabbedPane.remove(tabbedPane.getTabCount()-1);
}
});
closeButton.setPreferredSize(dim);
closeButton.setToolTipText("Close");
JLabel tabLabel = new JLabel("Tab " + (++tabCounter));
System.out.print(tabbedPane.getSelectedIndex());
tab.setOpaque(false);
tab.add(tabLabel, BorderLayout.WEST);
tab.add(closeButton, BorderLayout.EAST);
tabbedPane.addTab(null, editor);
tabbedPane.setTabComponentAt(tabbedPane.getTabCount()-1, tab);
}
}
Thanks!