Results 1 to 9 of 9
  1. #1
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Question [SOLVED] How to get text area to line up with label.

    I got enough of the program running that I am actually using it day-to-day but I am a programmer ... the entire screen viewable area changes in size as the size of the window changes...getting the labels to line up like they have them in TextSamplerDemoMetal.png at the upper left corner is gonna waste time; I have tried several Layout mangers, do not even know which one I am using right now.....I could dig through any one of them very effectively + I know part of the answer will be to read the example in the tutorial but digging through all of them and doing code arcaheology to find the right approach has not worked.

    I need to get ui working like this:
    Java Code:
       label:[ textarea ]
       label:[ textarea ]
       label:[ textarea ]
       label:[ textarea ]
       label:[ textarea ]
       label:[ textarea ]
    keeping them at least somewhat centered, the pairs can go to the left if it makes any diff in coding ~ I do need the center axis to be the same for all eight or ten of them, maybe expansion as soon as I get it where Team Lead can use it. Then add some buttons in an orderly fashion, perhaps at the bottom of a BorderLayout with a Label or some Labels at the top, all of it to control this stack of pairs.

    I have two buttons working right now, which is gonna be hard to explain to Team Lead who has not written a lot of code why they move around and are lined up on the wrong side and diss-appear and so on ... it was enough for me getting the labels to behave like I thought they would - I have them in some whaterver it is I could find and don't even know what kind of panel it is..I have a big chunk of code that is working off of this so I need to get it definitely closer to what it will eventuate to so that I do not have too much re-patching - I need to do re-factoring right now and it has gone too far without doing that.

    The effectiveness of that work hinges on this design - what goes in here is what all the hardcode is there to serve.

    I can use some pretty simple stuff and hardcode or any of several approaches, I need right now the ( it probably won't post well ) have the labels and the text area march down a definite common center matching up horizonatlly without moving around whenver JFrame is resized.

    Java Code:
        +    
        +    
        +    
        +    
        +    
        +    
        +    
        +
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    This is Swing, correct? So you're using JLabels, correct? And are those then JTextAreas or are they JTextFields?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Your problem description sounds like the SpringForm.java example in the SpringLayout tutorial
    How to Use SpringLayout (The Java™ Tutorials > Creating a GUI with JFC/Swing > Laying Out Components Within a Container)

    db

  4. #4
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default Swing

    Java Code:
        private JTextField s_1= new JTextField(20);//
        private JTextField s_2= new JTextField(20);//
        private JTextField s_3= new JTextField(20);//
        private JTextField s_4= new JTextField(20);//
        private JTextField s_5= new JTextField(20);//
        private JTextField s_6= new JTextField(new String(new java.util.Date().toString()));//
        private JTextField s_7= new JTextField(20);//
        private JTextField s_8= new JTextField(new String(new java.lang.Long(java.lang.System.currentTimeMillis()).toString()));//
        private JTextField s_9= new JTextField(20);//
    
        //
        private JTextArea s_10;//
    How to Use SpringLayout at How to Use SpringLayout (The Java™ Tutorials > Creating a GUI with JFC/Swing > Laying Out Components Within a Container) looks like what I have been through before.

    I had a BorderLayout just about working once on another code set, wondering if I can add a BoxLayout to the center of a BorderLayout and put something on Top and Bottom that will align each added across horizontally not wrapping them.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm not sure if this would help you out. This is some code I was messing with in September to create a JPanel that has labels on the left aligned with JTextFields on the right. The class will create a variable number of these label/textfield combos, and the labels Strings are passed as an array to the class constructor. The JTextFields are held in a Map<String, JTextField> with the label strings being the key (no duplicates allowed).

    QueryComponent2.java
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.swing.BorderFactory;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JComponent;
    import javax.swing.JTextField;
    
    public class QueryComponent2
    {
      private static final int FIELD_LENGTH = 12;
      private static final int MID_GAP = 25;
      private static final int LINE_GAP = 10;
      private static final int BORDER_GAP = 10;
      private JPanel mainPanel = new JPanel();
      private Map<String, JTextField> fieldMap = new HashMap<String, JTextField>();
      private String[] labelStrings;
    
      public QueryComponent2(String[] labels)
      {
        JPanel labelPanel = new JPanel(new GridLayout(0, 1, 0, LINE_GAP));
        JPanel fieldPanel = new JPanel(new GridLayout(0, 1, 0, LINE_GAP));
        
        labelStrings = new String[labels.length];
        System.arraycopy(labels, 0, labelStrings, 0, labels.length);
        for (String string : labels)
        {
          labelPanel.add(new JLabel(string));
          JTextField textField = new JTextField(FIELD_LENGTH);
          fieldPanel.add(textField);
          fieldMap.put(string, textField);
        }
        
        mainPanel.setBorder(BorderFactory.
            createEmptyBorder(BORDER_GAP, BORDER_GAP, BORDER_GAP, BORDER_GAP));
        mainPanel.setLayout(new BorderLayout(MID_GAP, 0));
        mainPanel.add(labelPanel, BorderLayout.WEST);
        mainPanel.add(fieldPanel, BorderLayout.CENTER);
      }
      
      public String getFieldText(String key)
      {
        return fieldMap.get(key).getText();
      }
    
      // get the mainPanel to place into a JOptionPane
      public JComponent getComponent()
      {
        return mainPanel;
      }
    }
    QueryComponentTest.java
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JComponent;
    
    public class QueryComponentTest
    {
      private static final String[] FIELD_NAMES =
      {
        "First Name", "Second Name", "Phone Number", "What the Heck", "DILLIGAF",
        "Monday", "Tuesday", "Wednesday"
      };
      private JPanel mainPanel = new JPanel();
    
      public QueryComponentTest()
      {
        JButton getNamesBtn = new JButton("Get Names");
        getNamesBtn.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            getNamesAction();
          }
        });
        mainPanel.add(getNamesBtn);
      }
      
      // occurs when button is pressed
      private void getNamesAction()
      {
        // create object to place into JOptionPane
        QueryComponent2 queryComp = new QueryComponent2(FIELD_NAMES);
        
        // show JOptionPane
        int result = JOptionPane.showConfirmDialog(mainPanel, 
            queryComp.getComponent(), // place the component into the JOptionPane 
            "Get Names", 
            JOptionPane.OK_CANCEL_OPTION,
            JOptionPane.PLAIN_MESSAGE);
        if (result == JOptionPane.OK_OPTION) // if ok selected
        {
          // query the queryComp for its contents by calling its getters
          for (int i = 0; i < FIELD_NAMES.length; i++)
          {
            System.out.println(FIELD_NAMES[i] + ": " + queryComp.getFieldText(FIELD_NAMES[i]));
          }
        }
      }
    
      public JComponent getComponent()
      {
        return mainPanel;
      }
    
      private static void createAndShowUI()
      {
        JFrame frame = new JFrame("QueryComponentTest");
        frame.getContentPane().add(new QueryComponentTest().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default Here's the basics using GridBagLayout

    An example is worth a thousand words. I created this with Matisse, so ignore some of the comments.

    Obviously, there are many ways to get there, but this is how I do my forms.

    The labels called mFill_xxx simply take up space, pushing the important contents to the top-left. When you run the app, the screen should be just big enough to hold the labels and text fields. You can make it larger, and nothing will move. Make it smaller, and things scrunch up.

    Patient: "Doctor, when I do this, it hurts!"
    Doctor: "Stop doing that."

    You don't need a GridBagConstraints for every label and text field, as they are all the same. Matisse always creates a new one for every component, but the overhead is negligible.

    Java Code:
    package test;
    
    public class SimpleInputForm
        extends
        javax.swing.JFrame {
    	/** For serialization */
    	private static final long serialVersionUID = -2512071786508396902L;
    
    	public static void main(final String args[]) {
    		java.awt.EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				new SimpleInputForm().setVisible(true);
    			}
    		});
    	}
    
    	//GEN-BEGIN:variables
    	// Variables declaration - do not modify
    	private javax.swing.JLabel mFill_Bottom;
    	private javax.swing.JLabel mFill_Right;
    	private javax.swing.JLabel mLabel_Line1;
    	private javax.swing.JLabel mLabel_Line2;
    	private javax.swing.JLabel mLabel_Line3;
    	private javax.swing.JLabel mLabel_Line4;
    	private javax.swing.JLabel mLabel_Line5;
    	private javax.swing.JPanel mPanel_Main;
    	private javax.swing.JTextField mText_Field1;
    	private javax.swing.JTextField mText_Field2;
    	private javax.swing.JTextField mText_Field3;
    	private javax.swing.JTextField mText_Field4;
    	private javax.swing.JTextField mText_Field5;
    	// End of variables declaration//GEN-END:variables
    	/** Creates new form SimpleInputForm */
    	public SimpleInputForm() {
    		initComponents();
    	}
    
    	//GEN-BEGIN:initComponents
    	// <editor-fold defaultstate="collapsed" desc="Generated Code">
    	private void initComponents() {
    		java.awt.GridBagConstraints gridBagConstraints;
    		mPanel_Main = new javax.swing.JPanel();
    		mLabel_Line1 = new javax.swing.JLabel();
    		mText_Field1 = new javax.swing.JTextField();
    		mFill_Right = new javax.swing.JLabel();
    		mLabel_Line2 = new javax.swing.JLabel();
    		mText_Field2 = new javax.swing.JTextField();
    		mLabel_Line3 = new javax.swing.JLabel();
    		mText_Field3 = new javax.swing.JTextField();
    		mText_Field4 = new javax.swing.JTextField();
    		mLabel_Line4 = new javax.swing.JLabel();
    		mText_Field5 = new javax.swing.JTextField();
    		mLabel_Line5 = new javax.swing.JLabel();
    		mFill_Bottom = new javax.swing.JLabel();
    		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    		getContentPane().setLayout(new java.awt.GridLayout());
    		mPanel_Main.setLayout(new java.awt.GridBagLayout());
    		mLabel_Line1.setText("Label for line:");
    		gridBagConstraints = new java.awt.GridBagConstraints();
    		gridBagConstraints.gridx = 0;
    		gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    		gridBagConstraints.insets = new java.awt.Insets(2,
    		    10,
    		    2,
    		    2);
    		mPanel_Main.add(mLabel_Line1,
    		    gridBagConstraints);
    		mText_Field1.setColumns(20);
    		gridBagConstraints = new java.awt.GridBagConstraints();
    		gridBagConstraints.gridx = 1;
    		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
    		gridBagConstraints.insets = new java.awt.Insets(2,
    		    2,
    		    2,
    		    10);
    		mPanel_Main.add(mText_Field1,
    		    gridBagConstraints);
    		mFill_Right.setText(" ");
    		gridBagConstraints = new java.awt.GridBagConstraints();
    		gridBagConstraints.gridy = 0;
    		gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
    		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    		gridBagConstraints.weightx = 1.0;
    		gridBagConstraints.weighty = 1.0;
    		mPanel_Main.add(mFill_Right,
    		    gridBagConstraints);
    		mLabel_Line2.setText("Label for line:");
    		gridBagConstraints = new java.awt.GridBagConstraints();
    		gridBagConstraints.gridx = 0;
    		gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    		gridBagConstraints.insets = new java.awt.Insets(2,
    		    10,
    		    2,
    		    2);
    		mPanel_Main.add(mLabel_Line2,
    		    gridBagConstraints);
    		mText_Field2.setColumns(20);
    		gridBagConstraints = new java.awt.GridBagConstraints();
    		gridBagConstraints.gridx = 1;
    		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
    		gridBagConstraints.insets = new java.awt.Insets(2,
    		    2,
    		    2,
    		    10);
    		mPanel_Main.add(mText_Field2,
    		    gridBagConstraints);
    		mLabel_Line3.setText("Label for line:");
    		gridBagConstraints = new java.awt.GridBagConstraints();
    		gridBagConstraints.gridx = 0;
    		gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    		gridBagConstraints.insets = new java.awt.Insets(2,
    		    10,
    		    2,
    		    2);
    		mPanel_Main.add(mLabel_Line3,
    		    gridBagConstraints);
    		mText_Field3.setColumns(20);
    		gridBagConstraints = new java.awt.GridBagConstraints();
    		gridBagConstraints.gridx = 1;
    		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
    		gridBagConstraints.insets = new java.awt.Insets(2,
    		    2,
    		    2,
    		    10);
    		mPanel_Main.add(mText_Field3,
    		    gridBagConstraints);
    		mText_Field4.setColumns(20);
    		gridBagConstraints = new java.awt.GridBagConstraints();
    		gridBagConstraints.gridx = 1;
    		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
    		gridBagConstraints.insets = new java.awt.Insets(2,
    		    2,
    		    2,
    		    10);
    		mPanel_Main.add(mText_Field4,
    		    gridBagConstraints);
    		mLabel_Line4.setText("Label for line:");
    		gridBagConstraints = new java.awt.GridBagConstraints();
    		gridBagConstraints.gridx = 0;
    		gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    		gridBagConstraints.insets = new java.awt.Insets(2,
    		    10,
    		    2,
    		    2);
    		mPanel_Main.add(mLabel_Line4,
    		    gridBagConstraints);
    		mText_Field5.setColumns(20);
    		gridBagConstraints = new java.awt.GridBagConstraints();
    		gridBagConstraints.gridx = 1;
    		gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
    		gridBagConstraints.insets = new java.awt.Insets(2,
    		    2,
    		    2,
    		    10);
    		mPanel_Main.add(mText_Field5,
    		    gridBagConstraints);
    		mLabel_Line5.setText("Label for line:");
    		gridBagConstraints = new java.awt.GridBagConstraints();
    		gridBagConstraints.gridx = 0;
    		gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
    		gridBagConstraints.insets = new java.awt.Insets(2,
    		    10,
    		    2,
    		    2);
    		mPanel_Main.add(mLabel_Line5,
    		    gridBagConstraints);
    		mFill_Bottom.setText(" ");
    		gridBagConstraints = new java.awt.GridBagConstraints();
    		gridBagConstraints.gridx = 0;
    		gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
    		gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
    		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
    		gridBagConstraints.weightx = 1.0;
    		gridBagConstraints.weighty = 1.0;
    		mPanel_Main.add(mFill_Bottom,
    		    gridBagConstraints);
    		getContentPane().add(mPanel_Main);
    		pack();
    	}// </editor-fold>
    	//GEN-END:initComponents
    }

  7. #7
    Lindberg is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    I'm new here, i like in the tuto ...
    Thanks Fubarable !!
    Sou novo aqui neste forum e gostei muito deste tutorial.
    Espero poder contribuir, com as duvidas ! e tirar duvidas quando puder ...
    peço desculpas pelo inglês, sei quase nada ...
    Last edited by Lindberg; 11-06-2010 at 04:12 PM.

  8. #8
    Lindberg is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    example of a search on google ...
    http://www.google.com.br/search?&q=get+here+you+param

    I have one that will help you, then you send ...
    Last edited by Lindberg; 12-18-2010 at 07:14 PM.

  9. #9
    Lindberg is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default Open html on desktop

    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;

    public class MostrarSite{
    public static void main(String[] args) throws Exception {
    String url = "http://www.yahoo.com";
    JEditorPane editor = new JEditorPane(url);
    editor.setEditable(false);
    JScrollPane pane = new JScrollPane(editor);
    JFrame f = new JFrame("HTML Demo");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(pane);
    f.setSize(800, 600);
    f.setVisible(true);
    }
    }

Similar Threads

  1. POI changing text to a form label
    By natt in forum Advanced Java
    Replies: 2
    Last Post: 09-19-2008, 11:49 AM
  2. eol in text area....
    By Nicholas Jordan in forum Networking
    Replies: 0
    Last Post: 09-14-2008, 11:59 PM
  3. [SOLVED] Text Area new line problem
    By Jay-1.1 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 05:18 AM
  4. Text Area problem
    By mcal in forum New To Java
    Replies: 0
    Last Post: 02-11-2008, 10:42 PM
  5. textfiled and text area to UTF-16?
    By Mr tuition in forum AWT / Swing
    Replies: 0
    Last Post: 12-04-2007, 01:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •