creating GUI components on the runtime based on user Response

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 850 Views - Last Post: 11 August 2013 - 02:16 PM Rate Topic: -----

#1 murali2489  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 03-August 13

creating GUI components on the runtime based on user Response

Posted 03 August 2013 - 07:11 PM

Hi All,

I am studying Java Swing and want to write simple programs in it.

I have created a Jframe which holds a JPanel. JPanel has two rows, first row has labels like name, date etc and second row has relative textFileds for user input.

This textfield I created inside the constructor is only for one event, I don't know how many events might come in a day, so if a user wants to add another event, he has to click a button and another row with same Textfields like second row has to come.

I only know how to create GUI components before compiletime and don't know how to write code that creates componets on run time.

Please advise.


Below is my code I wrote which creates components on compiletime.



public class MainWindow {
	
	private JLabel jSno;
	private JLabel jNo;
	private JLabel jTicket;
	private JLabel jDate;
	private JLabel jDescription;
	private JLabel jPriority;
	private JLabel jFollowUp;
	private JLabel jState;
	private JLabel jStatus;
	private JTextField jtTicket;
	private JTextField jtDate;
	private JTextArea jtDescription;
	private JTextField jtPriority;
	private JTextField jtState;
	private JTextArea jtFollowUp;
	private JSpinner jSStatus;
	
	String status[] = {"Following","Completed"};
	
	
public MainWindow(){
	
	JFrame jFrm = new JFrame("Events GUI program");
	
	jFrm.setSize(new Dimension(800,200));
	jFrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	jFrm.setVisible(true);
	
	JTabbedPane jtp = new JTabbedPane();
	
	jSno = new JLabel("Index",SwingConstants.CENTER);
	jSno.setBorder(BorderFactory.createEtchedBorder());
	jNo = new JLabel("1",SwingConstants.CENTER);
	jNo.setBorder(BorderFactory.createEtchedBorder());
	jTicket = new JLabel("Ticket No",SwingConstants.CENTER);
	jTicket.setBorder(BorderFactory.createEtchedBorder());
	
	jDate = new JLabel("Date",SwingConstants.CENTER);
	jDate.setBorder(BorderFactory.createEtchedBorder());

	jDescription = new JLabel("Description",SwingConstants.CENTER);
	jDescription.setBorder(BorderFactory.createEtchedBorder());

	jPriority = new JLabel("Priority",SwingConstants.CENTER);
	jPriority.setBorder(BorderFactory.createEtchedBorder());
	
	jState = new JLabel("State",SwingConstants.CENTER);
	jState.setBorder(BorderFactory.createEtchedBorder());

	jFollowUp = new JLabel("FollowUp",SwingConstants.CENTER);
	jFollowUp.setBorder(BorderFactory.createEtchedBorder());
	
	jStatus = new JLabel("Status",SwingConstants.CENTER);
	jStatus.setBorder(BorderFactory.createEtchedBorder());
	
	jtTicket = new JTextField();
	jtDate = new JTextField();
	jtDescription = new JTextArea();
	jtDescription.setLineWrap(true);
	jtPriority = new JTextField();
	jtState = new JTextField();
	jtFollowUp = new JTextArea();
	jtFollowUp.setLineWrap(true);
	jtFollowUp.setWrapStyleWord(true);
	SpinnerListModel mod = new SpinnerListModel(status);
	jSStatus = new JSpinner(mod);
	
	JPanel panel1 = new JPanel();
	panel1.setLayout(new GridLayout(2,8));
	panel1.setOpaque(true);
	
	panel1.add(jSno);
	panel1.add(jTicket);
	panel1.add(jDate);
	panel1.add(jDescription);
	panel1.add(jPriority);
	panel1.add(jState);
	panel1.add(jFollowUp);
	panel1.add(jStatus);
	panel1.add(jNo);
	panel1.add(jtTicket);
	panel1.add(jtDate);
	panel1.add(jtDescription);
	panel1.add(jtPriority);
	panel1.add(jtState);
	panel1.add(jtFollowUp);
	panel1.add(jSStatus);
	
	jtp.addTab("Incidents", panel1);
	JPanel panel2 = new JPanel();
	panel2.setLayout(new GridLayout(2,6));
	
	
	JScrollPane scroll = new JScrollPane(jtp);
	
	scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
	scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
	jFrm.getContentPane().setLayout(new BorderLayout());
	jFrm.add(scroll);
	
	
	
	
}

  public static void main(String[] args) {

	MainWindow ref = new MainWindow();
}

} 

This post has been edited by macosxnerd101: 03 August 2013 - 07:17 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: creating GUI components on the runtime based on user Response

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10470
  • View blog
  • Posts: 38,809
  • Joined: 27-December 08

Re: creating GUI components on the runtime based on user Response

Posted 03 August 2013 - 07:19 PM

You might want to look at using a JTable. You can add a row using its DefaultTableModel.
Was This Post Helpful? 1
  • +
  • -

#3 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,536
  • Joined: 29-July 11

Re: creating GUI components on the runtime based on user Response

Posted 03 August 2013 - 08:55 PM

I didn't read your code, so I don't know what it is doing, but creating components on "run-time" is the same.

public class Main {
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				
				class MainWindow extends JFrame {
					{
						setTitle("Main Window");
						setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
						setLayout(new FlowLayout());
						JButton button1 = new JButton("click to add another button");
						button1.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent e) {
								//adds a buton to the JPanel.
								add(new JButton("added button"));
								pack();
							}
						});
						add(button1);
						pack();
						setVisible(true);
					}
				}//end Mainwindow
				
				new MainWindow();
			}
		});//end SwingUtilities.invokeLater()
		new Main();
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 murali2489  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 03-August 13

