Alright I have created an object, its a simple object. Basically just textfields and a combobox. I have them all in a gridbag layout with weightx and weight y set to one so that they fill out the panel I place them on.

Question 1:
What I'm wanting to do is basically re-create a jTable look and feel (where the columns all line up). So I'm wondering if there is a way where I can set the object's textfields to a specific width and add an option to allow the user to expand the field (as you see in the jtable).


Question 2:
I'm wondering how I can save all the information I put in these objects as well as deleting them. Basically every time I click a button it will add information from textfields to the object and display the object in a panel.

From what I have read, I might need to make the objects selectable by writing a custom list renderer and placing them on a list instead of panel.

Anyways here is my code for the object (minus the GUI type code, which i can add if needed):

public class DatabaseObject extends javax.swing.JPanel {

  private String fieldOne;
  private String fieldTwo;
  private String fieldThree;
  private String fieldFour;
  private String fieldFive;
  private String fieldSix;
  private ArrayList comboBoxNumber;

    /** Creates new form DatabaseObject */
    public DatabaseObject(String fieldOne, String fieldTwo, String fieldThree, String fieldFour, String fieldFive, String fieldSix, ArrayList comboBoxNumber ) {
        initComponents();

        this.fieldOne = fieldOne;
        this.fieldTwo = fieldTwo;
        this.fieldThree = fieldThree;
        this.fieldFour = fieldFour;
        this.fieldFive = fieldFive;
        this.fieldSix = fieldSix;
        this.comboBoxNumber = comboBoxNumber;

        for (int i = 0; i < comboBoxNumber.size(); i++){
           jComboBox1.addItem(comboBoxNumber.get(i));
        }

        jTextField1.setText(fieldOne);
        jTextField2.setText(fieldTwo);
        jTextField3.setText(fieldThree);
        jTextField4.setText(fieldFour);
        jTextField5.setText(fieldFive);
        jTextField6.setText(fieldSix);

    }

  public String getFieldOne() {
    return this.fieldOne;
  }

  public String getFieldTwo() {
    return this.fieldTwo;
  }

  public String getFieldThree() {
    return this.fieldThree;
  }

  public String getFieldFour() {
    return this.fieldFour;
  }

  public String getFieldFive() {
    return this.fieldFive;
  }

  public String getFieldSix() {
    return this.fieldSix;
  }

