JFormattedTextField - vertical misalignment problems...

Status
Nicht offen für weitere Antworten.

acinau

Mitglied
Hi, anstatt BoxLayout, verwende ich JFormattedTextField um die paare "JLabels und JTextField" aufzustellen.
There is one bad display problem, the vertical misalignment betw JLables and their corresponding JTextFields.
Here is the code longish stub code:
Code:
public class LayoutTest8 extends JApplet implements AdjustmentListener
{ ...java...
  private JLabel lblName, lblAddr1, lblAddr2, lblCity, lblZip, lblCountry;
  private JFormattedTextField fldName, fldAddr1, fldAddr2, fldCity, fldZip, fldCountry;
  private static String strName = "Name: ";
  private static String strAddr1 = "Address: ";
  private static String strAddr2 = "Address: ";
  private static String strCity  = "City: ";
  private static String strZip   = "Zip Code: ";
  private static String strCountry = "Country: ";
  private NumberFormat pctFormat;
  private MessageFormat msgFormat;
  
  public void init()
  {
    setBackground(Color.lightGray);
    Container contentPane = getContentPane(); 
    JPanel px = new JPanel();
    px.setLayout(new GridLayout(5,2));
    ...java...
    JPanel p9 = new JPanel();
    // p9.setLayout(new BoxLayout(p9, BoxLayout.Y_AXIS));
      // create labels:
      lblName = new JLabel(strName);
      lblAddr1 = new JLabel(strAddr1);
      lblAddr2 = new JLabel(strAddr2);
      lblCity = new JLabel(strCity);
      lblZip = new JLabel(strZip);
      lblCountry = new JLabel(strCountry);
      // create text fields:
      fldName = new JFormattedTextField(msgFormat);
      fldName.setColumns(20);
      fldName.setEditable(false);
      fldAddr1 = new JFormattedTextField(msgFormat);
      fldAddr1.setColumns(20);
      fldAddr1.setEditable(false);
      fldAddr2 = new JFormattedTextField(msgFormat);
      fldAddr2.setColumns(20);
      fldAddr2.setEditable(false);
      fldCity = new JFormattedTextField(msgFormat);
      fldCity.setColumns(20);
      fldCity.setEditable(false);
      fldZip = new JFormattedTextField(msgFormat);
      fldZip.setColumns(20);
      fldZip.setEditable(false);
      fldCountry = new JFormattedTextField(msgFormat);
      fldCountry.setColumns(20);
      fldCountry.setEditable(false);
      // Accessibility : build pairs of labels + textfields
      lblName.setLabelFor(fldName);
      lblAddr1.setLabelFor(fldAddr1);
      lblAddr2.setLabelFor(fldAddr2);
      lblCity.setLabelFor(fldCity);
      lblZip.setLabelFor(fldZip);
      lblCountry.setLabelFor(fldZip);
      // put pairs of label and text into their respective grid...
      JPanel p9_left = new JPanel(new GridLayout(0,1));
        p9_left.add(lblName);
        p9_left.add(lblAddr1);
        p9_left.add(lblAddr2);
        p9_left.add(lblCity);
        p9_left.add(lblZip);
        p9_left.add(lblCountry);
      JPanel p9_right = new JPanel(new GridLayout(0,1));
      p9_right.setBackground(Color.WHITE);
        p9_right.add(fldName);
        p9_right.add(fldAddr1);
        p9_right.add(fldAddr2);
        p9_right.add(fldCity);
        p9_right.add(fldZip);
        p9_right.add(fldCountry);
      p9.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
      p9.add(p9_left,BorderLayout.CENTER);
      p9.add(p9_right,BorderLayout.LINE_END);
    px.add(p9);

Yes it works, bar the alignment problem.
Dar ich auf eine Antwort hoffen dieses mal? Tja!
TIA 🙂
 
Damals war es C and F77
Nachher C and C++
Nunmehr kommt noch Java dazu.
 
Und, kannst do mal nachschauen ?
Gibt's da eine *einfache* Loesung zu meinem Problem, also eine, die ich verstehen kann?
 
nö, is mir erstens zu viel code und zweitens sind awt & swing auch nicht gerade so mein ding. hab erst vor kurzem damit angefangen, mal 'n bißchen damit rumzuspielen.
 
nö, is mir erstens zu viel code und zweitens sind awt & swing auch nicht gerade so mein ding. hab erst vor kurzem damit angefangen, mal 'n bißchen damit rumzuspielen.

<edit>
oops, doppelpost -> bitte löschen
 
In vinos veritas...
The bloody (blutiger) Java Console prints only what I tell it to print with
Code:
System.out.println("blabla");
Right?
So why don't you have a look at this and modify it to your need:
Code:
  OutputArea output;
   ...
    try // 
    { bloodyNewFrame = new AFrame(codeBase, baseInstr);
    } catch (IOException iox)
    { System.out.println("exception: " + iox.getMessage());
      iox.printStackTrace();
    }
    //
    String s = "Action event detected."
             + "\n"
             + "    Event source: " + source.getText()
             + " (an instance of " + getClassName(source) + ")";
     output.append(s + "\n");
     output.setCaretPosition(output.getDocument().getLength());
  }
and the code for the helper class OutputArea is
Code:
  // ==============================
  public class OutputArea extends JTextArea
  {
    JTextArea outputArea;
    JScrollPane scrolloutput;
    OutputArea(int rows, int columns)
    {
      add(outputArea = new JTextArea(rows, columns));
      outputArea.setEditable(false);
      outputArea.setLineWrap(true);
      scrolloutput = new JScrollPane(outputArea);
      scrolloutput.setPreferredSize(new Dimension(250,250));
    }
  }// end of class OutputArea
  // ==============================
Modify it, try it, works for me...
 
Hey, el_barto
I figured mine out , have you figured yours out yet?
Sowohl JLabel als auch JTextField muessen die selben set PreferredSize(new Dimension(x,y)) haben
Was habe ich dir gesagt ? In vinos veritas!
 
doch, is anstrengend. glaub, das wird heute nix mehr. fakt ist aber, dass ich es so ähnlich schon ausprobiert habe. das problem ist halt der mehrfache aufruf von append(). probier doch in deinem bsp. mal folgendes:
Code:
...
output.append("bla1");
try {
    Thread.sleep(3000);
} catch (InterruptedException ie) {
    ...
}
output.append("bla2");
...
wenn dann sofort "bla1" im textarea erscheint und nach 3 sekunden "bla2", dann ist das die lösung die ich suche. wenn allerdings 3 sekunden gar nix passiert und dann "bla1" und "bla2" gleichzeitig auftauchen, dann druck dir deine source aus und benutz sie als untersetzer für dein weinglas. :wink:
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben