Hi all,

I've created a GUI layout for what I am supposed to do. I have 2 combo boxes in the GUI and both serves the same function. The both of them needs to be able to drop-down the list of files from one of my folders. Hence, I just have to make one of the combo boxes to work and use the same codes for the other. At the same time, the combo box would need to allow multiple selections, How do I do that?

would be thankful if anyone could offer me some help.

below are my codes:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Layout extends JFrame {
	
   	Layout() {
   
   		//add radio buttons
    	final JRadioButton query1, query2, query3, target1, target2, target3;

    	
    	query1 = new JRadioButton();
    	query1.setLayout(new FlowLayout());
		query1.add(new JTextField(15));
		query1.add(new JButton("Upload"));
		query1.setSelected(true);

    	query2 = new JRadioButton();
    	query2.setLayout(new FlowLayout());
    	query2.add(new JLabel("Select PDB File:"));
    	query2.add(new JComboBox());

    	query3 = new JRadioButton();
    	query3.setLayout(new FlowLayout());
    	query3.add(new JLabel("    Enter Sequence :"));
		query3.add(new JTextField(15));
		
    	target1 = new JRadioButton();
    	target1.setLayout(new FlowLayout());
    	target1.add(new JTextField(15));
		target1.add(new JButton("Upload"));
		target1.setSelected(true);
	
    	target2 = new JRadioButton();
    	target2.setLayout(new FlowLayout());
    	target2.add(new JLabel("Select PDB File :"));
    	target2.add(new JComboBox());

    	target3 = new JRadioButton();
		target3.setLayout(new FlowLayout());
    	target3.add(new JLabel("    Enter Sequence :"));		
		target3.add(new JTextField(15));
		
    	//query group button
    	final ButtonGroup group1 = new ButtonGroup();
    	group1.add(query1);
    	group1.add(query2);
    	group1.add(query3);
    
    	//target group button
    	final ButtonGroup group2 = new ButtonGroup();
    	group2.add(target1);
    	group2.add(target2);
    	group2.add(target3);    
   	
    	JPanel querypane = new JPanel();
    	querypane.setLayout(new BoxLayout(querypane, BoxLayout.Y_AXIS));
    	querypane.setSize(10,10);
    	querypane.setBorder(BorderFactory.createTitledBorder("Query Structure"));
    	querypane.add(query1);
    	querypane.add(query2);
    	querypane.add(query3);

		JPanel targetpane = new JPanel();
		targetpane.setLayout(new BoxLayout(targetpane, BoxLayout.Y_AXIS));
		targetpane.setSize(10,10);
		targetpane.setBorder(BorderFactory.createTitledBorder("Target Structure"));
    	targetpane.add(target1);
    	targetpane.add(target2);
    	targetpane.add(target3);
    	
    	
		//end of query & target layout
		
		
		JCheckBox GeoHash, SuperPos, DynPro;
		
		GeoHash = new JCheckBox();
		GeoHash.setLayout(new FlowLayout());
		GeoHash.add(new JLabel("    Geometric Hashing"));
		GeoHash.add(new JButton("Results"));
		//GeoHash.setLayout(new GridLayout(2,1) );
		
		SuperPos = new JCheckBox();
		SuperPos.setLayout(new FlowLayout());
		SuperPos.add(new JLabel("    Superposition"));
		SuperPos.add(new JButton("Results"));
		
		DynPro = new JCheckBox();
		DynPro.setLayout(new FlowLayout());
		DynPro.add(new JLabel("    Dynamic Programming"));
		DynPro.add(new JButton("Results"));

		JPanel algorithmpane = new JPanel();
    	algorithmpane.setLayout(new BoxLayout(algorithmpane, BoxLayout.X_AXIS));
    	algorithmpane.setBorder(BorderFactory.createTitledBorder("Algorithms"));
    	algorithmpane.add(GeoHash);
    	algorithmpane.add(SuperPos);
    	algorithmpane.add(DynPro);
    	
    	
    	
    	JTextArea screen = new JTextArea(20,60);
    	JPanel resultpane = new JPanel();
    	resultpane.setLayout(new FlowLayout());
    	resultpane.setLayout(new BoxLayout(resultpane, BoxLayout.X_AXIS));
    	resultpane.add(screen, BorderLayout.CENTER);
    	resultpane.setBorder(BorderFactory.createTitledBorder("Results"));
    	
		
		JButton Compute, Print, Save;
		Compute = new JButton("Compute");
		Compute.setLayout(new BoxLayout(Compute, BoxLayout.X_AXIS));
		Print = new JButton("Print");
		Print.setLayout(new BoxLayout(Print, BoxLayout.X_AXIS));
		Save = new JButton("Save");
		Save.setLayout(new BoxLayout(Save, BoxLayout.X_AXIS));
		
		JPanel bottom = new JPanel();
		bottom.setLayout(new FlowLayout());
		//bottom.setLayout(new BoxLayout(bottom, BoxLayout.X_AXIS));
		bottom.add(Compute);
		bottom.add(Print);
		bottom.add(Save);

		
		Container contentPane = getContentPane();
		contentPane.add(querypane);
		contentPane.add(targetpane);
		contentPane.add(algorithmpane); 
		contentPane.add(resultpane);
		contentPane.add(bottom);
			  
 
    		}//end of Layout()

 


    public static void main(String[] args) {
    	
    	//create frame
    	Layout frame = new Layout() ;
    	frame.setResizable(false);
    	frame.setTitle("Biomolecular Structure Comparison");
    	frame.setSize(700,680);
    	frame.setLayout(new FlowLayout());
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setLocation(100,0);
				    
		//show frame
    	frame.setVisible(true); 
    		   
		    }//end of main
		    
		    

		    	
    }//end of class

Recommended Answers

All 7 Replies

See the File class for a method to get the names of the files in a directory as an array, then use the removeAllItems of the ComboBox and the addItem method of JComboBox in a loop, then validate/repaint the JComboBox.

I have the name of the files in another program..can i call it out from there? but im not very sure how to call them out when its in another file..

That has nothing to do with comboboxes and the like, but with standard classes and methods.

public class Object1 {
  public String[] getArray() {
    return new String[] { "A", "B" };
  }
}
public class Object2 {
  Object1 obj1 = new Object1();
  public void doSomeAction() {
    String[] array = obj1.getArray();
  }
}

I can now display all the files in my JCombo Box but I want to also have a selection in the list that says "ALL" and at the same time set the default editable region to "Please Select File". And I would also want to allow multiple selections. How do i do these?

the code that i've used is here:

File dir = new File("I:\\MP Project\\Pre-processing\\Raw Files");
        FilenameFilter filter = new FilenameFilter()
        {
            public boolean accept(File dir, String name)
            {
                return name.endsWith(".txt");
            }
        };
        String[] var = dir.list(filter);
        
        if (var == null)
        {
       		System.out.println("JCOMBO BOX Directory is INCORRECT or does not exist!");
        }
        else
        {
            for (int i=0; i<var.length; i++) //populate the combo
            {
                String filename = var[i];
                queryCombo.addItem(filename);
                targetCombo.addItem(filename);
            }
        }

By adding an "ALL" and using a SelectionListener together with setText to set the value in the entry portion of the ComboBox. A ComboBox cannot (AFAIK) do multiple selections. Maybe you want to use a List and TextField in tandem?

hmmm how do i go about using JTextField and List? I've sourced for the codes but am not very sure of how to go about doing them

By using a SelectionListener on the List to populate the textfield (essentially the same way that combobox works, behind the scenes).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.