Draw a JPane in JFrame - java

I have search on internet and found a lot of information about drawing in Java. But when I add new JFrame class in Netbeans then I cannot add a own JPane in the JFrame. Hopefully somebody can help me with this issue/question.
Drawing the JPane is possible when I make a new JFrame in a class, but I would like to use the design view in Netbeans. That is not possible when I make a new JFrame.
I look forward to receiving an answer.

It looks like you made a JFrame instance yourself, and then tried to add a JPane(l) to it using NetBeans swing builder. This wont work. Try creating a new swing class using the swing builder and let netbeans make the Jframe.
Also, make sure you set a correct layout for your JFrame.
Also, like BenCole said, I think you mean a JPanel, not a JPane.

As an alternative, create your own top level container and add your designer panel to its content pane. Here's a simple example. In this way you can limit the use of the designer to a single panel, while you explore other layouts.

Related

Adding new objects to a jframe

I have seen a program and was hoping to replicate one feature of it in my program. The program is a flashcard app, and when you create a new deck it adds another section to the homescreeen. I will attach images. I would love some advice on how to do this.
One of possible way is to use correct layouts, here is examples. Using this you may dynamically add any components (labels, images, buttons etc.) into your JPanel (I recommend to use JPanel as main container of all components instead of JFrame that should be one and just contain your JPanel).

How to add combobox from source code along with drag and drop GUI Java NetBean

I'm starting using java with NetBeans IDE. I'm using drag and dop GUI, it's so easy to use, but I got a problem. I'm writing this code at the contructor:
JComboBox combobox=new JComboBox();
combobox.addItem("Apple");
combobox.addItem("Banana");
for(int i=1;i<=10;i++){
combobox.addItem(i);
}
just right above initComponents(); hoping that my new combobox will shown when I run the project, but it doesn't. Did I do something wrong? Thanks in advance
Yes, you are creating a JComboBox, and yes, you are adding items to it (numeric and int -- that's a problem, but that's a discussion for another day), but no, you're not showing any code where you add this newly created JComboBox to a component that is displayed in the GUI. To display a component in a Swing GUI, it must be created and added to a component that is ultimately displayed in a top-level window, in the "GUI".
So this begs the question, how do you add your created JComboBox to your GUI that you've created with drag and drop code? One way: you could add it to say a JPanel that's already in your GUI, but you will need to do this after initializing components, usually this means after the constructor calls initComponents(), and you'll also need to make sure that this JPanel uses a layout manager that makes it easy for it to accept new components (this means most of the layout managers except NetBean's default layout, GroupLayout).
There are other issues, such as whether or not the container holding your JComboBox is large enough to display it, but the best suggestion that I can give is for you to go through the Swing tutorials, and hit especially hard the layout manager section. You can find links to the Swing tutorials and other Swing resources here: Swing Info.

How do I create a JPanel and put it on a JFrame from another class?

I am trying to learn some Java GUI stuff. I am trying to create a special jPanel, which I have created in another class [it is a Panel with two labels on it] inside one class, and using a passed JFrame add it to that JFrame from the other class. I have been trying myJFrame.add(thePanel) and such, but it never seems to actually do anything. I have tried doubly making sure it is sset to visible, is at a reasonable location, ect.
I am trying to create the panel in one class, and .add it to the given jframe. There is probably something I am not understanding.
I am not the best at the java GUI stuff... so please be kind :) Any advice you can give would be wonderful.
I am using Netbeans. If I drag the special Panel I created over to my panel in the Design editor, it adds an instance of that panel, but I can't seem to make my own instance of this and have it appear.

Adding to a JPanel which uses GroupLayout

I would like to add components (JButton and JSpinner) to a JPanel which was created using Netbeans GUI builder. This panel uses GroupLayout, and I can't seem to use add() to add a component. Is there any way to either add something to a panel which has a GroupLayout, or change this panel to a FlowLayout?
It's definitely not an easy thing to do. It depends on where you want to add those components. I usually reserve an empty placeholder JPanel with GUI builder, and then add components to that panel, using whatever layout I want. However, this only works when you want to add components in one place. If they are scattered around the GUI, it may be not that easy.
Another options is to migrate to manual GUI creation, possibly using Netbeans-generated code as a starting point. But depending on how complex your GUI is, it may be a tedious work.
There is an option if you right-click the JPanel to change it to a FlowLayout (Set Layout). This fixed all the problems I was having.

Small Window Over Components

I already look at java library and dont know what to use to do this..
I already tryed JInternalFrame but thats not what I really want.. because it needs to be added to a JDesktopPanel right??
And in my program I have a JFrame with content pane using BorderLayout..
Then on borderlayout center I have a JTextArea, on borderlayout east I have a list.. and on borderlayout south I have a JPanel..
What I want is, when I do a certain action, it will pop up a "mini window" where I need to choose something.. u see?
and if I create JDesktopLane it will overlap what I have on the container..
the window will be made by my like a color chooser pallete , like a grid with colors.. and a label on top saying some text..
I just dont know how to make a "window" over the other components, and users can still drag over the frame, and interact with all the other components.. the jtextarea and such..
I guess you understood, thanks alot in advance!!
If u dont understand something please tell me, I really want to do this :)
Just dont know what to use..
Thanks again ;)
Have you tried JDialog?
It's because Jdialog are not component to be add in a JFrame, it's an independant thing running on it's own
if you use JDialog, the construct parameter parent indicate wich frame the JDialog is related to.
The typical class for this task is JWindow, a borderless top-level window that can be freely positioned. You could use SwingUtilities.getPointFromComponent to get the screen coordinates for a realized coordinate.
Top-level windows (JFrame, JDialog, JWindow) are not added to containers. They can get other windows as parent.
I dont want to use another JFrame.. that is kinda bad for code, its a small window with a simple function..
Structure your code so you can read it, others can read it, and you can debug it easily (the latter is a result from the first). A low class count is useless and -most of the time- contraproductive.
Why should another JFrame (or other window) be bad?
If you absolutely want to avoid opening top level windows (e.g. to avoid applet warning icons or to implement a special kind of user interface) you could use a JLayeredPane to add additional JPanels above your existing GUI elements.

Categories

Resources