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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:16:51+00:00 2026-05-30T20:16:51+00:00

I’m writing a program to allow a user to select a csv file, with

  • 0

I’m writing a program to allow a user to select a csv file, with varying number of columns and column titles, select the columns from the csv file that they wish to graph and finally plot the graph. The interface for this is a GUI that displays the three steps, the code for which is below. My problem is that the panel for “step 2” can only be displayed after the user has selected a file as the number of columns in the csv file can vary and the check-boxes to select the columns titles vary accordingly.

I’m currently looking at approaches for displaying the panels sequential. Ideally I’d like to keep the gui as it currently appears. I’ve read up briefly about using component listeners, but I’m not sure if they’re right for what I need, this is the example I was looking at: ComponentEventDemo

Another suggestion I’ve seen is using wizard dialogs like this Creating Wizard Dialogs with Java Swing, however I’d rather leave the previous steps visible if possible.

Any advice on a suitable approach to this problem would be great. Thanks!

package dynamicgui;

import java.awt.event.*;
import static javax.swing.GroupLayout.Alignment.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import javax.swing.*;

public class Attempt2GUI extends JPanel {//{
    //  implements ItemListener{

    public javax.swing.JFileChooser fc;
    //public JFileChooser fc;// = new JFileChooser(); 
    JFrame frame = new JFrame();
    JPanel panel1 = new JPanel(); //setp 1 panel
    JPanel panel2 = new JPanel(); //setp 2
    JPanel panel3 = new JPanel(); //step 3
    public String[] checkBoxNames = {"open", "close", "high", "low", "max",
        "today", "yesterday", "tomorrow", "lowest", "min"};

    public void setupGUI() {

        GroupLayout p1layout = new GroupLayout(panel1);
        panel1.setLayout(p1layout);
        //      panel1.addComponentListener(this);
        p1layout.setAutoCreateGaps(true);
        p1layout.setAutoCreateContainerGaps(true);

        GroupLayout p2layout = new GroupLayout(panel2);
        panel2.setLayout(p2layout);
        //     panel2.addComponentListener(this);
        p2layout.setAutoCreateGaps(true);
        p2layout.setAutoCreateContainerGaps(true);

        GroupLayout p3layout = new GroupLayout(panel3);
        panel3.setLayout(p3layout);
        //     panel3.addComponentListener(this);
        p3layout.setAutoCreateGaps(true);
        p3layout.setAutoCreateContainerGaps(true);


        //CONTENTS OF TOP PANEL
        JLabel step1 = new JLabel("Step 1:");
        step1.setFont(new java.awt.Font("Tahoma", 1, 11));
        step1.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);

        JLabel inst1 = new JLabel("Select the file you use for this program.");

        JButton fileChooseBtn = new JButton("Select File");