  public ArrayList getComboBoxInfo() {
      return this.comboBoxNumber;
  }

Then back in my main part of the program (where the button is pressed and the object is added)

I have:

private void InsertNewDataActionPerformed(java.awt.event.ActionEvent evt) {                                              
        
        //add driveCount to displayObject from a combobox1
        String fieldSix = (String) jComboBox1.getSelectedItem();

        //add info from fieldOne, fieldTwo, fieldThree, fieldFour, and fieldSix to the databaseObject
        String fOne = fieldOne.getText();
        String fTwo = fieldTwo.getText();
        String fThree = fieldThree.getText();
        String fFour = fieldFour.getText();
        String fFive = fieldFive.getText();

        //add comboBoxNumbers to arraylist aComboBox if they are blank or contain '-' 
        ArrayList<String> aComboBox = new ArrayList<String>();
        if (!comboBox1TextField.getText().equals("") && (!comboBox1TextField.getText().equals("-"))) {
            aComboBox.add(Dcc1TextField.getText());
        }
        if (!comboBox2TextField.getText().equals("") && (!comboBox2TextField.getText().equals("-"))) {
            aComboBox.add(Dcc2TextField.getText());
        }
        if (!comboBox3TextField.getText().equals("") && (!comboBox3TextField.getText().equals("-"))) {
            aComboBox.add(Dcc3TextField.getText());
        }
        if (!comboBox4TextField.getText().equals("") && (!comboBox4TextField.getText().equals("-"))) {
            aComboBox.add(Dcc4TextField.getText());
        }
        if (!comboBox5TextField.getText().equals("") && (!comboBox5TextField.getText().equals("-"))) {
            aComboBox.add(Dcc5TextField.getText());
        }
        if (!comboBox6TextField.getText().equals("") && (!comboBox6TextField.getText().equals("-"))) {
            aComboBox.add(Dcc6TextField.getText());
        }
        if (!comboBox7TextField.getText().equals("") && (!comboBox7TextField.getText().equals("-"))) {
            aComboBox.add(Dcc7TextField.getText());
        }
        if (!comboBox8TextField.getText().equals("") && (!comboBox8TextField.getText().equals("-"))) {
            aComboBox.add(Dcc8TextField.getText());
        }
        if (!comboBox9TextField.getText().equals("") && (!comboBox9TextField.getText().equals("-"))) {
            aComboBox.add(Dcc9TextField.getText());
        }
        if (!comboBox10TextField.getText().equals("") && (!comboBox10TextField.getText().equals("-"))) {
            aComboBox.add(Dcc10TextField.getText());
        }

        //add object to jpanel
        DisplayObject dbo = new DisplayObject(fOne, fTwo, fThree, fFour, fFive, fieldSix, aComboBox);
        
	//increment +1 for the row of the display object
        row++;

        //constraints for display object
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = row;
        gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;

        jPanel1.add(dbo, gridBagConstraints); //adds new displayObject, one per row



        
        //Clear the all fields from the GUI
        comboBox1TextField.setText((""));
        comboBox2TextField.setText((""));
        comboBox3TextField.setText((""));
        comboBox4TextField.setText((""));
        comboBox5TextField.setText((""));
        comboBox6TextField.setText((""));
        comboBox7TextField.setText((""));
        comboBox8TextField.setText((""));
        comboBox9TextField.setText((""));
        comboBox10TextField.setText((""));
        fieldOne.setText((""));
        fieldTwo.setText((""));
        fieldThree.setText((""));
        fieldFour.setText((""));
        fieldFive.setText((""));
        aComboBox.clear();


}

The above code just adds the information entered by a user from textfields into the display object. The textfields on the display object vary in size, overall i want to make it so they all line up like a table would.

And second, I would like to grab all that information and perhaps add it to a arraylist or something so that I can save the information to a csv/txt file. I would also like to be able to select/click the display object and be able to click a button to delete it from the panel and from the arraylist or whatever i use to save the information.

Anyone that could point me in the right direction, or give me some advice would be much appreciated.

*Note*
The code above is changed so that its easier to see what is going on, so the names of the textfields, combo boxes, etc are not the same as in my project.

class DatabaseObject extends javax.swing.JPanel

This rings all kinds of alarm bells!

The normal way of doing things is to have a "database object" that encapsulates the data items and provides accessors, as well as methods for saving/retrieving them (possibly delageted). You then have a GUI object that takes a database object as a parameter and displays it and/or collects new data values for it.

I don't know enough about your app to know whether you should be doing something different, but it comes as no surprise that you are running into problems with saving.
I can go into more detail if you think it's relevant.

class DatabaseObject extends javax.swing.JPanel

This rings all kinds of alarm bells!

The normal way of doing things is to have a "database object" that encapsulates the data items and provides accessors, as well as methods for saving/retrieving them (possibly delageted). You then have a GUI object that takes a database object as a parameter and displays it and/or collects new data values for it.

I don't know enough about your app to know whether you should be doing something different, but it comes as no surprise that you are running into problems with saving.
I can go into more detail if you think it's relevant.

Thanks for the reply James.

I couldn't find a way to set the width of each textfield other than setting the max, min, and preferred all to the same thing.

As for saving, I have decided to grab all the information entered in the database gui and instantly add it to an arraylist or vector and clear the panel that i add the display objects to and then have it add the objects from the arraylist or vector of database objects surrounded by a for loop.


Not sure if that makes sense, but I believe it should work. I'll be sure to post back when I get to testing it.

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.