Re: creating GUI components on the runtime based on user Response

Posted 04 August 2013 - 09:22 AM

Thanks macosxnerd101, Farrell2k


I will try your suggestions and If I come up with other doubts , I will post again. Thanks :)
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: creating GUI components on the runtime based on user Response

Posted 04 August 2013 - 08:26 PM

"Creating GUI component at run time"

is never a good idea. I won't even bother reading your code
Was This Post Helpful? 0
  • +
  • -

#6 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,536
  • Joined: 29-July 11

Re: creating GUI components on the runtime based on user Response

Posted 04 August 2013 - 09:06 PM

View Postpbl, on 05 August 2013 - 03:26 AM, said:

"Creating GUI component at run time"

is never a good idea.


Oh, come on, now. without the ability to create components at run time, we wouldn't have such wonderful gui designers, :)
Was This Post Helpful? 1
  • +
  • -

#7 murali2489  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 03-August 13

Re: creating GUI components on the runtime based on user Response

Posted 04 August 2013 - 09:07 PM

Hi PBL,

ok,I will try with JTable in my Program...
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: creating GUI components on the runtime based on user Response

Posted 04 August 2013 - 09:11 PM

View Postfarrell2k, on 05 August 2013 - 12:06 AM, said:

Oh, come on, now. without the ability to create components at run time, we wouldn't have such wonderful gui designers, :)/>

And without them we wouldn't have to waste our time talking about their completly useless existence and dumb newbies (or more experienced so called programmers) wouldn't be wasting their time defending them
Was This Post Helpful? 0
  • +
  • -

#9 murali2489  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 03-August 13

Re: creating GUI components on the runtime based on user Response

Posted 04 August 2013 - 10:16 PM

Hi PBL, Farrell2k

I am new to Java, that's why so many doubts crosses my mind, so as you were saying, I need to put some buttons at each row at the end, A JRadio button having yes or no and depending upon that selection I will trigger some more actions, can I do that while having JTable and at the end of each row comes these buttons ?
Was This Post Helpful? 0
  • +
  • -

#10 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 823
  • View blog
  • Posts: 2,536
  • Joined: 29-July 11

Re: creating GUI components on the runtime based on user Response

Posted 04 August 2013 - 10:34 PM

View Postmurali2489, on 05 August 2013 - 05:16 AM, said:

Hi PBL, Farrell2k

I am new to Java, that's why so many doubts crosses my mind, so as you were saying, I need to put some buttons at each row at the end, A JRadio button having yes or no and depending upon that selection I will trigger some more actions, can I do that while having JTable and at the end of each row comes these buttons ?


Swing is versatile enough that you can probably do just about anything you want, but I have no idea what you're planning and why in the world you want to create components at run-time.
Was This Post Helpful? 0
  • +
  • -

#11 murali2489  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 03-August 13

Re: creating GUI components on the runtime based on user Response

Posted 04 August 2013 - 10:53 PM

Hi Farrell2k ,

Sorry I didn't explain clearly the idea of my project in the first post,

The thing is im working in administration project and few months I got the taste of Java and really wanted to move into Java project so badly, so I have decided to make a software for my project after studying so that they will switch me to Java Project . Now I have studied the basics of Core Java and Swing.


The idea about my project is everyday many incidents comes to our team like tickets and we work on it and resolve it. But we wont know how many incidents will come in a Day in advance. I want to create a GUI app which tracks the information of these incidents like name, number, description and what is the action taken, and radio buttons for yes or no whether they are following up on the incidents or not.

As you were asking why I need to create components in the runtime, if I don't know how may issues may come in a day, I have only option to create in during runtime... am I correct ? please correct me if im wrong...
Was This Post Helpful? 0
  • +
  • -

#12 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: creating GUI components on the runtime based on user Response

Posted 04 August 2013 - 11:06 PM

An incident should be a record (like in a data base), probably an instance of a class that stores the details you listed, not a new component in the GUI. The GUI could show the details of the current or some number of records with a number that indicates the number of open incidents. There are many possibilities.
Was This Post Helpful? 0
  • +
  • -

#13 murali2489  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 03-August 13

Re: creating GUI components on the runtime based on user Response

Posted 04 August 2013 - 11:37 PM

Attached Image

Please see the image attachment,that is the Gui I created using the code I posted earlier, an incident comes to our team and we update the details in the textfields which in turn gets saved in the database. That pic I attached is for only one event, if another incident comes, then how can I add it into the app which in turn gets saved in the database.
Was This Post Helpful? 0
  • +
  • -

#14 murali2489  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 03-August 13

Re: creating GUI components on the runtime based on user Response

Posted 04 August 2013 - 11:45 PM

View Postmurali2489, on 04 August 2013 - 11:37 PM, said:

Attachment event.jpg

Please see the image attachment,that is the Gui I created using the code I posted earlier, an incident comes to our team and we update the details in the textfields which in turn gets saved in the database. That pic I attached is for only one event, if another incident comes, then how can I add it into the app which in turn gets saved in the database.



As I said in the previous post, if a user logs in he should able to see in the application the incidents which are currently following up by our team, which is nothing but the incidents which was last saved by any users in our team. if an incident was resolved, that row should be removed from the app and also its contents in the database.
Was This Post Helpful? 0
  • +
  • -

#15 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: creating GUI components on the runtime based on user Response

Posted 05 August 2013 - 03:23 AM

So you want to populate a tabular display with the contents of a database, updating as changes to the database occur, possibly multi-user (but not sure on that point). Sounds like you want to build a JTable to be the front end of some kind of database, either your own simple file structure or maybe an SQL database.

Once you firm up the requirements in your mind - you could even write them down - the design may become clearer.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2