        fileChooseBtn.addActionListener(new ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileChooseBtnActionPerformed(evt);
            }
        });

        JButton toStep2Btn = new JButton("Next Step");
        //       toStep2Btn.addComponentListener(this);
        toStep2Btn.setActionCommand("toStep2");


        fc = new javax.swing.JFileChooser();
        //note: 'fc' is the public file chooser 

        JSeparator p1lineBreak = new JSeparator();

        //CONTENTS OF SECOND PANEL
        JLabel step2 = new JLabel("Step 2:");
        step2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        step2.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);

        JLabel inst2 = new JLabel("Select the title of the columns you wish to plot.");


        System.out.println("checkBoxNames length = " + checkBoxNames.length);

        System.out.println("length / 4 = " + checkBoxNames.length / 4);
        System.out.println("length % 4 = " + checkBoxNames.length % 4);


        JCheckBox[] checkBox;// = new JCheckBox[checkBoxNames.length];
        checkBox = new JCheckBox[checkBoxNames.length];

        JButton toStep3Btn = new JButton("Next Step");

        JSeparator p2lineBreak = new JSeparator();

        //CONTENTS OF THIRD PANEL
        JLabel step3 = new JLabel("Step :3");
        step3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        step3.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);

        JLabel inst3 = new JLabel("Plot the Graph.");

        JButton plotBtn = new JButton("Plot Graph");

        plotBtn.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                graphBtnActionPerformed(evt);
            }
        });

        //***************************************************************        
        //----------------LAYOUT PANELS-------------------------------
        //***************************************************************

        //TOP PANEL - panel to choose file to use
        //horizonttal layout
        GroupLayout.ParallelGroup p1withSeparator = p1layout.createParallelGroup(LEADING);
        GroupLayout.SequentialGroup p1leftToRight = p1layout.createSequentialGroup();
        p1leftToRight.addComponent(step1);
        GroupLayout.ParallelGroup p1rightColumn = p1layout.createParallelGroup(LEADING);
        p1rightColumn.addComponent(inst1);
        p1rightColumn.addComponent(fileChooseBtn);
        p1rightColumn.addComponent(toStep2Btn);
        p1leftToRight.addGroup(p1rightColumn);
        p1withSeparator.addGroup(p1leftToRight);
        p1withSeparator.addGap(17, 17, 17);
        p1withSeparator.addComponent(p1lineBreak);

        p1layout.setHorizontalGroup(p1withSeparator);

        //vertical layout
        GroupLayout.SequentialGroup p1topToBottom = p1layout.createSequentialGroup();
        GroupLayout.ParallelGroup p1topRow = p1layout.createParallelGroup(BASELINE);
        p1topRow.addComponent(step1);
        p1topRow.addComponent(inst1);
        p1topToBottom.addGroup(p1topRow);
        p1topToBottom.addComponent(fileChooseBtn);
        p1topToBottom.addComponent(toStep2Btn);
        p1topToBottom.addGap(20, 20, 20);
        p1topToBottom.addComponent(p1lineBreak);

        p1layout.setVerticalGroup(p1topToBottom);

        //    panel1.addComponentListener(this);

        //***********************************************************************
        //---------SECOND PANEL - checkboxes for column titles
        //***********************************************************************        

        //horiszontal layout - before dynamic
        GroupLayout.ParallelGroup p2withSeparatorH = p2layout.createParallelGroup(LEADING);
        GroupLayout.SequentialGroup p2leftToRight = p2layout.createSequentialGroup();
        p2leftToRight.addComponent(step2);
        GroupLayout.ParallelGroup p2rightColumnH = p2layout.createParallelGroup(LEADING);
        p2rightColumnH.addComponent(inst2);

        //vertical group - before dynamic             

        GroupLayout.SequentialGroup p2withSeparatorV = p2layout.createSequentialGroup();
        GroupLayout.ParallelGroup p2topToBottom = p2layout.createParallelGroup(BASELINE);
        p2topToBottom.addComponent(step2);
        GroupLayout.SequentialGroup p2rightColumnV = p2layout.createSequentialGroup();
        p2rightColumnV.addComponent(inst2);

        //add dynamic - both horizontal and vertical 
        if (checkBoxNames.length > 0) {
            int rows = checkBoxNames.length / 4; //change '4' to alter num boxes from left to right
            int remainder = checkBoxNames.length % 4;

            if (rows > 0 || remainder > 0) {
                GroupLayout.SequentialGroup boxesLeftToRightH = p2layout.createSequentialGroup();
                GroupLayout.ParallelGroup boxesTopToBottomV = p2layout.createParallelGroup();
                GroupLayout.ParallelGroup column1H = p2layout.createParallelGroup();
                GroupLayout.ParallelGroup column2H = p2layout.createParallelGroup();
                GroupLayout.ParallelGroup column3H = p2layout.createParallelGroup();
                GroupLayout.ParallelGroup column4H = p2layout.createParallelGroup();

                GroupLayout.SequentialGroup column1V = p2layout.createSequentialGroup();
                GroupLayout.SequentialGroup column2V = p2layout.createSequentialGroup();
                GroupLayout.SequentialGroup column3V = p2layout.createSequentialGroup();
                GroupLayout.SequentialGroup column4V = p2layout.createSequentialGroup();

                if ((rows > 1) || (rows == 1 && remainder > 0)) {

                    for (int c = 0; c < 4; c++) {
                        //cycle through each of the 4columns, change '4' to alter num boxes from left to right
                        for (int i = c; i < checkBoxNames.length - remainder; i = i + 4) {
                            //change '4' to alter num boxes from left to right
                            checkBox[i] = new JCheckBox(checkBoxNames[i]);
                            JCheckBox cb = new JCheckBox(checkBoxNames[i]);
                            cb.addItemListener(new MyCheckBoxListener());
                            //  cb.setActionCommand(checkBoxNames[i]);

                            switch (c) {
                                case 0:
                                    column1H.addComponent(cb);
                                    column1V.addComponent(cb);
                                    break;
                                case 1:
                                    column2H.addComponent(cb);
                                    column2V.addComponent(cb);
                                    break;
                                case 2:
                                    column3H.addComponent(cb);
                                    column3V.addComponent(cb);
                                    break;
                                case 3:
                                    column4H.addComponent(cb);
                                    column4V.addComponent(cb);
                                    break;
                            }
                        }
                        if (remainder - c > 0) {
                            checkBox[checkBoxNames.length - (remainder - c)] = new JCheckBox(
                                checkBoxNames[checkBoxNames.length - (remainder - c)]);
                            JCheckBox cb = new JCheckBox(checkBoxNames[checkBoxNames.length - (remainder - c)]);
                            cb.addItemListener(new MyCheckBoxListener());
                            //   cb.setActionCommand(checkBoxNames[c]);
                            switch (c) {
                                case 0:
                                    column1H.addComponent(cb);
                                    column1V.addComponent(cb);
                                    break;
                                case 1:
                                    column2H.addComponent(cb);
                                    column2V.addComponent(cb);
                                    break;
                                case 2:
                                    column3H.addComponent(cb);
                                    column3V.addComponent(cb);
                                    break;
                                case 3:
                                    column4H.addComponent(cb);
                                    column4V.addComponent(cb);
                                    break;
                            }
                        }
                    }
                }

                if (rows == 0 && remainder > 0) {
                    for (int i = 0; i < checkBoxNames.length; i++) {
                        //change '4' to alter num boxes from left to right

                        if (remainder - i > 0) {
                            checkBox[checkBoxNames.length - (remainder - i)] = new JCheckBox(
                                checkBoxNames[checkBoxNames.length - (remainder - i)]);
                            JCheckBox cb = new JCheckBox(checkBoxNames[checkBoxNames.length - (remainder - i)]);
                            cb.addItemListener(new MyCheckBoxListener());
                            //     cb.setActionCommand(checkBoxNames[i]);
                            switch (i) {
                                case 0:
                                    column1H.addComponent(cb);
                                    column1V.addComponent(cb);
                                    break;
                                case 1:
                                    column2H.addComponent(cb);
                                    column2V.addComponent(cb);
                                    break;
                                case 2:
                                    column3H.addComponent(cb);
                                    column3V.addComponent(cb);
                                    break;
                                case 3:
                                    column4H.addComponent(cb);
                                    column4V.addComponent(cb);
                                    break;
                            }
                        }
                    }
                }

                boxesLeftToRightH.addGroup(column1H);
                boxesLeftToRightH.addGroup(column2H);
                boxesLeftToRightH.addGroup(column3H);
                boxesLeftToRightH.addGroup(column4H);

                boxesTopToBottomV.addGroup(column1V);
                boxesTopToBottomV.addGroup(column2V);
                boxesTopToBottomV.addGroup(column3V);
                boxesTopToBottomV.addGroup(column4V);

                p2rightColumnH.addGroup(boxesLeftToRightH);
                p2rightColumnV.addGroup(boxesTopToBottomV);

            }

        }// end if length >0  


        //add horizonatal - after dygnamic    
        p2rightColumnH.addComponent(toStep3Btn);
        p2leftToRight.addGroup(p2rightColumnH);
        p2withSeparatorH.addGroup(p2leftToRight);
        p2withSeparatorH.addGap(17, 17, 17);
        p2withSeparatorH.addComponent(p2lineBreak);
        p2layout.setHorizontalGroup(p2withSeparatorH);

        //add vertical - after dynamic
        p2rightColumnV.addComponent(toStep3Btn);
        p2topToBottom.addGroup(p2rightColumnV);
        p2withSeparatorV.addGroup(p2topToBottom);
        p2withSeparatorV.addGap(20, 20, 20);
        p2withSeparatorV.addComponent(p2lineBreak);
        p2layout.setVerticalGroup(p2withSeparatorV);

        //   panel2.addComponentListener(this);


        //***********************************************************************        
        //------------BOTTOM PANEL - panel that plots the graphs-----------------
        //***********************************************************************

        //horizontal layout
        GroupLayout.SequentialGroup p3leftToRight = p3layout.createSequentialGroup();
        p3leftToRight.addComponent(step3);
        GroupLayout.ParallelGroup p3rightColumn = p3layout.createParallelGroup(LEADING);
        p3rightColumn.addComponent(inst3);
        p3rightColumn.addComponent(plotBtn);
        p3leftToRight.addGroup(p3rightColumn);

        p3layout.setHorizontalGroup(p3leftToRight);

        //vertical layout
        GroupLayout.SequentialGroup p3topToBottom = p3layout.createSequentialGroup();
        GroupLayout.ParallelGroup p3topRow = p3layout.createParallelGroup(BASELINE);
        p3topRow.addComponent(step3);
        p3topRow.addComponent(inst3);
        p3topToBottom.addGroup(p3topRow);
        p3topToBottom.addComponent(plotBtn);
        p3layout.setVerticalGroup(p3topToBottom);



        //***************************************************************        
        //----------------ADD PANELS TO MAIN PANEL-------------------------------
        //***************************************************************


        /*  
        frame.getContentPane().setLayout(new GridLayout(3,0));
        frame.getContentPane().add(panel1);
        frame.getContentPane().add(panel2);
        frame.getContentPane().add(panel3);

        frame.pack();
        frame.setVisible(true);


         * 
         */

    }//end setupGUI

    public void addToFrame() {

        Attempt2GUI a2g = new Attempt2GUI();

        //   a2g.setupGUI();

        frame.setTitle("Plot Graph");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        frame.getContentPane().setLayout(new GridLayout(3, 0));
        frame.getContentPane().add(panel1);
        frame.getContentPane().add(panel2);
        frame.getContentPane().add(panel3);

        frame.pack();
        frame.setVisible(true);
    }
    /*
     * for the select file button to open file chooser window
     */
    private static String selFilepath = "XXX";

    private void fileChooseBtnActionPerformed(ActionEvent evt) {

        System.out.println("the filepath before file selected is " + selFilepath);
        // Show open dialog; this method does not return until the dialog is closed
        fc.showOpenDialog(this);
        File selFile = fc.getSelectedFile();
        selFilepath = selFile.getAbsolutePath();

        System.out.println("the file selected is " + selFile);
        System.out.println("the file selected in string form is " + selFilepath);
    }

    /*
     * the filepath will be passed to the reader class
     */
    public String getFilepath() {
        return selFilepath;
    }

    /*
     * set the second panel visible
     *
    private void toStep2BtnActionPerformed(ActionEvent evt) {

    if("toStep2".equals(evt.getActionCommand())){
    panel2.setVisible(true);

    //Need to revalidate and repaint, or else the panel
    //will probably be drawn in the wrong place.
    panel2.revalidate();
    panel2.repaint();
    }
    }

    /*
     * set the third panel visible
     *
    private void toStep3BtnActionPerformed(ActionEvent evt) {
    throw new UnsupportedOperationException("Not yet implemented");

    }


    /*
     * call in the graph class 
     */
    private void graphBtnActionPerformed(ActionEvent evt) {
    }

    public static void main(String[] args) {

        try {
            UIManager.setLookAndFeel(
                UIManager.getSystemLookAndFeelClassName());

        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MyGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MyGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MyGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MyGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        Attempt2GUI a2 = new Attempt2GUI();
        a2.setupGUI();
        a2.addToFrame();

        System.out.println("filepath is " + a2.getFilepath());
        a2.getFilepath();

        //    mmg.getSelectedBoxes();
    }

    /*
     * keep running total of all checkboxes selected
     */
    public static ArrayList<Integer> selected = new ArrayList<Integer>();

    public int[] addSelectedBoxes(int index) {

        selected.add(index);

        for (int i = 0; i < selected.size(); i++) {
            System.out.println("selected list in lineGUI is " + selected.get(i));
        }

        int[] selectedBoxes = {1, 2, 3};
        return selectedBoxes;
    }
}//end class lineGUI

