NetBeans Forums
| View previous topic :: View next topic |
| Author |
Message |
CollinRyans
Joined: 12 Dec 2009 Posts: 3
|
Posted: Sat Dec 12, 2009 10:58 pm Post subject: How do I use arrays in NetBeans? |
|
|
Hi, I am completely new to netbeans. I am normally used to working with JCreator and used to the line by line input/output interface. For a school project I have to make a GUI, and I chose to use NetBeans. However I am having trouble implementing the codes I already made with JCreator onto NetBeans with a GUI.
The code I used with JCreator is:
| Code: | import java.util.Scanner;
public class Sorting_Assignment{
public static void main(String[] args){
String[] strName;
boolean blnSentinel = false;
int intNumberOfNames = 0;
strName = new String[10000];
Scanner input = new Scanner(System.in);
System.out.println("Type '!' to finish\n");
for (int i = 0; blnSentinel != true; i++){
System.out.print("Enter name(" + (i+1) + "): ");
strName[i] = input.next();
intNumberOfNames = i;
if(strName[i].equalsIgnoreCase("!")){
blnSentinel = true;
}
}
System.out.println();
int i, j;
String t;
for (i = 1; i < intNumberOfNames; i++){
j = i;
t = strName[j];
while (j > 0 && (int)(strName[j - 1].toUpperCase()).charAt(0) > (int)(t.toUpperCase()).charAt(0)){
strName[j] = strName[j - 1];
j--;
}
strName[j] = t;
}
for (i = 0; i < intNumberOfNames; i++){
System.out.println(strName[i]);
}
}
} |
What it basically does is get the user to input names, and then view them in alphabetical order (the algorithm isn't perfect but that's beside the point).
The GUI I designed for this program with NetBeans is this:
What I want to do is the same thing. The user should be able to input some names into the textfield and then after the person is finished he/she will click sort and the output will be shown in the textarea sorted in alphabetical order.
After some trial and error to figure out how to use the basics of building and programming a GUI in NetBeans, I still havn't figured out how to make the program work.
Any help will be tremendously appreciated. |
|
| Back to top |
|
 |
Christopher Lam Posted via mailing list.
|
Posted: Sun Dec 13, 2009 4:32 pm Post subject: Re: How do I use arrays in NetBeans? |
|
|
Hi,
There's no need to write you own sorting method, Arrays and Collections classes already provided. Also, in the GUI, I thought using a JList is more appropriate than a TextArea, so here is the code to do what you described but the way I organized the code and the naming convention is not the best, I whipped it up within a short time just to give you some idea. Note the code below should compile and run properly.
package sortnames;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
/**
*
* @author Christopher Lam
*/
public class Main extends WindowAdapter implements ActionListener
{
JTextField txtName = new JTextField(15);
JButton enterButt = new JButton("Enter");
JButton sortButt = new JButton("Sort");
JList nameList = new JList(new NameListModel());
public Main()
{
JFrame frame = new JFrame("Enter names");
frame.setBounds(10, 10, 450, 400);
frame.addWindowListener(this);
Container mainPane = frame.getContentPane();
mainPane.setLayout(new BorderLayout());
JPanel topPanel = new JPanel(new FlowLayout());
topPanel.add(txtName);
topPanel.add(enterButt);
topPanel.add(sortButt);
mainPane.add(topPanel, BorderLayout.NORTH);
mainPane.add(nameList, BorderLayout.CENTER);
NameListModel model = (NameListModel)nameList.getModel();
enterButt.addActionListener(this);
sortButt.addActionListener(this);
frame.setVisible(true);
}
@Override
public void windowClosing(WindowEvent e)
{
super.windowClosed(e);
e.getWindow().dispose();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// TODO code application logic here
new Main();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource().equals(sortButt))
{
NameListModel model = (NameListModel)nameList.getModel();
model.sort();
}
else if (e.getSource().equals(enterButt))
{
NameListModel model = (NameListModel)nameList.getModel();
model.addName(txtName.getText());
txtName.setText("");
txtName.requestFocus();
}
//throw new UnsupportedOperationException("Not supported yet.");
}
}
class NameListModel implements ListModel
{
ArrayList<String> array = new ArrayList<String>();
ListDataListener listener = null;
public NameListModel()
{
}
private void updateList()
{
if (listener != null)
{
listener.contentsChanged(new ListDataEvent(this,
ListDataEvent.CONTENTS_CHANGED, 0, array.size()-1));
}
}
public void addName(String name)
{
array.add(name);
updateList();
}
public void sort()
{
Collections.sort(array);
updateList();
}
public int getSize()
{
return array.size();
//throw new UnsupportedOperationException("Not supported yet.");
}
public Object getElementAt(int index)
{
return array.get(index);
//throw new UnsupportedOperationException("Not supported yet.");
}
public void addListDataListener(ListDataListener l)
{
//throw new UnsupportedOperationException("Not supported yet.");
listener = l;
}
public void removeListDataListener(ListDataListener l)
{
//throw new UnsupportedOperationException("Not supported yet.");
}
}
Regards,
Christopher Lam
On 13-Dec-2009, at 6:58 AM, CollinRyans wrote:
| Quote: | Hi, I am completely new to netbeans. I am normally used to working with JCreator and used to the line by line input/output interface. For a school project I have to make a GUI, and I chose to use NetBeans. However I am having trouble implementing the codes I already made with JCreator onto NetBeans with a GUI.
The code I used with JCreator is:
Code:
import java.util.Scanner;
public class Sorting_Assignment{
public static void main(String[] args){
String[] strName;
boolean blnSentinel = false;
int intNumberOfNames = 0;
strName = new String[10000];
Scanner input = new Scanner(System.in);
System.out.println("Type '!' to finish\n");
for (int i = 0; blnSentinel != true; i++){
System.out.print("Enter name(" + (i+1) + "): ");
strName[i] = input.next();
intNumberOfNames = i;
if(strName[i].equalsIgnoreCase("!")){
blnSentinel = true;
}
}
System.out.println();
int i, j;
String t;
for (i = 1; i < intNumberOfNames; i++){
j = i;
t = strName[j];
while (j > 0 && (int)(strName[j - 1].toUpperCase()).charAt(0) > (int)(t.toUpperCase()).charAt(0)){
strName[j] = strName[j - 1];
j--;
}
strName[j] = t;
}
for (i = 0; i < intNumberOfNames; i++){
System.out.println(strName[i]);
}
}
}
What it basically does is get the user to input names, and then view them in alphabetical order (the algorithm isn't perfect but that's beside the point).
The GUI I designed for this program with NetBeans is this:
[Image: http://img121.imageshack.us/img121/5225/68801148.png ]
What I want to do is the same thing. The user should be able to input some names into the textfield and then after the person is finished he/she will click sort and the output will be shown in the textarea sorted in alphabetical order.
After some trial and error to figure out how to use the basics of building and programming a GUI in NetBeans, I still havn't figured out how to make the program work.
Any help will be tremendously appreciated.
|
|
|
| Back to top |
|
 |
neji
Joined: 21 Jun 2010 Posts: 1
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|