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 7722759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:16:08+00:00 2026-06-01T04:16:08+00:00

I have two forms. First one is to decide button numbers by using jslider.

  • 0

I have two forms. First one is to decide button numbers by using jslider. Second form is to display jbuttons according to jslider value. When i click jbutton2, the second form shows and display buttons. It is working perfectly. However, I want to create jbutton without clicking jbutton2 in the first form.
Instead, when I change jslider, it should create buttons on the second form at the run time and once i change jslider it should create that amount of button again on the second form and refresh the second form buttons number according to jslider value.
I have tried revalidate();, repaint(); but they do not work, they dont refresh the second form.
So, How can I refresh second form when the jslider ,that is on the first form, changes ?

  • 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-01T04:16:09+00:00Added an answer on June 1, 2026 at 4:16 am

    Actually you should provide a piece of code so we can understand it better.
    But generally i think i could understand what you mean (if i’m not wrong).

    Basically you need the layout manager on the second form.
    here is a code sample, i hope it can generally answer your need.

    Code for the first form

    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JSlider;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    
    public class FirstForm extends JFrame {
        JButton fbtn  = new JButton("Show F2");
        JSlider fslider = new JSlider(1, 10);
        SecondForm fsecond = new SecondForm();
        public FirstForm(){
            setSize(200,200);
            getContentPane().setLayout(new BorderLayout());
            getContentPane().add(fbtn,BorderLayout.NORTH);
            getContentPane().add(fslider,BorderLayout.CENTER);
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            fbtn.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    fsecond.setVisible(true);
                }
            });
            fslider.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    fsecond.setBtnCount(fslider.getValue());
                }
            });
        }
        public static void main(String[] args) {
            // TODO code application logic here
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                @Override
            public void run() {
                    new FirstForm().setVisible(true);
                }
            });
        }
    }
    

    Code for the second form

    import java.awt.Component;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.util.Vector;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    
    public class SecondForm extends JFrame {
        Vector fbtns = new Vector();
        int fshowbtncount=1;
        GridBagConstraints gbc = new GridBagConstraints();
        public SecondForm(){
            setSize(200, 200);
            setLocation(201, 0);
            getContentPane().setLayout(new GridBagLayout());
            fbtns.add(new JButton("Button ".concat(String.valueOf(fbtns.size()+1))));
            invalidateForm();
        }
        private void invalidateForm(){
           gbc.fill = GridBagConstraints.VERTICAL;
           for (int i=0; i<fbtns.size(); i++) {
               getContentPane().remove((Component)fbtns.get(i));
           }
           int y=0;
           for (int i=0; i<fshowbtncount; i++) {
               gbc.gridx=0;
               gbc.gridy=y++;
               getContentPane().add((Component)fbtns.get(i),gbc);
           }
           pack();
           invalidate();
        }
        public void setBtnCount(int cnt){
          if (cnt>=0 && cnt!=fshowbtncount) {
              fshowbtncount = cnt;
              while (fbtns.size()<cnt) {
                fbtns.add(new JButton("Button ".concat(String.valueOf(fbtns.size()+1))));    
              }
              invalidateForm();
          }  
        }
    }
    

    Hope this can help you out.

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

Sidebar

Related Questions

I have two forms. First one is to decide numbers of button by using
I have two forms, one with a button 'Add' which loads the second form
I have two forms. The first form is the mainForm, this never goes anywhere.
I have two forms, one with a radio button that users must select to
I have a two forms on a page. The first one has several quantity
I have two servers, the first one with SQL Server 2005 and the second
Here I have a two files. One for forms and second php for getting
I have two forms on one page: a results form and a search form.
First thing, sorry for my English. I have two forms in index. Login form
I have two forms on one html page. Using jQuery, is it possible to

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.