/*
 * handle the itemLIstener on the checkboxes 
 */
class MyCheckBoxListener implements java.awt.event.ItemListener {

    ComponentGUI lg = new ComponentGUI();
    String[] boxes = lg.checkBoxNames;
    ArrayList<Integer> selectedList = new ArrayList<Integer>();
    /*
     * tdentify the selected boxes and return an array of the indexes of these boxes
     * to be used by the gui to plot each in the selected list series selected
     */

    @Override
    public void itemStateChanged(ItemEvent ie) {

        for (int i = 0; i < boxes.length; i++) {
            if (ie.getItem().toString().endsWith("text=" + boxes[i] + "]")) {
                selectedList.add(i);
                lg.addSelectedBoxes(i);
            }
        }

        int[] selectedArray = new int[selectedList.size()];

        for (int i = 0; i < selectedList.size(); i++) {
            selectedArray[i] = selectedList.get(i);
        }

        for (int i = 0; i < selectedArray.length; i++) {
            System.out.println("box selected = " + selectedArray[i]);
            System.out.println("selectedList = " + selectedList.get(i));
        }
    }

    public ArrayList<Integer> getNewBoxTick() {
        return selectedList;
    }
}
  • 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-05-30T20:16:53+00:00Added an answer on May 30, 2026 at 8:16 pm

    The wizard article you linked provides a basic description of how to roll your own wizard implementation and you should be able to easily display a list of the steps and highlight the current one.

    I rolled my own wizard implementation and it was not too complicated. Here are some hints on how you could do it:

    • create a wizard dialog that has 3 areas: the steps list, the actual step controls and the buttons to navigate through the steps
    • when navigating to the previous/next step you might want to do the following:
      • update the back/next buttons according to the position within the step list
      • execute the “next” action or roll back the previous action if needed
      • replace the step controls (if you have a panel for each step just replace the panel)

    You might want to create a base class for wizard steps that provides common data like next or rollback methods, the step description/name, the step controls panel etc.

    There might be alternative solutions which might make use of tabbed panes or the CardLayout layout manager.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from

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.