The documentation for Processing PApplet states
there's nothing to prevent you from embedding a PApplet into a JFrame ... If people want to use Swing, they can embed themselves as they wish.
However, I did not find any example how to do this, and when I have tried it, the result never worked well - with the code I have now my PApplet is embedded, I can even create a menu bar, but the applet does not react to resizing the JFrame. Also, the frame member of PApplet is null, therefore any articles I have found (like this one on Processing forum or Window Size and Full Screen in the Processing docs) regarding this do not work for me.
Here is what I have now, more or less copied from Using a Processing Sketch as a Java Component:
import processing.core.PApplet;
public class CurveEditorApplet extends PApplet {
public static void main(String args[]) {
new CurveFrame();
}
}
and
import javax.swing.*;
public class CurveFrame extends JFrame {
CurveFrame() {
setSize(600, 600);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBounds(20, 20, 600, 600);
Curve2D sketch = new Curve2D();
panel.add(sketch);
add(panel);
sketch.init();
sketch.frame = this;
setVisible(true);
setResizable(true);
}
}
How can I change it so that the applet is resized when resizing the main window?
Related
I'm new to Java Swing (and fairly new to Java in general) and I've been messing around with Swing for a while, but after making some changes my panels stopped displaying and I don't know why.
When using isDisplayable() on my JPanel object it returns false. After more investigation my program does not seem to display graphics at all.
Even a simple piece of code doesn't work for me anymore:
public class window extends JFrame {
window(){
setBounds(100,100,1200,700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.black);
setVisible(true);
}
public static void main(String[] args) throws IOException{
new window();
}
I have no idea what I changed for this to happen, but all I get is a blank a completely blank window.
Any help is appreciated a lot!
See the code comments.
import java.awt.Color;
import javax.swing.*;
public class BlackWindow extends JFrame {
BlackWindow(){
setBounds(100,100,400,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// sets the frame black, not the content pane
//setBackground(Color.BLACK);
getContentPane().setBackground(Color.BLACK);
setVisible(true);
}
public static void main(String[] args) {
new BlackWindow();
}
}
This example also speaks to a few general principles we follow when using Swing's JFrame:
Don't extend the JFrame class, just use an instance of one.
Don't do anything significant to the coloring or styling of a frame, instead color (for example) a JPanel and add it to the frame.
this is my first question on stackoverflow so pls don't be so hard with me (and my English), I'll try my best asking a good question.
The problem is about what the title suggests -> setting the background of a JTextpane (specifically setting it transparent). As the setBackground-method takes a color argument, the way i went was with an alpha value = 0. This leads to the following visual artifacts:
example picture of the visual artifacts when running the code below
I will describe it in detail now, say what i've already tried and then post a minimal example for everyone to be able to recreate the bug easily.
I have a JFrame (myFrame) holding a JPanel (Test()) in which i paint. The background of this JPanel is set to green as you can see in the code. I did this to differ between the background of the TextPane and the actual background of the panel.
We also have a JTextPane inputfield -> which takes user input (it displays what you write). This is held by a JScrollPane scrollpane.
So the Target (meaning what i try to achieve): is a User input field, which is transparent, but still displays the text the user puts in. I tried to achieve this by setting the background of the inputfield and the scrollpane to a transparent color.
I would really appreciate it, if you don't just type something like (you need to setOpague(false) for ....) if you don't know exactly what you're talking about, because i nearly tried everything i can think of and read every post i could find in the internet about the problem. I will post some of them at the end. So...
What i already tried:
The usual way i found while searching for something like "How to make TextPane transparent" was to setOpague(false) at itself as well as the scrollpane and the viewport of the scrollpane. 1. Reading about what the method actually does i don't think that is a proper solution. 2. setting Opague(false) on these three leads to everything being invisible so, that's not good. Packing the frame again after setting the background. Either nothing was fixed or the components became completely invisible.
I also tried: setBackground(null), setting the Background of just the inputfield (just every single component and every possible combination amongst the three (scrollpane, viewport, inputfield)), mixing setOpague(true)/setOpague(false)/setBackground(...) in every way i could think of. Overriding the paintComponent method of the textpane and scrollpane seemed like a good approach, but i did not come very far with it.
So here is the code:
public class Test extends JPanel {
JTextPane inputField = new JTextPane();
JScrollPane scrollpane = new JScrollPane(inputField);
#Override
public void paint(Graphics g) {
super.paint(g);
inputField.setBounds(10,10,100,100);
scrollpane.setBounds(10,10,100,100);
}
public Test(){
this.setOpaque(true);
this.setBackground(Color.GREEN);
inputField.setBounds(10,10,100,100);
scrollpane.setBounds(10,10,100,100);
inputField.setBackground(new Color(0,0,0,0));
scrollpane.setBackground(new Color(0,0,0,0));
scrollpane.getViewport().setBackground(new Color(0,0,0,0));
this.add(scrollpane);
}
public static void main(String[] args) {
JFrame myFrame = new JFrame();
myFrame.add(new Test());
myFrame.pack();
myFrame.setSize(640,480);
myFrame.setPreferredSize(new Dimension(640,480));
myFrame.setVisible(true);
}
}
And here some links to posts I read which describe similar problems:
Java - Transparent JScrollPane
Transparent JEditorPane in a JScrollPane over a background JPanel
setOpaque(true/false); Java
I would really appreciate if someone can help me with the problem or even only suggest me an alternative solution. I'm writing a little chat-program atm for a project for my university and i think transparent message fields are a neat idea. I will try to answer here as fast as i can. Thx in advance.
Do not use transparent color as background - kind of hard to delete with it (e.g. when component is being repaint (and opaque)).
Do not use setBounds (unless using null LayoutManager). In below example I used setPreferredSize but still better to correctly use LayoutManager (I am a bit lazy, and lot of work to do at the moment).
public class Test extends JPanel {
JTextPane inputField = new JTextPane();
JScrollPane scrollpane = new JScrollPane(inputField);
public Test(){
this.setOpaque(true);
this.setBackground(Color.GREEN);
inputField.setPreferredSize(new Dimension(300, 100));
inputField.setOpaque(false);
scrollpane.setOpaque(false);
scrollpane.getViewport().setOpaque(false);
this.add(scrollpane);
}
public static void main(String[] args) {
JFrame myFrame = new JFrame();
myFrame.add(new Test());
myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
myFrame.setPreferredSize(new Dimension(640,480));
myFrame.pack();
myFrame.setVisible(true);
}
}
The whole code of main should be called on the EDT (e.g. SwingUtilities.invokeLater) since it is manipulating swing components but that is not part of the question (and not that relevant here since setVisible is the last command - components not being displayed while changed).
Your program gave me some errors when launching, occasionally. Make sure to run a swing GUI from the EDT (invokeLater()).
The line that is causing your issue is:
scrollpane.getViewport().setBackground(new Color(0,0,0,0));
Also - is there some reason you are setting bounds manually instead of using a layout manager?
Maybe I didn't understand your question, or you want to have an image in the background - but could you not just set the color of your text area to be the same color as your JPanel?
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
public class Test extends JPanel {
JTextPane inputField = new JTextPane();
JScrollPane scrollpane = new JScrollPane(inputField);
public Test(){
this.setBackground(Color.GREEN);
scrollpane.setPreferredSize(new Dimension(300, 300));
inputField.setBackground(Color.GREEN);
this.add(scrollpane);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame myFrame = new JFrame();
myFrame.add(new Test());
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setPreferredSize(new Dimension(640,480));
myFrame.pack();
myFrame.setVisible(true);
}
});
}
}
I want do design a simple login format and in order to do so I want two JTextFields for Username/Password and a Login Button. The Login button is display as expected but when I add the JTextField, nothing shows in my JFrame. Would be nice if someone could help a beginner out...
Here's my code (I know it's ugly but this is just a "code sketch"):
package bucketlistpackage;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GameFrame extends JFrame {
public GameFrame(String title) {
super(title); //sets title of frame
startFrame(); //sets details of main frame
final Container logincont = getContentPane(); //creates content pane
JFrame loginframe = new JFrame();
usernameField(loginframe);
loginButton(loginframe);
logincont.add(loginframe);
}
private void usernameField(JFrame loginframe) {
JTextField usernameF = new JTextField("Username", 1);
usernameF.setBounds(50, 50, 50, 20);
loginframe.add(usernameF);
usernameF.setVisible(true);
}
private void startFrame() {
this.setSize(1000, 1000);
this.setVisible(true);
}
private void loginButton(Container cont) {
JButton loginB = new loginButton();
loginB.setSize(300, 150);
loginB.setText("Login");
cont.add(loginB);
}
}
The problem lies on how you are adding component to one another in your case.
You are adding a JFrame to a Container, when in all case it should be the other way around.
The other problem is that you are not using Layouts to manage the components positions on the JFrame.
Another problem as well is that you are not refreshing the windows after adding all the stuff on it.
A bit of a resume on how Java works with native UIs:
Java creates a new thread for the UI. So if you open the debugger you will see AWT threads as well as the main threads and others. This means that you have to manage this in a correct way, because after the application starts SWING and the functions you determine for reactions will lay the ground on how it will behave. Your main thread will die, but the visual threads will keep active.
If you are just starting to program I would encourage you to practice a bit more native java language before moving to SWING or AWT. This libraries can be really painful and tricky to use.
The other thing is SWING library follows a hierarchy for the components:
JFrame > JPanels > Components
In your code you have already worked with all of them but in a disorganized way. JFrame is the main application window, where the graphics will be displayed (can also be a Canvas or whatever class you want to use for that matter). JPanels are organizers, you can use different layouts to organize whatever is inside it. And finally the Components are well... everything! A component can be a JTextField, but it can also be a JPanel, or JButton.
The idea with SWING is to create multiple Panels and organize the end components inside them, using the help of the different layouts to see whats the best approach to make them attractive in many different window sizes.
Finally, if you are using Eclipse, there is a plugin called WindowBuilder which might help you. I don't recommend you using it if you are very new to Java since it will make you depend a lot on it instead of learning how to actually code with SWING.
Hope this helps!!!
Btw, to fix the code above I would do something like this:
public GameFrame(String title) {
super(title); //sets title of frame
startFrame(); //sets details of main frame
final Container logincont = getContentPane(); //creates content pane
logincont.setLayout(new BorderLayout());
usernameField(logincont, BorderLayout.NORTH);
loginButton(logincont, BorderLayout.CENTER);
this.revalidate();
this.repaint();
}
private void usernameField(Container loginframe, String direction) {
JTextField usernameF = new JTextField("Username");
// usernameF.setBounds(50, 50, 50, 20);
loginframe.add(usernameF, direction);
usernameF.setVisible(true);
}
private void startFrame() {
this.setSize(1000, 1000);
this.setVisible(true);
}
private void loginButton(Container cont, String direction) {
JButton loginB = new JButton();
loginB.setSize(300, 150);
loginB.setText("Login");
cont.add(loginB, direction);
}
I have drawn a rectangle using a JPanel
My main objective is to store my Requirement Engineering chapter into a JPanel or a JFrame
import java.awt.*;
import javax.swing.*;
class RequirementEngineering extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent();
g.drawRect(10,10,60,60);
g2.drawString("Feasibility study", 20, 20); //rectangle is my main objective, I will look after the string later
}
public static void main(String[] args)
{
}
}
how do I display the JPanel? I know, JApplet doesn't require a main method, but how do we represent JPanel in main() method?
I have this doubt for long, and other posts are confusing me further, Could I have a direct question
My main question being "How to add JFrame to JPanel" pertaining my current coding
thanks in advance
see if you need to use a Window based app, you can do as:
JPanel customPanel = new RequirementEngineering();
JFrame frame = new JFrame("my window");
frame.getContentPane().add(customPanel );
frame.setSize(300,200);
frame.setVisible(true);
If you need in Applet,
public class JAppletExample extends JApplet {
public void init() {
Container content = getContentPane();
JPanel customPanel = new RequirementEngineering();
content.add(customPanel );
}
And you can run it using appletViewer or in any Web Browser such as IE.
I'm using the Intelij idea platform.
I have the following code:
package GUI.test;
import javax.swing.*;
public class Frame extends JFrame{
Frame(){}
public void main (String[] args){
new Frame();
}
}
I expected to see a JFrame after compiling this code, but was nothing appeared. What kind of problem can it be?
Frames are not visible by default--use the
setVisible(true);
method in order to display frames.
You also might want to take a look at other options such as
setSize(int width, int height);
method to resize a frame,
setLocation(int xLoc, int yLoc);
to move the frame, and
setTitle(String title);
to set the title of the component.
Aside, it is good practice to use a variable to hold components so they can be manipulated when needed.
You might want to add this
Frame()
{
setVisible(true);
setSize(100,100);
}
package GUI.test;
import javax.swing.*;
public class Frame extends JFrame{
private myFrame;
public Frame()
{
myFrame = new JFrame("put a title here"); //title not necessary but it's there if you want it
myFrame.setSize(400,400); // sets the window size
myFrame.setVisible(true); // toggles the frame to be visible inside the window
myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); // this will terminate the VM once the *last* JFrame is closed, so you can have multiple frames open and just close one
}
public void main (String[] args){
new Frame();
}
}
Thanks a lot for answering. I wrote an example without details. I have added: setVisible(true);
setSize(100,100);
to a class constructor. But i havent seen form jet.
Have to say about one feature. I'm not running method main in class Frame, i'm compiling this class Frame. In my previous exersises with java i allways run main method, but when i tried to work with GUI form and used swing library, ability to run method main have dissapiared.
If you haven't cought what i mean, i'd send a screenshot.