2

I created a Form with Y.AXIS ordered Boxes. Every box contains a JLabel and a JList. This boxes contents are X.Axis ordered. (picture)

  1. How can i add a vertical spacer between the boxes, to make it more readable.
  2. How can i split the label and the list for having the label more on the left and the list more on the right
  3. I'm also open for other ideas making this form more readable.

BOXLAYOUT

Here is the code I'm working with. createCustomJList is returning a JList

String ean = (String) table.getModel().getValueAt(selection[0], 0);

JPanel addinfo= new JPanel();

String[] operations=new String[{"ROHTABAK","HERSTELLER","WARENGRUPPE","MARKENLOGO"};
Box moreInfo[] = new Box[4];

for(int i=0;i<operations.length;i++){   
    moreInfo[i] = Box.createHorizontalBox();
    moreInfo[i].add(new JLabel(operations[i]));
    moreInfo[i].add(createCustomJList(database.customgetter(operations[i],ean)));
    addinfo.add(moreInfo[i]);
}

BoxLayout layout = new BoxLayout(addinfo, BoxLayout.Y_AXIS);
addinfo.setLayout(layout);

JOptionPane.showMessageDialog(null,
    addinfo, "Naehere Infos",
    JOptionPane.OK_CANCEL_OPTION);

EDIT: I tried the solution with the gridlayout, but used JLists instead.

Is there a way to have something like a black border around a jlist?

BlackBorderJlist

SOLVEDEDIT: list.setBorder(new LineBorder(Color.darkGray, 1));

ENDRESULT:

RESULT

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Stefan Sprenger
  • 1,050
  • 20
  • 42
  • 2
    Just for future reference, these answers can be useful: http://stackoverflow.com/questions/17874717/providing-white-space-in-a-swing-gui – kiheru Jan 19 '14 at 16:40

1 Answers1

2

Myself, I would use a GridBagLayout for something like this. For instance, while this is not a perfect renditioning of your problem, and I use JTextFields where you use JLists, you get the idea:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.HashMap;
import java.util.Map;

import javax.swing.*;

@SuppressWarnings("serial")
public class InputForm extends JPanel {
   private static final int COLUMNS = 10;
   private static final int GAP = 3;
   private static final Insets LABEL_INSETS = new Insets(GAP, GAP, GAP, 15);
   private static final Insets TEXTFIELD_INSETS = new Insets(GAP, GAP, GAP, GAP);
   private String[] labelTexts;
   private Map<String, JTextField> fieldMap = new HashMap<String, JTextField>();

   public InputForm(String[] labelTexts) {
      this.labelTexts = labelTexts;
      setLayout(new GridBagLayout());
      for (int i = 0; i < labelTexts.length; i++) {
         String text = labelTexts[i];
         JTextField field = new JTextField(COLUMNS);
         fieldMap.put(text, field);

         addLabel(text, i);
         addTextField(field, i);
      }
   }

   public String[] getLabelTexts() {
      return labelTexts;
   }

   private void addTextField(JTextField field, int row) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.gridx = 1;
      gbc.gridy = row;
      gbc.anchor = GridBagConstraints.EAST;
      gbc.fill = GridBagConstraints.HORIZONTAL;
      gbc.insets = TEXTFIELD_INSETS;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      add(field, gbc);
   }

   private void addLabel(String text, int row) {
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.gridx = 0;
      gbc.gridy = row;
      gbc.anchor = GridBagConstraints.WEST;
      gbc.fill = GridBagConstraints.BOTH;
      gbc.insets = LABEL_INSETS;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      add(new JLabel(text), gbc);
   }

   public String getFieldText(String key) {
      String text = "";
      JTextField field = fieldMap.get(key);
      if (field != null) {
         text = field.getText();
      }
      return text;
   }

   private static void createAndShowGui() {
      String[] labelTexts = new String[] { "ROHTABAK", "HERSTELLER",
            "WARENGRUPPE", "MARKENLOGO" };
      InputForm inputForm = new InputForm(labelTexts);

      int result = JOptionPane.showConfirmDialog(null, inputForm, "Naehere Infos",
            JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
      if (result == JOptionPane.OK_OPTION) {
         for (String text : labelTexts) {
            System.out.printf("%20s %s%n", text, inputForm.getFieldText(text));
         }
      }
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

which when displayed shows:

enter image description here

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • would like to stay on the jlist, because there can be more entries and it's more generic :-/ – Stefan Sprenger Jan 19 '14 at 14:23
  • @StefanSprenger: I didn't tell you not to use JList. I'm just using JTextFields as placeholders since I don't have any of your list data, and was hopeful that you could extrapolate from my answer, use my *ideas* and sure, go ahead and use your JLists. – Hovercraft Full Of Eels Jan 19 '14 at 14:25
  • So there is no way to specify BoxLayout more closely? – Stefan Sprenger Jan 19 '14 at 14:55
  • @StefanSprenger: It depends on what you're trying to achieve. If your layout appearance is similar to the image I've posted, then no, don't use a BoxLayout for this and instead use a GridBagLayout or MIGLayout. If it's closer to your picture, then by all means tweak BoxLayout. – Hovercraft Full Of Eels Jan 19 '14 at 15:40
  • i implemented it with your method now. But i have a small problem now. I edited my question. Maybe you could have a look :) – Stefan Sprenger Jan 19 '14 at 15:44
  • solved by "list.setBorder(new LineBorder(Color.darkGray, 1));" thanks for your help :) – Stefan Sprenger Jan 19 '14 at 15:54
  • @StefanSprenger: That's a completely different question, and may require a new thread on stackoverflow. Are you asking about a border around the JList itself (perhaps calling `setBorder` will do this) or around the list items in which case you need to work with the list cell renderer. – Hovercraft Full Of Eels Jan 19 '14 at 15:54