Java swing controls and JxBrowser page in the same frame? - java

I can't seem to find any information on this, is it possible to use JxBrowser to display a webpage and java swing controls within the same frame and if so, how? My current code is just one to display a window with google maps currently as I keep trying things and deleting them.
public static void main(String[] args) {
Browser browser = new Browser();
BrowserView view = new BrowserView(browser);
JFrame frame = new JFrame("Google Maps");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.setSize(1500,1000);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
browser.loadURL("http://maps.google.com");
}

BrowserView that you embed into a frame is a Swing component. You can add other Swing components in the same way:
public static void main(String[] args) {
Browser browser = new Browser();
BrowserView view = new BrowserView(browser);
JFrame frame = new JFrame("Google Maps");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
// Let's add a button
frame.add(new JButton("My Button"), BorderLayout.SOUTH);
frame.setSize(1500,1000);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
browser.loadURL("http://maps.google.com");
}

Related

Text Box does not immediately pop up in JFrame [duplicate]

I have a simple Swing GUI. (and not only this, all swing GUI I have written). When run it, it doesn't show anything except blank screen, until I resize the main frame, so every components have painted again, and I can show them.
Here is my simple code :
public static void main(String[] args) {
JFrame frame = new JFrame("JScroll Pane Test");
frame.setVisible(true);
frame.setSize(new Dimension(800, 600));
JTextArea txtNotes = new JTextArea();
txtNotes.setText("Hello World");
JScrollPane scrollPane = new JScrollPane(txtNotes);
frame.add(scrollPane);
}
So, my question is : how can when I start this class, the frame will appear all components I have added, not until I resize frame.
Thanks :)
Do not add components to JFrame after the JFrame is visible (setVisible(true))
Not really good practice to call setSize() on frame rather call pack() (Causes JFrame to be sized to fit the preferred size and layouts of its subcomponents) and let LayoutManager handle the size.
Use EDT (Event-Dispatch-Thread)
call JFrame#setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) as said by #Gilbert Le Blanc (+1 to him) or else your EDT/Initial thread will remain active even after JFrame has been closed
Like so:
public static void main(String[] args) {
//Create GUI on EDT Thread
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("JScroll Pane Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea txtNotes = new JTextArea();
txtNotes.setText("Hello World");
JScrollPane scrollPane = new JScrollPane(txtNotes);
frame.add(scrollPane);//add components
frame.pack();
frame.setVisible(true);//show (after adding components)
}
});
}
Your simple code is missing a few things.
You have to invoke SwingUtilities to put the Swing components on the event dispatch thread.
You should call the setDefaultCloseOperation on the JFrame.
You have to call the JFrame methods in the correct order. The setSize or pack method is called, then the setVisible method is called last.
public class SimpleFrame implements Runnable {
#Override
public void run() {
JFrame frame = new JFrame("JScroll Pane Test");
JTextArea txtNotes = new JTextArea();
txtNotes.setText("Hello World");
JScrollPane scrollPane = new JScrollPane(txtNotes);
frame.add(scrollPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(800, 600));
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new SimpleFrame());
}
}

Change Jframe content without opening a new frame

I have a program that opens up a Jframe. inside this Jframe, I have a button that opens another class that has a Jframe too. Is there any way to to get the new class, and new gui opened in the same frame as the first one, and remove the existing content in the first frame?
This is the first class:
public class FirstFrame {
public FirstFrame() {
JFrame frame = new JFrame();
frame.setTitle("Title");
frame.setLayout(new BorderLayout());
frame.setSize(500,350);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Code for a button, that has an actionlistener wich opens the next
//class
frame.setVisible(true);
frame.setResizable(false);
}
public static void main(String[] args){
new FirstFrame();
}
}
The second class is almost like this one. The difference between the two frames is that they have different content.
When I run my program, everything works just fine, but I get two windows. Also, it says in the task manager that the two frames are running as two different programs.
Is there a way to make the content and even the name of the frame change, instead of having to deal with two different frames?
You can change the content pane of your JFrame. Let's say you have two JComponents called a and b. You initialize your frame:
JFrame frame = new JFrame();
frame.setContentPane(a);
Then you can just call
frame.setContentPane(b);
to change the content. You may also need to call revalidate and repaint to refresh the frame.
In the below example, pressing the button changes the content of JFrame from single JButton a to JLabel b. You can use any JComponent instead, such as JPanel containing multiple JComponents.
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setTitle("Title");
frame.setSize(500, 350);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton a = new JButton("button A");
frame.setContentPane(a);
frame.setVisible(true); // calling setVisible after content pane has been set to refresh a frame
a.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JComponent b = new JLabel("label B");
frame.setContentPane(b);
frame.revalidate();
}
});
}

A Simple GUI Interface for Java

