Help with a layout/gui issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blazedaces
    Contributor
    • May 2007
    • 284

    #1

    Help with a layout/gui issue

    Hey, so here's an example of what I want to do:

    This "window" if you will, or simple gui, will accept an ArrayList<Strin g> and in turn spit out for every one of them a checkbox and a text field where you can rename it to whatever name you chose (later I'll add something to check and make sure none of the names are the same... but for now it doesn't matter).

    I'm not very familiar with GUI in java so I don't know the best way of doing this. I'm not asking for code, but simply some help in the right direction. I've downloading all of java swing's tutorial demos and examples and have looked at just about every component and layout example in there. Still I don't feel like I know what I'm doing and you'll see in my code soon what I mean.

    Before I do I want to show you guys what I want the gui to look like in a sense:

    Code:
    ArrayList<String> temp = new ArrayList<String>(2);
                	temp.add("First Tag\\");
        			temp.add("Second Tag\\");
    
    If above is the arraylist I would want the gui to look something like this:
    
    Note: ---- represents the border of the window, [] represents a checkbox, text represents non-editable text and "text" represents editable text
    
    Well I didn't mention this earlier but the point of the checkboxes is to not include those strings, when I return an arraylist later, but that's not important now.  The gui showing up is coming first...
    ----------------------------------------------
    -  []First Tag: "First Tag"                  -
    -  []Second Tag: "Second Tag"                -
    ----------------------------------------------
    
    Of course with a title and minimize/maximize/close buttons there too, but they come automatically...
    Here's what I've got so far with the code, I tried to put stuff into JPanels and then add them to the gui later:

    [code=java]
    import java.awt.*;
    import java.awt.event. *;
    import javax.swing.*;
    import java.util.*;

    public class TagChoosingWind ow implements ItemListener {
    JTextField[] textFields;
    JCheckBox[] checkBoxes;
    JPanel[] panels;

    short[] included;

    ArrayList<Strin g> tags;

    public TagChoosingWind ow(ArrayList<St ring> al) {
    tags = al;
    int size = tags.size();

    included = new short[size];

    textFields = new JTextField[size];
    checkBoxes = new JCheckBox[size];
    panels = new JPanel[size];

    for (int i = 0; i < size; i++) {
    checkBoxes[i] = new JCheckBox(tags. get(i) + " : ");
    checkBoxes[i].setSelected(tr ue);
    checkBoxes[i].addItemListene r(this);

    textFields[i] = new JTextField(tags .get(i));

    included[i] = 1;

    panels[i] = new JPanel(new BoxLayout(panel s[i], BoxLayout.LINE_ AXIS));
    panels[i].add(checkBoxes[i]);
    panels[i].add(textFields[i]);
    }


    }

    private void buildUI(Contain er container) {
    container.setLa yout(new BoxLayout(conta iner, BoxLayout.PAGE_ AXIS));

    for (int i = 0; i < panels.length; i++) {
    container.add(p anels[i]);
    }
    }

    public void itemStateChange d(ItemEvent e) {
    Object source = e.getItemSelect able();
    int index = -1;
    for (int i = 0; i < checkBoxes.leng th; i++) {
    if (source == checkBoxes[i]) {
    index = i;
    break;
    }
    }

    if (e.getStateChan ge() == ItemEvent.SELEC TED) {
    textFields[index].setEnabled(tru e);
    included[index] = 1;
    } else {
    textFields[index].setEnabled(fal se);
    included[index] = 0;
    }
    }

    private void createAndShowGU I() {
    //Create and set up the window.
    JFrame frame = new JFrame("Choose What Data Tags You Want and Name Them");
    frame.setDefaul tCloseOperation (JFrame.EXIT_ON _CLOSE);

    //Set up the content pane.
    TagChoosingWind ow tagchoosingwind ow = new TagChoosingWind ow(tags);
    tagchoosingwind ow.buildUI(fram e.getContentPan e());

    //Display the window.
    frame.pack();
    frame.setVisibl e(true);
    }

    public static void main(String args[]) {
    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.Swi ngUtilities.inv okeLater(new Runnable() {
    public void run() {
    ArrayList<Strin g> temp = new ArrayList<Strin g>(2);
    temp.add("First Tag\\");
    temp.add("Secon d Tag\\");
    TagChoosingWind ow tagchoosingwind ow = new TagChoosingWind ow(temp);
    tagchoosingwind ow.createAndSho wGUI();
    }
    });
    }
    }
    [/code]

    Oh and thank you for all help, it's much appreciated,

    -blazed
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Have a look at the JTable component with its TableModel compadre. The first
    column could consists of JCheckBoxes and the second column can be a
    JTextField. The TableModel receives the List at construction time so it is able
    to set up the row dimension of the JTable (there are just two columns).

    Of course you have to build your own TableModel for this (check the API docs).
    You can build a 'collect' method in your model that collects all the selected
    Strings from the JTable.

    kind regards,

    Jos

    Comment

    • blazedaces
      Contributor
      • May 2007
      • 284

      #3
      Originally posted by JosAH
      Have a look at the JTable component with its TableModel compadre. The first
      column could consists of JCheckBoxes and the second column can be a
      JTextField. The TableModel receives the List at construction time so it is able
      to set up the row dimension of the JTable (there are just two columns).

      Of course you have to build your own TableModel for this (check the API docs).
      You can build a 'collect' method in your model that collects all the selected
      Strings from the JTable.

      kind regards,

      Jos
      A good idea. I was considering a JTable at first, but didn't like the look it gave. What you suggested I think might solve all my problems though because if I had my own built TableModel I'm guessing I could manipulate those lines in between the cells to not appear along with other look-and-feel parts of the table.

      Thanks,

      -blazed

      Well, I took a look at it, that's not exactly what I thought TableModel was... but alright. Maybe there's another way to avoid that ... if not, it doesn't matter I guess...

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by blazedaces
        A good idea. I was considering a JTable at first, but didn't like the look it gave. What you suggested I think might solve all my problems though because if I had my own built TableModel I'm guessing I could manipulate those lines in between the cells to not appear along with other look-and-feel parts of the table.

        Thanks,

        -blazed

        Well, I took a look at it, that's not exactly what I thought TableModel was... but alright. Maybe there's another way to avoid that ... if not, it doesn't matter I guess...
        You have to read a bit further: there's an AbstractTableMo del class that does
        the bulk of the work for you; you only have to implement three or four methods:
        row and column sizes, get a value and set a value. That's all there is to it. The
        AbstractModel class does the rest for you. For most of the 'complicated'
        interfaces there's an Abstract class implementation for you (e.g. AbstractList,
        AbstractTableMo del etc.) so you don't have to (re)implement all the boring
        bookkeeping stuff yourself.

        kind regards,

        Jos

        Comment

        • blazedaces
          Contributor
          • May 2007
          • 284

          #5
          Originally posted by JosAH
          You have to read a bit further: there's an AbstractTable model that does the bulk
          of the work for you; you only have to implement three or four methods: row and
          column sizes, get a value and set a value. That's all there is to it. The Abstract
          thing does the rest for you. For most of the 'complicated' interfaces there's an
          Abstract implementation for you (e.g. AbstractList, AbstractTableMo del etc.)
          so you don't have to (re)implement all the boring bookkeeping stuff yourself.

          kind regards,

          Jos
          Well, I did take a look at that, but it doesn't seem to be able to do what I want:

          mainpulate the table so there are no borders between the individual cells or specific cells (like a border between the columns, but not the rows or vice versa)

          I may be wrong. I will have to investigate further... I do notice in the "How to use tables" tutorial you can change the background, which is sweet. I never like looking into a lightbulb ;)

          -blazed

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by blazedaces
            Well, I did take a look at that, but it doesn't seem to be able to do what I want:

            mainpulate the table so there are no borders between the individual cells or specific cells (like a border between the columns, but not the rows or vice versa)

            I may be wrong. I will have to investigate further... I do notice in the "How to use tables" tutorial you can change the background, which is sweet. I never like looking into a lightbulb ;)

            -blazed
            Swing components don't work that way: the Model takes care of the data.
            the visual component (the JTable) takes care of the rendering; Hava a look at the
            setInterCellSpa cing() method for example.

            kind regards,

            Jos

            Comment

            • blazedaces
              Contributor
              • May 2007
              • 284

              #7
              Originally posted by JosAH
              Swing components don't work that way: the Model takes care of the data.
              the visual component (the JTable) takes care of the rendering; Hava a look at the
              setInterCellSpa cing() method for example.

              kind regards,

              Jos
              I think I found a solution to my problem. You can use the setBackground with setGridColor to set them as the same color and poof... no more grid! hehe!

              -blazed

              Comment

              Working...