Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8444233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:21:14+00:00 2026-06-10T09:21:14+00:00

I have 9 jbuttons added to jpanel and the panel added to jscrollpane and

  • 0

I have 9 jbuttons added to jpanel and the panel added to jscrollpane and it added to jframe.

http://www.pic1.iran-forum.ir/images/up9/95426323683658592564.jpg

when i change frame orientation by:
applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

the panel move to right and size of buttons got fixed and wouldn`t fill the panel but you see in the image below that scrolbar is fill all width of panel

http://www.pic1.iran-forum.ir/images/up9/60975202722295688553.jpg

(i used gridbaglayout for adding buttons and borderlayout.center for add scrollpane).

is that a bug in java or ?

EDIT:
its the simplest view . Does it help?

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.*;

public class MyFrame extends JFrame{
private JButton[] arrayButton = new JButton[9];
private JButton btnLeft = new JButton("<");
private JButton btnRight = new JButton(">");
private JScrollPane scpButtons = new JScrollPane();

public MyFrame() {
    for (int i = 0; i < arrayButton.length; i++) 
        arrayButton[i] = new JButton("btn");

    JPanel pnlButton = initPnlButton();
    scpButtons.setViewportView(pnlButton);
    setLayout(new BorderLayout());
    add(scpButtons, BorderLayout.CENTER);

           // comment it and see the result 
    applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);      
    pack();
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setExtendedState(JFrame.MAXIMIZED_BOTH);
    setVisible(true);
}

private JPanel initPnlButton() {
    JPanel pnlButton = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1, 1, 10,
            1, new Insets(0, 0, 0, 0), 0, 0);

    int ind = 0;
    int row = 3;
    int column = 4;
    for (int i = 0; i < row; i++) {
        for (int j = 1; j < column; j++) {
            gbc.gridx = j;
            gbc.gridy = i;
            pnlButton.add(arrayButton[ind++], gbc);
        }
    }
    gbc.weightx = 0;
    gbc.gridheight = 3;
    gbc.gridx = 0;
    gbc.gridy = 0;
    pnlButton.add(btnLeft, gbc);
    gbc.gridx = 4;
    gbc.gridy = 0;
    pnlButton.add(btnRight, gbc);
    pnlButton.setPreferredSize(new Dimension(1000, 700));
    return pnlButton;
}
public static void main(String[] args) {
    new MyFrame();
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:21:16+00:00Added an answer on June 10, 2026 at 9:21 am

    after playing with lots of properties and moving up and down the statements i found the answer.
    if you put

    scpButtons.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    

    after

    applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

    everything will be okay and no need to setSize(getWidth() + 1, getHeight() + 1);

    so funny it got a whle day from me 🙂 does c# or other langs have bugs like this?

    if you let SwingUtilities bloc to run , befor resizing the window everything are yet LtR.

    import java.awt.BorderLayout;
    import java.awt.ComponentOrientation;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import javax.swing.*;
    
    public class MyFrame extends JFrame {
    private JButton[] arrayButton = new JButton[9];
        private JButton btnLeft = new JButton("<");
        private JButton btnRight = new JButton(">");
        private JScrollPane scpButtons = new JScrollPane();
    
        public MyFrame() {
            for (int i = 0; i < arrayButton.length; i++)
                arrayButton[i] = new JButton("btn");
    
            JMenu mnuSettings = new JMenu("MENU");
            JMenuBar menubar = new JMenuBar();
            menubar.add(mnuSettings);
            setJMenuBar(menubar);
    
            JPanel pnlButton = initPnlButton();
            scpButtons.setViewportView(pnlButton);
            setLayout(new BorderLayout());
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            add(scpButtons, BorderLayout.CENTER);
    
            pack();
            // [1]
            setExtendedState(JFrame.MAXIMIZED_BOTH);
            //setSize(getWidth() + 1, getHeight() + 1);
            // [2]
            setVisible(true);
            // SwingUtilities.invokeLater(new Runnable() {
            // public void run() {
            applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    //here
            scpButtons.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
            // }
            // });
        }
    
        private JPanel initPnlButton() {
            JPanel pnlButton = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1, 1, 10,
                    1, new Insets(0, 0, 0, 0), 0, 0);
    
            int ind = 0;
            int row = 3;
            int column = 4;
            for (int i = 0; i < row; i++) {
                for (int j = 1; j < column; j++) {
                    gbc.gridx = j;
                    gbc.gridy = i;
                    pnlButton.add(arrayButton[ind++], gbc);
                }
            }
            gbc.weightx = 0;
            gbc.gridheight = 3;
            gbc.gridx = 0;
            gbc.gridy = 0;
            pnlButton.add(btnRight, gbc);
            gbc.gridx = 4;
            gbc.gridy = 0;
            pnlButton.add(btnLeft, gbc);
            pnlButton.setPreferredSize(new Dimension(1000, 700));
            return pnlButton;
        }
    
        public static void main(String[] args) {
            new MyFrame();
        }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program which creates JButtons which are then added to a JPanel
I have a series of JButtons that I have added to a JPanel. They
I have a subclass of JFrame(it contains JButton, Title and Jpanel) and i added
I have JFrame , and I added my JPanel class with paintComponent() method. For
I have a panel (with GridLayout( 0,1 )) embedded on JFrame. As per my
I am creating a GUI with grid layout. I have added the JTextFields, JButtons
I have a JPanel with a GridLayout(1,0) set to a JFrame Borderlayout.SOUTH , there
I have a JPanel and i am creating JButtons dynamically on runtime. I want
I have made two panels and then added in third panel. How can I
I have a java program with a JFrame and 3 JButtons in it. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.