Skip to content

8357176: java.awt javadoc code examples still use Applet API #25278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions src/java.desktop/share/classes/java/awt/BorderLayout.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -89,31 +89,39 @@
* the {@code CENTER} component may stretch both horizontally
* and vertically to fill any space left over.
* <p>
* Here is an example of five buttons in an applet laid out using
* Here is an example of five buttons in a window laid out using
* the {@code BorderLayout} layout manager:
* <p>
* <img src="doc-files/BorderLayout-1.gif" alt="Diagram of an applet
* <img src="doc-files/BorderLayout-1.png" alt="Diagram of a window
* demonstrating BorderLayout. Each section of the BorderLayout contains a
* Button corresponding to its position in the layout, one of: North, West,
* Center, East, or South." style="margin: 7px 10px;">
* Center, East, or South.">
* <p>
* The code for this applet is as follows:
* The code for this program is as follows:
*
* <hr><blockquote><pre>
* import java.awt.*;
* import java.applet.Applet;
* {@snippet lang='java':
* import java.awt.BorderLayout;
* import java.awt.Button;
* import java.awt.EventQueue;
* import java.awt.Frame;
*
* public class buttonDir extends Applet {
* public void init() {
* setLayout(new BorderLayout());
* add(new Button("North"), BorderLayout.NORTH);
* add(new Button("South"), BorderLayout.SOUTH);
* add(new Button("East"), BorderLayout.EAST);
* add(new Button("West"), BorderLayout.WEST);
* add(new Button("Center"), BorderLayout.CENTER);
* }
* public class BorderLayoutExample {
*
* public static void main(String[] args) throws Exception {
* EventQueue.invokeAndWait(() -> {
* Frame frame = new Frame("BorderLayout");
* frame.setLayout(new BorderLayout());
* frame.add(new Button("North"), BorderLayout.NORTH);
* frame.add(new Button("South"), BorderLayout.SOUTH);
* frame.add(new Button("East"), BorderLayout.EAST);
* frame.add(new Button("West"), BorderLayout.WEST);
* frame.add(new Button("Center"), BorderLayout.CENTER);
* frame.setSize(300, 300);
* frame.setVisible(true);
* });
* }
* }
* }
* </pre></blockquote><hr>
*
* @author Arthur van Hoff
* @see java.awt.Container#add(String, Component)
Expand Down
39 changes: 22 additions & 17 deletions src/java.desktop/share/classes/java/awt/FlowLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,36 @@
* <li>{@link #TRAILING TRAILING}
* </ul>
* <p>
* For example, the following picture shows an applet using the flow
* For example, the following picture shows a window using the flow
* layout manager (its default layout manager) to position three buttons:
* <p>
* <img src="doc-files/FlowLayout-1.gif"
* <img src="doc-files/FlowLayout-1.png"
* ALT="Graphic of Layout for Three Buttons"
* style="margin: 7px 10px;">
* >
* <p>
* Here is the code for this applet:
* Here is the code for this program:
*
* <hr><blockquote><pre>
* import java.awt.*;
* import java.applet.Applet;
* {@snippet lang='java':
* import java.awt.Button;
* import java.awt.EventQueue;
* import java.awt.FlowLayout;
* import java.awt.Frame;
*
* public class myButtons extends Applet {
* Button button1, button2, button3;
* public void init() {
* button1 = new Button("Ok");
* button2 = new Button("Open");
* button3 = new Button("Close");
* add(button1);
* add(button2);
* add(button3);
* public class FlowLayoutExample {
*
* public static void main(String[] args) throws Exception {
* EventQueue.invokeAndWait(() -> {
* Frame frame = new Frame("FlowLayout");
* frame.setLayout(new FlowLayout());
* frame.add(new Button("OK"));
* frame.add(new Button("Open"));
* frame.add(new Button("Close"));
* frame.pack();
* frame.setVisible(true);
* });
* }
* }
* </pre></blockquote><hr>
* }
* <p>
* A flow layout lets each component assume its natural (preferred) size.
*
Expand Down
101 changes: 47 additions & 54 deletions src/java.desktop/share/classes/java/awt/GridBagLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,15 @@
* left-to-right container and Figure 3 shows the layout for a horizontal,
* right-to-left container.
*
* <div style="margin:0 auto;width:680px;text-align:center;font-weight:bold">
* <div style="margin:0 auto;width:850px;text-align:center;font-weight:bold">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to keep the width small as it was?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt it - unless you make the font really small, maybe?
If you look at how it is today you'll see the buttons are packed together using Motif L&F
https://docs.oracle.com/en/java/javase/21/docs/api/java.desktop/java/awt/GridBagLayout.html
BTW I tried a couple of times to upload javadoc to cr.openjdk.org so I could show the new rendered javadoc but uploads failed ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW I tried a couple of times to upload javadoc to cr.openjdk.org so I could show the new rendered javadoc but uploads failed ...

I experienced some issues with uploading files to cr.openjdk.org some time ago; recently it worked well, however, the login isn't instantaneous. I uploaded the updated javadoc for PassFailJFrame yesterday.

Yeah, it would be helpful to look at the updated javadoc. If I have some time, I'll build updated javadoc and upload it…

* <div style="float:left">
* <p><img src="doc-files/GridBagLayout-1.gif"
* <p><img src="doc-files/GridBagLayout-1.png"
* alt="The preceding text describes this graphic (Figure 2)."
* style="margin: 7px 10px;">
* <p>Figure 2: Horizontal, Left-to-Right
* </div>
* <div style="float:right">
* <p><img src="doc-files/GridBagLayout-2.gif"
* <p><img src="doc-files/GridBagLayout-2.png"
* alt="The preceding text describes this graphic (Figure 3)."
* style="margin: 7px 10px;">
* <p>Figure 3: Horizontal, Right-to-Left
Expand Down Expand Up @@ -276,73 +276,66 @@
* <p>
* Here is the code that implements the example shown above:
*
* <hr><blockquote><pre>
* import java.awt.*;
* import java.util.*;
* import java.applet.Applet;
* {@snippet lang='java':
* import java.awt.Button;
* import java.awt.EventQueue;
* import java.awt.Frame;
* import java.awt.GridBagConstraints;
* import java.awt.GridBagLayout;
*
* public class GridBagEx1 extends Applet {
* public class GridBagLayoutExample {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sample indents each code block by 4 spaces, which is good.

*
* protected void makebutton(String name,
* GridBagLayout gridbag,
* GridBagConstraints c) {
* private static void addButton(String name,
* GridBagLayout gridbag,
* GridBagConstraints c,
* Frame frame) {
* Button button = new Button(name);
* gridbag.setConstraints(button, c);
* add(button);
* frame.add(button);
* }
*
* public void init() {
* GridBagLayout gridbag = new GridBagLayout();
* GridBagConstraints c = new GridBagConstraints();
* public static void main(String[] args) throws Exception {
* EventQueue.invokeAndWait(() -> {
* Frame frame = new Frame("GridBagLayout");
* GridBagLayout gridbag = new GridBagLayout();
* GridBagConstraints c = new GridBagConstraints();
* frame.setLayout(gridbag);
*
* setFont(new Font("SansSerif", Font.PLAIN, 14));
* setLayout(gridbag);
* c.fill = GridBagConstraints.BOTH;
* c.weightx = 1.0;
* addButton("Button1", gridbag, c, frame);
* addButton("Button2", gridbag, c, frame);
* addButton("Button3", gridbag, c, frame);
*
* c.fill = GridBagConstraints.BOTH;
* c.weightx = 1.0;
* makebutton("Button1", gridbag, c);
* makebutton("Button2", gridbag, c);
* makebutton("Button3", gridbag, c);
* c.gridwidth = GridBagConstraints.REMAINDER; //end row
* addButton("Button4", gridbag, c, frame);
*
* c.gridwidth = GridBagConstraints.REMAINDER; //end row
* makebutton("Button4", gridbag, c);
* c.weightx = 0.0; //reset to the default
* addButton("Button5", gridbag, c, frame); //another row
*
* c.weightx = 0.0; //reset to the default
* makebutton("Button5", gridbag, c); //another row
* c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
* addButton("Button6", gridbag, c, frame);
*
* c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
* makebutton("Button6", gridbag, c);
* c.gridwidth = GridBagConstraints.REMAINDER; //end row
* addButton("Button7", gridbag, c, frame);
*
* c.gridwidth = GridBagConstraints.REMAINDER; //end row
* makebutton("Button7", gridbag, c);
* c.gridwidth = 1; //reset to the default
* c.gridheight = 2;
* c.weighty = 1.0;
* addButton("Button8", gridbag, c, frame);
*
* c.gridwidth = 1; //reset to the default
* c.gridheight = 2;
* c.weighty = 1.0;
* makebutton("Button8", gridbag, c);
* c.weighty = 0.0; //reset to the default
* c.gridwidth = GridBagConstraints.REMAINDER; //end row
* c.gridheight = 1; //reset to the default
* addButton("Button9", gridbag, c, frame);
* addButton("Button10", gridbag, c, frame);
*
* c.weighty = 0.0; //reset to the default
* c.gridwidth = GridBagConstraints.REMAINDER; //end row
* c.gridheight = 1; //reset to the default
* makebutton("Button9", gridbag, c);
* makebutton("Button10", gridbag, c);
*
* setSize(300, 100);
* }
*
* public static void main(String args[]) {
* Frame f = new Frame("GridBag Layout Example");
* GridBagEx1 ex1 = new GridBagEx1();
*
* ex1.init();
*
* f.add("Center", ex1);
* f.pack();
* f.setSize(f.getPreferredSize());
* f.show();
* frame.pack();
* frame.setVisible(true);
* });
* }
* }
* </pre></blockquote><hr>
* }
*
* @author Doug Stein
* @author Bill Spitzak (original NeWS &amp; OLIT implementation)
Expand Down
44 changes: 26 additions & 18 deletions src/java.desktop/share/classes/java/awt/GridLayout.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1995, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1995, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -32,25 +32,33 @@
* lays out a container's components in a rectangular grid.
* The container is divided into equal-sized rectangles,
* and one component is placed in each rectangle.
* For example, the following is an applet that lays out six buttons
* For example, the following is a window that lays out six buttons
* into three rows and two columns:
*
* <hr><blockquote>
* <pre>
* import java.awt.*;
* import java.applet.Applet;
* public class ButtonGrid extends Applet {
* public void init() {
* setLayout(new GridLayout(3,2));
* add(new Button("1"));
* add(new Button("2"));
* add(new Button("3"));
* add(new Button("4"));
* add(new Button("5"));
* add(new Button("6"));
* {@snippet lang='java':
* import java.awt.Button;
* import java.awt.EventQueue;
* import java.awt.Frame;
* import java.awt.GridLayout;
*
* public class GridLayoutExample {
*
* public static void main(String[] args) throws Exception {
* EventQueue.invokeAndWait(() -> {
* Frame frame = new Frame("GridLayout");
* frame.setLayout(new GridLayout(3, 2));
* frame.add(new Button("1"));
* frame.add(new Button("2"));
* frame.add(new Button("3"));
* frame.add(new Button("4"));
* frame.add(new Button("5"));
* frame.add(new Button("6"));
* frame.setSize(200, 200);
* frame.setVisible(true);
* });
* }
* }
* </pre></blockquote><hr>
* }
* <p>
* If the container's {@code ComponentOrientation} property is horizontal
* and left-to-right, the above example produces the output shown in Figure 1.
Expand All @@ -59,13 +67,13 @@
*
* <div style="margin:0 auto;width:600px;text-align:center;font-weight:bold">
* <div style="float:left">
* <p><img SRC="doc-files/GridLayout-1.gif"
* <p><img SRC="doc-files/GridLayout-1.png"
* alt="Shows 6 buttons in rows of 2. Row 1 shows buttons 1 then 2.
* Row 2 shows buttons 3 then 4. Row 3 shows buttons 5 then 6.">
* <p>Figure 1: Horizontal, Left-to-Right
* </div>
* <div style="float:right">
* <p><img SRC="doc-files/GridLayout-2.gif"
* <p><img SRC="doc-files/GridLayout-2.png"
* alt="Shows 6 buttons in rows of 2. Row 1 shows buttons 2 then 1.
* Row 2 shows buttons 4 then 3. Row 3 shows buttons 6 then 5.">
* <p>Figure 2: Horizontal, Right-to-Left
Expand Down
Loading