Automatically size JPanel inside JFrame
You can set a layout manager like BorderLayout and then define more specifically, where your panel should go:
MainPanel mainPanel = new MainPanel();
JFrame mainFrame = new JFrame();
mainFrame.setLayout(new BorderLayout());
mainFrame.add(mainPanel, BorderLayout.CENTER);
mainFrame.pack();
mainFrame.setVisible(true);
This puts the panel into the center area of the frame and lets it grow automatically when resizing the frame.
how to do auto-resizing drawings in JPanel?
Provide positions and sizes as a proportion of the width and height of the panel. Whenever the panel is resized, the rendering engine will schedule a call to the paintComponent()
method and the rectangle will be drawn proportionally. E.G.
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.*;
public class DrawRect extends JPanel {
@Override
protected void paintComponent(Graphics g) {
int w = getWidth();
int h = getHeight();
g.drawRect(w/10, h/10, w/2, h/2);
}
/* A custom component should give the layout manager hints as to
its preferred size. */
@Override
public Dimension getPreferredSize() {
return new Dimension(200,200);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
DrawRect panel = new DrawRect();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
How to set JPanel height to auto
I use JScrollPane to make JFrame scrollable so the entire form is visible.
package helpdesk;
import java.awt.*;
import javax.swing.*;
/**
*
* @author Joko Wandiro
*/
public class Layout extends Base {
JPanel panel_form_edit = new JPanel();
FlowLayout layout_form_edit_outer = new FlowLayout(FlowLayout.LEFT);
JPanel panel_form_edit_outer = new JPanel();
// Form - Edit
JLabel label_name_update = new JLabel("Name", JLabel.LEFT);
JTextField input_id_update = new JTextField();
JTextField input_name_update = new JTextField();
JButton btn_update = new JButton("Update");
public Layout() {
setLookAndFeel();
setExtendedState(JFrame.MAXIMIZED_BOTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Main Layout
GridLayout layout = new GridLayout(5, 1, 10, 10);
setLayout(layout);
// Form - Edit
label_name_update.setPreferredSize(new Dimension(400, 20));
input_name_update.setPreferredSize(new Dimension(400, 20));
input_id_update.setVisible(false);
GridLayout layout_form_edit = new GridLayout(4, 1, 10, 10);
panel_form_edit.setLayout(layout_form_edit);
panel_form_edit.add(input_id_update);
panel_form_edit.add(label_name_update);
panel_form_edit.add(input_name_update);
panel_form_edit.add(btn_update);
panel_form_edit_outer.setLayout(layout_form_edit_outer);
panel_form_edit_outer.add(panel_form_edit);
add(panel_form_edit_outer);
// Set window or jframe scrollable
Container container = getContentPane();
JScrollPane scroll = new JScrollPane(container);
setContentPane(scroll);
setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Layout app = new Layout();
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
);
} catch (Exception exc) {
// ignore error
}
}
}
controlling JPanels size according to JFrame Resize
You must test this code:
if (jpanelDarkGray.getHeight() != jpanelLightGray.getHeight()) {
if (jpanelMidSep.getHeight() == 8) {
jpanelMidSep.setPreferredSize(new Dimension(jpanelMidSep.getWidth(), 9));
}
if (jpanelMidSep.getHeight() == 9) {
jpanelMidSep.setPreferredSize(new Dimension(jpanelMidSep.getWidth(), 8));
}
}
Related Topics
Eclipse No Tests Found Using Junit 5 Caused by Noclassdeffounderror for Launcherfactory
Remove End of Line Characters from Java String
How to Solve Maven 2.6 Resource Plugin Dependency
How to Send Text to Textarea in Div Element Using Selenium
Status Expected:<200> But Was:<404> in Spring Test
Rounded Swing Jbutton Using Java
How to Solve Gradle Error Cannot Find Symbol
Every Digits in Number of a Range Should Be Is Divisible by N
Getting Url Parameter in Java and Extract a Specific Text from That Url
Kafka: How to Delete Records from a Topic Using Java API
How to Save a File Downloaded With Httpclient into a Specific Folder
Subscript and Superscript a String in Android
Java to Jackson Json Serialization: Money Fields
How to Clean Project Cache in Intellij Idea Like Eclipse'S Clean
How to Select One Object from the List of Objects
How to Read a File, Reverse the Order, and Write Reverse Order