Results 1 to 11 of 11
  1. #1
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default How to insert new text,box from menu

    Hi. ill just show the code.
    from my menuclass
    Java Code:
    aMenuItem.addActionListener(new ActionListener() {
    		public void actionPerformed(ActionEvent e){
    		if(e.getActionCommand().equals("New row")){
    		createrow();
    it calls the method createrow();
    Java Code:
    public void createrow() {
    		
    		aTextField = new JTextField("", 30);
    		aTextField.setBorder(BorderFactory.createLineBorder(Color.black));
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.ipady = 1;       //reset to default
    		c.weighty = 1.0;   //request any extra vertical space
    		c.anchor = GridBagConstraints.WEST; //bottom of space
    		c.insets = new Insets(5,0,0,10);  //top padding
    		c.gridy = 1; // what row.
    		aPanel0.add(aTextField, c); //add TextField 1 to Panel
    		
                    selDbType1 = new JComboBox(dbType1);
    		c.fill = GridBagConstraints.HORIZONTAL;
    		c.ipady = 1;       //reset to default
    		c.weighty = 1.0;   //request any extra vertical space
    		c.anchor = GridBagConstraints.EAST; //bottom of space
    		c.insets = new Insets(5,0,0,10);  //top padding
    		c.gridy = 1; // what row
    		selDbType1.setPreferredSize(new Dimension(80, 20)); // set the size for dropmenu
    		selDbType1.setSelectedIndex(2); // set to 3 choice
    		selDbType1.addActionListener(new ActionListener(){ // add action handler to combobox
    	}
    Can it be done this way. i also need c.gridy to be dynamic so it pops up at end of page. thankfull for any advice.
    EDIT. Mabey should explain better what i want it to do. When a user pushes "new row" i want a new row containing 1 textfield and 1 combobox to insert at bottom of panel. they both need a uniqe id.
    Last edited by ocean; 10-03-2009 at 12:15 PM.

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

    Default

    I'm having a bit of difficulty figuring out just what your problem is, but that could be my fault as it's Saturday morning, and I may have had a few beers too many last night.

    Regardless, consider posting an SSCCE, a very short compilable and runnable program that depends on no outside files, data, images, that demonstrates your problem and that can be posted here (see the link for details). Then we can play with your code and more quickly understand the problems and more quickly help find a solution.

    Much luck!

    edit: it almost sounds as if you'd be better off using a JTable and a custom table model (derived from DefaultTableModel) as well as custom cell editor and renderer to allow comboboxes. Have you considered this?

    If you want to see something like this in action, have a look at the Sun JTable tutorial here: JTable Tutorial. Look especially at the section marked "Using a Combo Box as an Editor". Then look at the source code for the demo program, as it's all there: http://java.sun.com/docs/books/tutor...enderDemo.java

    Again, much luck!
    Last edited by Fubarable; 10-03-2009 at 03:41 PM.

  3. #3
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Okey. this is something i just wrote quick to show my point.
    Java Code:
    package test;
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class test {
    	JTextField aTextField;
    	public JMenuBar aOceanMenu() {
    		// Start arkiv and exit
    		JMenuBar aMenuBar = new JMenuBar();
    		JMenu aMenu = new JMenu("Arkiv");
    		JMenuItem aMenuItem = new JMenuItem("New row");
    		aMenuItem.addActionListener(new ActionListener() {
    		public void actionPerformed(ActionEvent e){
    		if (e.getActionCommand().equals("New Row")){
    		createrow();
    		} // end getAction
    		}});
    		aMenu.add(aMenuItem);
    		aMenuBar.add(aMenu);
    		return aMenuBar;					
    }	// end JMenu
    	
    	public JPanel thingsGUI() {
    		  // Things to be on oceanGUI frame
    		JLabel aLabel = new JLabel("hello");
    		JPanel aPanel = new JPanel(new BorderLayout());
    		aPanel.add(aLabel, BorderLayout.WEST);
                           aTextField = new JTextField("1", 30);
    		aPanel.add(aTextField, BorderLayout.EAST);
    		return aPanel;
    		} // end gui-things
    	
    	public void createrow() {  
    		JTextField aTextField;
    	           JPanel aPanel = new JPanel(new BorderLayout());
    		aTextField = new JTextField("2", 30);
    		aPanel.add(aTextField, BorderLayout.EAST);
    		System.out.println("calls");
    		}
    	
    	private static void oceanGUI() {
    		// create and show the gui
    		JFrame aFrame = new JFrame("myGui 2");
    		aFrame.setSize(600, 600);
    		aFrame.setVisible(true);
    		aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		test frame = new test();
    		aFrame.add(frame.thingsGUI(), BorderLayout.NORTH);
    		test gui = new test();
    		aFrame.setJMenuBar(gui.aOceanMenu());
    	    aFrame.pack();
    	} // end oceanGui
    
    	public static void main(String[] args) {
    		// Run gui();
    		SwingUtilities.invokeLater(new Runnable() {
    		public void run() {
    	 	oceanGUI();
    
    		} // end public void run()
    	}// end invokeLater
    );}// end viod main
    }// end
    When the user push "New row" in menu i want the method createrow() to be called. and a new TextField to apper in my panel. Does this make any sence? :cool:

    EDIT: Will have a look at those links.
    EDIT: Mabey JTable can be used to list and edit the text i put in the file. But to enter it I dont think it sutible.
    Last edited by ocean; 10-03-2009 at 04:49 PM.

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

    Default

    Also, your code posted above is not an SSCCE (it won't compile for one) and this will make it much harder for us to help you. I strongly suggest you read the link and abide by its rules. Doing so will increase your chances of getting better help faster.
    Last edited by Fubarable; 10-03-2009 at 04:37 PM.

  5. #5
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Sorry did miss the import and package. Corrected that now! It runs.

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

    Default

    For eg, here's an "almost" SSCCE (it's too long):
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class AddNewRow {
      private static final Dimension MAIN_SIZE = new Dimension(400, 300);
      private JPanel mainPanel = new JPanel(new BorderLayout(5, 5));
      private JMenuBar menuBar = new JMenuBar();
      private ColumnPanel columnPanel = new ColumnPanel();
      private int rowCount = 0;
    
      public AddNewRow() {
        mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        mainPanel.setPreferredSize(MAIN_SIZE);
        mainPanel.add(new JScrollPane(columnPanel.getComponent()));
    
        JMenu aMenu = new JMenu("Arkiv");
        JMenuItem aMenuItem = new JMenuItem("New row");
        aMenuItem.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            createrow();
          }
        });
        aMenu.add(aMenuItem);
        menuBar.add(aMenu);
    
      }
    
      private void createrow() {
        JTextField textField = new JTextField(String.valueOf(rowCount + 1), 12);
        rowCount++;
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel.add(textField);
        columnPanel.addRow(panel);
    
      }
    
      public JComponent getComponent() {
        return mainPanel;
      }
    
      public JMenuBar getJMenuBar() {
        return menuBar;
      }
    
      private static void createAndShowUI() {
        AddNewRow addNewRow = new AddNewRow();
        JFrame frame = new JFrame("AddNewRow");
        frame.getContentPane().add(addNewRow.getComponent());
        frame.setJMenuBar(addNewRow.getJMenuBar());
        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();
          }
        });
      }
    }
    
    class ColumnPanel {
      private JPanel mainPanel = new JPanel();
    
      public ColumnPanel() {
        mainPanel.setLayout(new GridLayout(0, 1, 5, 5));
      }
    
      public JComponent getComponent() {
        return mainPanel;
      }
    
      public void addRow(JComponent component) {
        mainPanel.add(component);
        mainPanel.revalidate();
      }
    }

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

    Default

    Quote Originally Posted by ocean View Post
    Sorry did miss the import and package. Corrected that now! It runs.
    it runs, but aPanel is local only to a method and is completely invisible to the rest of the class.

  8. #8
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Thank you Fubarable. Will follow that rule from now on. By the way. how do i put a post to Solved?

  9. #9
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Hmm you really did put me to work here. Think i finally understand most of your code. I have some questions, but I save them for later. will try and solv it first.

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

  11. #11
    ocean is offline Senior Member
    Join Date
    Sep 2009
    Location
    Sweden/Borås
    Posts
    107
    Rep Power
    0

    Default

    Yes for my "use" and "edit functions i will use Jtable. but this is perfect for the "create" part. Im trying to make a program that you can make to what ever you like. eg if you want a adress book you go in and add how many field you want ex name, adress phone. save it. then open it and use(put in information) edit(change/look at information) and so on. But after all, this is my first week in the Java world and I have so much to learn. Thank you for taking your time to help us that are new.

Similar Threads

  1. Help! - How to insert a new line to a text file
    By matpj in forum New To Java
    Replies: 13
    Last Post: 02-24-2010, 06:28 PM
  2. Replies: 3
    Last Post: 02-28-2009, 10:17 AM
  3. Insert text at abitrary point in a JTextPane
    By angues in forum AWT / Swing
    Replies: 6
    Last Post: 02-02-2009, 05:08 PM
  4. Fill a menu dynamically when menu is shown
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 05:47 PM
  5. Replies: 7
    Last Post: 05-09-2008, 08:54 AM

Posting Permissions

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