doing some self study for a simple GUI interface for Java. Tried to code a simple interface. Here is the below code:
public void MainPanel() {
JFrame frame = new JFrame();
frame.setTitle("Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane mpt = new JTabbedPane();
mpt.addTab("Intro", new IntroPanel());
mpt.addTab("Catalogue", new CataloguePanel());
mpt.addTab("Order", new OrderPanel());
mpt.addTab("Track", new TrackPanel());
JPanel main = new JPanel();
main.setBackground(Color.white);
JLabel label1 = new JLabel("Intro");
main.add(label1);
frame.add(main);
frame.add(mpt);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
The problem I am currently facing is: If I put frame.add(main) infront of frame.add(mpt), only the tabs will appear but the the label intro. If I put frame.add(mpt) infront of frame.add(main). Intro will appear but not the tabs.
Why is this happening? Why are they overlapping each other? I did it the same way as some tutorial but to no avail..
Try giving it some other layout.
eg. frame.setLayout(new FlowLayout());
Info:
According to Java Naming Conventions your method's name should be like public void mainPanel()

Java Swing : why must resize frame, so that can show components have added

I have a simple Swing GUI. (and not only this, all swing GUI I have written). When run it, it doesn't show anything except blank screen, until I resize the main frame, so every components have painted again, and I can show them.
Here is my simple code :
public static void main(String[] args) {
JFrame frame = new JFrame("JScroll Pane Test");
frame.setVisible(true);
frame.setSize(new Dimension(800, 600));
JTextArea txtNotes = new JTextArea();
txtNotes.setText("Hello World");
JScrollPane scrollPane = new JScrollPane(txtNotes);
frame.add(scrollPane);
}
So, my question is : how can when I start this class, the frame will appear all components I have added, not until I resize frame.
Thanks :)
Do not add components to JFrame after the JFrame is visible (setVisible(true))
Not really good practice to call setSize() on frame rather call pack() (Causes JFrame to be sized to fit the preferred size and layouts of its subcomponents) and let LayoutManager handle the size.
Use EDT (Event-Dispatch-Thread)
call JFrame#setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) as said by #Gilbert Le Blanc (+1 to him) or else your EDT/Initial thread will remain active even after JFrame has been closed
Like so:
public static void main(String[] args) {
//Create GUI on EDT Thread
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("JScroll Pane Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea txtNotes = new JTextArea();
txtNotes.setText("Hello World");
JScrollPane scrollPane = new JScrollPane(txtNotes);
frame.add(scrollPane);//add components
frame.pack();
frame.setVisible(true);//show (after adding components)
}
});
}
Your simple code is missing a few things.
You have to invoke SwingUtilities to put the Swing components on the event dispatch thread.
You should call the setDefaultCloseOperation on the JFrame.
You have to call the JFrame methods in the correct order. The setSize or pack method is called, then the setVisible method is called last.
public class SimpleFrame implements Runnable {
#Override
public void run() {
JFrame frame = new JFrame("JScroll Pane Test");
JTextArea txtNotes = new JTextArea();
txtNotes.setText("Hello World");
JScrollPane scrollPane = new JScrollPane(txtNotes);
frame.add(scrollPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(800, 600));
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new SimpleFrame());
}
}

Java applet scrollbar

[Thanks for the answers. This comes for you http://www.youtube.com/watch?v=Vo0Cazxj_yc ]
This might and should be a very easy question, but i could not find a solution.
I have a java applet, and i want a vertical scrollbar so that i can load thousands of buttons into the applet and use the scrollbar to see buttons down on the applet.
Buttons are used to select items. if button is pressed, the item is selected.
When i load buttons, all of them are shown on one screen, squeezed together to fit the screen in width and height (~1000px,~1000px). Below code is a portion of my program. Please comment.
JFrame frame = new JFrame();
NameClassifier nameClassifier = new NameClassifier();
JScrollPane scrollPane = new JScrollPane(nameClassifier);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
frame.add(scrollPane);
frame.getContentPane().add(nameClassifier);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
System.out.println("exiting");
import java.awt.*;
import javax.swing.*;
class ManyButtons {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
NameClassifier nameClassifier = new NameClassifier();
JScrollPane scrollPane = new JScrollPane(nameClassifier);
scrollPane.setVerticalScrollBarPolicy(
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
frame.add(scrollPane);
// nameClassifier has already been added to the scroll pane.
//frame.getContentPane().add(nameClassifier);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
System.out.println("exiting");
}
});
}
}
class NameClassifier extends JPanel {
NameClassifier() {
super(new GridLayout(0,10,2,2));
for (int ii=1; ii<=1000; ii++) {
add(new JButton("Button " + ii));
}
}
}
I think you want to use a Wrap Layout.
Don't add anything directly to the frame, so
frame.add(scrollPane);
is wrong.
Add things to the content pane. probably
scrollPane.add(nameClassifier);
frame.getContentPane().add(scrollPane);
btw, that is one pretty gui design. :)

Categories

Resources