4 Replies - 1688 Views - Last Post: 09 March 2013 - 03:24 PM Rate Topic: -----

#1 Bivcala  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 31
  • Joined: 23-November 11

How to combine JList, Textfield and Jbutton in java?

Posted 09 March 2013 - 01:47 PM

I am creating a program where the user selects an item from a list, then the user enters a text to a TextField and then they press a button which then alerts the user of which item was selected alongside the text from the user.However, this seems to not work when it comes to the selection of the list item.

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;


public class fruitList extends JFrame implements ListSelectionListener
{
    private JTextField textField;
	private JList<String> fruitList;
	private JButton buttonwhich;
	private static fruitList  frame;

 public static void main(String[] args)
    {
	 
	 	fruitList  frame = new fruitList();
	 	frame.setTitle("Fruit List");
		frame.setSize(350,150);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
   }
   public fruitList()
   {
	   
	  final String[] FRUIT_NAME = {"Banana", "Apple", "Orange"};
	  
      setLayout(new FlowLayout());
      buttonwhich = new JButton("Which");
      fruitList = new JList<String>(FRUIT_NAME);
      fruitList.setVisibleRowCount(2); 
      add(new JScrollPane(fruitList));
      fruitList.addListSelectionListener(this);
      
      textField = new JTextField(10);

      add(textField);
      add(buttonwhich);
      ButtonHandler handler = new ButtonHandler();
      buttonwhich.addActionListener(handler);
  
   }  
   class ButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {

			JOptionPane.showMessageDialog(frame,
					"You Pressed \t" + e.getActionCommand());

		}
   }
}



This post has been edited by Bivcala: 09 March 2013 - 01:50 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to combine JList, Textfield and Jbutton in java?

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: How to combine JList, Textfield and Jbutton in java?

Posted 09 March 2013 - 01:49 PM

What specific problems or errors are you encountering? Telling us that it doesn't work isn't descriptive.
Was This Post Helpful? 0
  • +
  • -

#3 Bivcala  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 31
  • Joined: 23-November 11

Re: How to combine JList, Textfield and Jbutton in java?

Posted 09 March 2013 - 01:57 PM

View Postmacosxnerd101, on 09 March 2013 - 01:49 PM, said:

What specific problems or errors are you encountering? Telling us that it doesn't work isn't descriptive.

I am having problems with combining getting the selected item from the list and getting what text was inputted in to the textfield and then showing which item and what text was entered in the textfield when the button is pressed via the JOptionPane.

This post has been edited by Bivcala: 09 March 2013 - 01:58 PM

Was This Post Helpful? -1
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10596
  • View blog
  • Posts: 39,257
  • Joined: 27-December 08

Re: How to combine JList, Textfield and Jbutton in java?

Posted 09 March 2013 - 02:09 PM

Your code really shouldn't compile as you don't implement the valueChanged() method from ListSelectionListener.

Also, the JOptionPane button just closes the JOptionPane. It really doesn't trigger ActionEvents for your ActionListener.

Quote

I am having problems with combining getting the selected item from the list and getting what text was inputted in to the textfield

There are getter methods for both these classes. Read the documentation for specifics.

Lastly, the static frame variable is unnecessary and rather poor practice. Avoid static whenever possible. If you cannot justify why you have a static variable, you don't need it.
Was This Post Helpful? 2
  • +
  • -

#5 pbl  Icon User is offline

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

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

Re: How to combine JList, Textfield and Jbutton in java?

Posted 09 March 2013 - 03:24 PM

The only think that should be static in your code is the main() method
all the other ones declared as statiuc just proove that you missed the concept behind OO programming concepts. You shoulkd review these before trying to write a GUI.

And what macosxnerd101 wrote in hist first post is the thruth... you like it or not but he certainly does not desserve a -1 for trying to help you
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1