I am new to Java, started learning swing and have a problem with resizing a JPanel inside a JFrame. I am following this tutorial:
http://vincentramdhanie.blogspot.com/2009/11/cardlayout-with-netbeans.html
because ultimately I am learning each of the different layouts and have come to the CardLayout now. In the above, there is a JPanel being used for a status panel. That is what I want to do as well, but when I drag a JPanel onto my blank JForm it takes up all the space and I don't see any resizing handles for it like I would if I were using a .NET panel. Changing preferredSize in the properties window also does nothing. What am I missing here? I feel like a complete noob for asking such a basic question but I really can't find any way to resize this thing.. :-|
EDIT:
I forgot to mention; I am using NetBeans IDE
You can't resize the JComponent because you've select CardLayout. The CardLayout can holds/manages one or more components that share the same display space.
What you need to read documentation and good tutorials.
Related
I´m creating a TileMap out of JLabels and my Problem is that the last Tile is not placed right.
For some reason the last JLablel is on a wrong position. I think this Image:
and the Code explains what my problem is.
Btw. I am using BlueJ (maybe there is a bug in it, that I dont know)
Thanks for help!!!
//create Frame
this.setSize((size+2)*imageSize+16,(size+2)*imageSize+39);
this.addKeyListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setBackground(Color.black);
this.setVisible(true);
//create MapJLabels
for(int x = 0;x<size;x++){
for(int y = 0;y<size;y++){
mapJLabels[x][y] = new JLabel();
this.add(mapJLabels[x][y]);
mapJLabels[x][y].setSize(imageSize,imageSize);
mapJLabels[x][y].setLocation((x+1)*imageSize,(y+1)*imageSize);
mapJLabels[x][y].setVisible(true);
}
}
The code is attempting to set the size/location of the component. This implies you are using a null layout. Don't. Swing was designed to be used with layout managers. You should use the GridLayout. Read the section from the Swing tutorial on Layout Managers for more information.
You create an empty JLabel. By default a JLabel is transparent. I'm not sure now the picture you posted is created from the code you posted. I don't see how the grid is populated with images etc.
Swing components are visible by default so there is no need for the setVisible(true)
Your code attempts to set the size of the frame. Again, don't. When using layout managers you just pack() the frame and the size will be calculated automatically.
Your code attempts to leave space between the frame and labels. Don't. If you want extra space you can add an EmptyBorder to the panel. Read the section from the Swing tutorial on How to Use Borders
You code is using a KeyListener. Don't. Swing was designed to be used with Key Bindings
Components should be added to the frame BEFORE the frame is made visible.
The posted code looks reasonable, but since we can't see where you change the background of the labels or where you add icons to the labels, we don't know what the rest of your logic is doing. The problem could be there.
If you need more help after fixing the above problems then post a proper minimal, reproducible example that demonstrates your new problem.
Sorry if this has been posted before, but I cannot seem to find any good info that helps me, or I just don't understand other answers enough to help me as I just started programming GUIS.
I wrote a program that has various Items in their own (sub)Jpanel(jtextfields. combobox's, buttons etc.) and all the sub Jpanels in a main jpanel inside a jframe. My goal is to center and stack each sub JPanel on top of one another, so that when the user resizes the window each item stays centered and stacked on top of one another. (when I stacked I don't mean layered where one pane is in front of another, rather stacking the panes like a sandwich so to speak) My panes just move with the default flowlayout and I hope to stop that.
I have seen BoxLayout but like I said, I am new to GUIS and I am not sure if I can apply the BoxLayout to Jpanels.
First off, what you're referring to is a JPanel, not a JPane
Secondly,
JPanel panel = new JPanel(new BorderLayout());
BorderLayout is probably your best bet. For more information on layouts, check out
http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html#choosing
And by the way, NetBeans GUI Builder is probably your best friend as an introductory GUI programmer.
There are easy ways to design your User Interface in Java Swing or the last One JAVAFX. In Swing, there is a eclipse plugin called Swing Windows Builder, there you can easily build your UI by dragging and dropping and As for the JAVAFX, there is Nice and free IDE called Intelli IDE CE, it has also built-in UI designer called Oracle JavaFx Scene Builder. Go for the JavaFx and speed up your development.
I've made a java application in netbeans and am wondering how to have the size of the jframe half the width and height of the computer resolution and also having the components comply with this change. I tried putting code and it did make the frame half the height of the computer resolution but my components, such as buttons and textfields, stopped showing. How can I achieve this? Thanks.
(EDITED)
Set the JFrame's layout manager to GridLayout. In the properties window of the GridLayout itself (select in the navigator window) set columns to 1 and rows to 2. This should give you what you want and you won't have to get into the code.
This is the key code being called within the initComponents() method of your JFrame subclass (created by NetBeans) but it is important to understand where it is:
getContentPane().setLayout(new java.awt.GridLayout(2, 1));
I love Netbeans but you do have to understand the basics.
Good luck with your project. Swing is an awesome toolset that was way ahead of it's time.
As usual in this situation the key is using the right combination of layout managers for your containers. You're probably using NetBeans generated code (something I recommend you avoid until you are very comfortable with Swing coding), and it's probably having you use GroupLayout, a fine layout, but one that might not behave as well as you'd like on resizing components. I suggest that you go through the layout manager tutorial and try to nest JPanel containers and play with different layouts that re-size well such as GridLayout, GridBagLayout and BorderLayout to try to create the best layout that can re-size well.
Is there a way to resize a control, a JTextfield for example, at runtime in java? I want my textfield to have the resize cursors (just like when you point your cursor on the corner of a window) and will be able to resize on runtime. Ive read on the internet that vb6 and C# have those capabilities, is there anything for java? A sample code or a link to a good tutorial will be very much appreciated. Thank you.
It sounds like you are trying to implement a component editor, such as the GUI editors available in popular programing IDEs. The essential feature is a handle, a graphical object that can be selected and dragged to change the geometry. GraphPanel is a simple example of an object drawing program that illustrates many of the required techniques.
That depends on the Layout of the JTextField's container. A good tutorial is available at http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
For a quick and cheap solution you could use a JSplitPane component, with the JTextField to be resized in the left side, and an empty JPanel in the right side. By default a JSplitPane is decorated with a border and a divider, but you can remove these by setting an empty border.
I am using java to design a GUI; I inserted a java.awt.List on a JPanel component. My program works correctly but as showing this panel I see delay and displacement problem in showing List component. I can use javax.swing.JList (Swing component) on Jpanel and result is OK. Is there a constant problem in using awt components on swing components? Can I solve it to use java.awt.List on my JPanel?
thank you all....
sajad
nobody know how your code works, you have to show us for better/valuable answer for your problem(s), but there still not good idea Mixing heavy and light components or here maybe this post can help you
EDIT: bacis tutorial about JPanel with JList layed by some of LayoutManagers, examples for that on Java2s.com inc. Layout