Home > Enterprise >  How to show the Button in the bottom of the JScrollPane Inside a JFrame
How to show the Button in the bottom of the JScrollPane Inside a JFrame

Time:03-08

I am working on an application in which I am trying to show a banner in a JFrame. Inside the JFrame, I using a JScrollbar which is using JEditorPane to display content of a HTML page.

    public static JFrame ShowBanner() throws IOException {
        int width = 0;
        int height = 0;
        String urlText = null;
        String ScrnResol = getScreenResolution();
        String[] ScreenResolution = ScrnResol.split(",");
        try {
            width = Integer.parseInt(ScreenResolution[0]);
            height = Integer.parseInt(ScreenResolution[1]);
        } catch (NumberFormatException nfe) {
            logger.error("NumberFormatException: "   nfe.getMessage());
        }
        //Creating Frame
        frmOpt = new JFrame("Banner");
        frmOpt.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frmOpt.setPreferredSize(new Dimension(width, height));
        frmOpt.setUndecorated(true);
        frmOpt.setBackground(new Color(1.0f, 1.0f, 1.0f, 0.1f));
        frmOpt.setVisible(true);
        frmOpt.setAlwaysOnTop(true);
        frmOpt.setLocationRelativeTo(null);
        //bringToForeground(getHWND(frmOpt));
        JPanel panel = new JPanel();
        LayoutManager layout = new FlowLayout();
        panel.setLayout(layout);
        JEditorPane jEditorPane = new JEditorPane();
        jEditorPane.setEditable(false);
        try {
            
            String urlText=IOUtils.toString(BringUpFrame.class.getClassLoader().getResourceAsStream("banner.htm"));
            jEditorPane.setContentType("text/html");
            jEditorPane.setText(urlText);
        } catch (Exception e) {
            logger.error("Exception while executing showBanner: {}", e);
            jEditorPane.setContentType("text/html");
            jEditorPane.setText("<html>Page not found.</html>");
        }
        JScrollPane jScrollPane = new JScrollPane(jEditorPane);
        scrollPane.setColumnHeaderView(createButton());
        jScrollPane.setPreferredSize(new Dimension(width, height));
        panel.add(jScrollPane);
        frmOpt.add(panel);
        frmOpt.pack();
        frmOpt.setVisible(true);
        return frmOpt;

   }

   private static JPanel createButton() {
        JPanel panel = new JPanel();
        panel.setBackground(new Color(1.0f, 1.0f, 1.0f, 0.1f));
        JButton button = new JButton("Close");
        panel.add(button);
        return panel;
    }

Currently the view is looking like this:

enter image description here

As of Now button is displayed on the Top. What I want to do is to put the button in the bottom of the screen. I have tried some layout (BoxLayout and BorderLayout) but view was as per the expection. I can understand that it will be a small change but I am not having much exp in Swing Programming.

Can someone please suggest how can I achieve that.

Edit:

Tried suggested changes:

JScrollPane jScrollPane = new JScrollPane(jEditorPane);
panel.add(jScrollPane);
frmOpt.add(panel,BorderLayout.CENTER);
frmOpt.add(createButton(),BorderLayout.PAGE_END);

but it's not coming as expected. Button and html page in JscrollPane are displaying completely separated and Page is also not coming over complete screen. enter image description here

Thanks,

CodePudding user response:

The default layout manager for [the content pane of] JFrame is BorderLayout. The center component takes up all the remaining space. Hence BorderLayout ignores the preferred size of the center component. FlowLayout, on the other hand, respects the preferred size of its contained components. Since you have maximized the JFrame its center component will take up almost the entire screen so you just need to add the JScrollPane to the BorderLayout center.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ForManish {
    private void createAndDisplayGui() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setUndecorated(true);
        frame.add(createScrollPane(), BorderLayout.CENTER);
        frame.add(createButtonPanel(), BorderLayout.PAGE_END);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frame.setVisible(true);
    }

    private JScrollPane createScrollPane() {
        JEditorPane editorPane = new JEditorPane("text/html", "<h1 align=\"center\">Checkout unavailable");
        editorPane.setBackground(new Color(1.0f, 1.0f, 1.0f, 0.1f));
        JScrollPane scrollPane = new JScrollPane(editorPane);
        scrollPane.setBorder(BorderFactory.createLineBorder(new Color(1.0f, 1.0f, 1.0f, 0.1f)));
        return scrollPane;
    }

    private JPanel createButtonPanel() {
        JPanel buttonPanel = new JPanel();
        buttonPanel.setBackground(new Color(1.0f, 1.0f, 1.0f, 0.1f));
        JButton closeButton = new JButton("Close");
        buttonPanel.add(closeButton);
        return buttonPanel;
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new ForManish().createAndDisplayGui());
    }
}
  • Related