Results 1 to 11 of 11
  1. #1
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default jtextfield setcolumns

    Java Code:
    		txActName.setColumns(10);
    
    		txActName.setText(tm1.getValueAt(selectedRow2,0).toString());
    When the value is retrieved from a column with 50-length char data type, this works fine, however I need it to be 150-length char data type column and this cause the value to be flushed right away from the textfield (showed like less than a sec and textfield is cleared).

    Is there any solution to this ?


    Thanks in advance

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Not sure I understand your question.

    The setColumns(...) method is used to determine the preferred size of the text field. It does not limit the number of characters that can be added to the Document.

    If you need more help then you need to post your Short, Self Contained, Correct Example that demonstrates the problem.

  3. #3
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default

    I have a textfield (txActName) which listen to selection in my table. When the table cell is selected it'll populate the value in the textfield.
    The table itself takes data from MS SQL.
    First, I set the table column in the SQL database as char with 50 length and that works fine...the value is shown in the textfield.
    Then however, I think I need it more than just 50 length...I need 150, so I changed the database table column type as 150 length.
    And problem arises...when I click on the table the value of that column shows like less than a sec and then gone...so the textfield remains empty.

    I'll try to get an example posted shortly.


    Thanks for the reply.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    I'll try to get an example posted shortly.
    Remember, we don't has access to your database, so your SSCCE should "demonstrate" the problem without using a database.

  5. #5
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default

    Java Code:
    public class test extends JFrame {
    
    	private JPanel contentPane;
    	private JTextField txActName;
    	QueryTableModel qtm = new QueryTableModel();
    	JTable tableA = new JTable(qtm);
    
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					test frame = new test();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
    
    	public test() throws SQLException {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
    		
    		txActName = new JTextField();
    		txActName.setBounds(5, 5, 432, 20);
    		contentPane.add(txActName);
    		txActName.setColumns(10);
    		
    		JScrollPane scrollPane = new JScrollPane();
    		scrollPane.setBounds(5, 36, 427, 219);
    		contentPane.add(scrollPane);
    		
    		scrollPane.setViewportView(tableA);
    		tableA.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    		qtm.setQuery("SELECT dbcoltype FROM test");
    		tableA.setFillsViewportHeight(true);
    		tableA.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    		ListSelectionModel rowAM = tableA.getSelectionModel();
    		rowAM.addListSelectionListener(new ListSelectionListener(){
    			public void valueChanged(ListSelectionEvent actquery) {
    				tableA.getModel().removeTableModelListener(tableA);
    				tableA.clearSelection();
    				ListSelectionModel lsm1 = (ListSelectionModel)actquery.getSource();
    				int selectedRow2 = lsm1.getLeadSelectionIndex();
    				TableModel tm1 = tableA.getModel();
    				txActName.setText(tm1.getValueAt(selectedRow2,0).toString());
    			}
    		});
    	}
    }
    Last edited by Levian; 07-30-2011 at 05:26 AM.

  6. #6
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    Remember, we don't has access to your database, so your SSCCE should "demonstrate" the problem without using a database.
    The problem is when changing database column length, so I don't know how can I do that without involving the database.
    Anyway the database I use here is just 1 table (test) with 1 column (dbcoltype).

    When the dbcoltype is a 50-length char the data is shown in the txActName just fine, but when I change the type into a 150-length char, then problem exist.
    Last edited by Levian; 07-30-2011 at 05:28 AM.

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    so I don't know how can I do that without involving the database.
    Your question is about displaying data in a text field when you select a row in the table. You don't need a database to add data to a TableModel. You can hardcode data by doing something like:

    Java Code:
    table.setValueAt("1234....", 0, 0);

  8. #8
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default

    OK, I'm experimenting with the column length and I find that it's more to be said the value is cut on the long data...like abcdefghijklmnoprstuvwxyz might get cut and shows ghi...like it's rolled to the left and gone from the textfield view, hence I get the blank textfield but I can just go with left keypad to get what is actually in there.

    confirmed, but how to get textfield to show the value without the empty spaces ? or by other word showing from the left instead of the right ?
    Last edited by Levian; 07-30-2011 at 05:44 AM.

  9. #9
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    I still don't know what you mean. A text field does not show "...".

    I don't know what the "empty spaces" are that you are talking about.

    Maybe you need to use the String.trim() method to get rid of spaces at the end of your text.

    Or you can use textField.setCaretPosition(0) so the texgt is displayed from the beginning.

    Post a SSCCE that demonstrates your problem if you want more help. I don't have time to guess what you are trying to explain.

    Also the setColumns(...) method does nothing because you are not using a layout manager.
    Last edited by camickr; 07-30-2011 at 05:49 AM.

  10. #10
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    Your question is about displaying data in a text field when you select a row in the table.
    Ah hardcoded, sorry...I was quite confused enough to forget that and yes, it's about displaying data in the textfield.

  11. #11
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    Maybe you need to use the String.trim() method to get rid of spaces at the end of your text.
    Solved, trim() works...never knew on this means I need more to read.


    Thanks a lot

Similar Threads

  1. Problem with setColumns in JTextField
    By shai in forum New To Java
    Replies: 3
    Last Post: 07-26-2010, 05:41 AM
  2. JTextField
    By gancio in forum AWT / Swing
    Replies: 20
    Last Post: 08-26-2009, 03:11 PM
  3. Replies: 1
    Last Post: 01-30-2009, 06:44 PM
  4. JtextField
    By kashifu in forum Advanced Java
    Replies: 2
    Last Post: 06-27-2008, 04:25 PM
  5. help with JTextfield
    By gary in forum New To Java
    Replies: 4
    Last Post: 07-11-2007, 01:58 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
  •