File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to create array of JButton and JLabel ?

 
Yatin Shah
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello sir,

Here, I am giving my code. I am trying to create array of JButton and JLabel. But it's throwing NullPointerException. My code is not working. Please, check and tell me what's worang with my code? I want to display only those number of button and label that much lines are in List.txt file. So, it will work like dynamic array. It will rise while I will Increse in List.txt.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.lang.*;
import java.io.*;
import java.lang.*;

/* <applet code=ServerInterface width=800 height=525 > </applet> */

public class ServerInterface extends JApplet
{

JFrame f,ipList;
JLabel ln,lsn,lip,sil;
JLabel lsi[];
JPanel pc,pcs,si,ipPanel,lPanel,bPanel,nPanel,sPanel,ePanel,wPanel;
JButton sb[];
JButton badd,bedit,bdelete,bexit;
String msg;
JTextArea jta;

public void init()
{

Container contentpane=getContentPane();
contentpane.add(nPanel = new JPanel(), BorderLayout.NORTH);
contentpane.add(ePanel = new JPanel(), BorderLayout.EAST);
contentpane.add(wPanel = new JPanel(), BorderLayout.WEST);
contentpane.add(sPanel = new JPanel(), BorderLayout.SOUTH);

nPanel.add((ln=new JLabel(" SERVER MONITORING SYSTEM")));
nPanel.setBackground(Color.pink);
wPanel.setBackground(Color.pink);
nPanel.setBackground(Color.pink);
sPanel.setBackground(Color.pink);
Font titleFont = new Font("Monotype Corsiva", Font.BOLD, 36);
ln.setFont(titleFont);

contentpane.add(pc = new JPanel());
pc.setLayout(new FlowLayout(FlowLayout.CENTER));
pc.setLayout(new GridLayout(1,2));
pc.setBackground(Color.cyan);


jta=new JTextArea();
pc.add(jta);
jta.setEditable(false);

jta.setLayout(new FlowLayout(FlowLayout.CENTER));
jta.add(si = new JPanel(), BorderLayout.NORTH);
si.setBackground(Color.cyan);
si.add(sil = new JLabel(" System Information"), BorderLayout.NORTH);
sil.setForeground(Color.blue);
sil.setFont(new Font("Courier New",1,21));

pc.add(pcs=new JPanel());
pcs.setLayout(new FlowLayout(FlowLayout.CENTER));
pcs.setBackground(Color.cyan);
pcs.add(ipPanel = new JPanel(), BorderLayout.NORTH);
ipPanel.setBackground(Color.cyan);

pcs.add(lPanel = new JPanel());
lPanel.setBackground(Color.cyan);

pcs.add(bPanel = new JPanel(), BorderLayout.SOUTH);
bPanel.setBackground(Color.cyan);

ipPanel.add(lsn=new JLabel(" Server Name"));
lsn.setFont(new Font("Courier New",1,21));

ipPanel.add(lip=new JLabel(" IP Address"));
lip.setFont(new Font("Courier New",1,21));

String[] s = new String[100];
FileReader fr;
BufferedReader br;
int lineNo = 0,noLines;

try
{
fr = new FileReader("List.txt");
br = new BufferedReader(fr);

while((s[lineNo]=br.readLine()) != null)
lineNo++;

noLines = lineNo + 3;

lPanel.setLayout(new GridLayout(noLines,2));

for(int k = 0; k < 25; k++)
{
lPanel.add(sb[k]=new JButton("Telent"));

for(int i = 0; i < lineNo; i++)
{
if(i != -1)
{
lPanel.add(lsi[k]=new JLabel(s[i]));
}
}
}
br.close();
fr.close();

MyMainEventHandlerDesign handler = new MyMainEventHandlerDesign(this);

for(int j = 0; j < lineNo; j++)
{
sb[j].addActionListener(handler);
}

badd.addActionListener(handler);
bedit.addActionListener(handler);
bdelete.addActionListener(handler);
bexit.addActionListener(handler);
}


catch(NullPointerException npe)
{
System.out.println("Error : "+npe);
}

catch(FileNotFoundException fnfe)
{
System.out.println("File not found.... try another");
}

catch(IOException ioe)
{
System.out.println("I/0 Exception while reading file");
}

bPanel.add(badd=new JButton("ADD"));
bPanel.add(bedit=new JButton("EDIT"));
bPanel.add(bdelete=new JButton("DELETE"));
bPanel.add(bexit=new JButton("EXIT"));
}
public void doitnow()
{
if(msg.equals("ADD"))
{
f = new NewServerEntry("Add a Server",this);
}

if(msg.equals("EDIT"))
{
ipList = new EditServer("Edit a Server",this);
}

if(msg.equals("DELETE"))
{
ipList = new EditServer("Delete a Server",this);
}

if(msg.equals("EXIT"))
{
setVisible(false);
}
}
}


class MyMainEventHandlerDesign implements ActionListener
{
ServerInterface si;

public MyMainEventHandlerDesign(ServerInterface ob)
{
si=ob;
}
public void actionPerformed(ActionEvent ae)
{
String msg=(String)ae.getActionCommand();
si.msg=msg;
si.doitnow();
}
}


class NewServerEntry extends JFrame
{
ServerInterface si;
String arg;
JLabel lsn,lip;
JTextField tsn,tip;
JButton bok,bcancel;

NewServerEntry(String title, ServerInterface ob)
{
super(title);
si=ob;
Font addFont = new Font("Courier New", Font.BOLD, 18);

lsn=new JLabel("Enter Server Name :");
lsn.setFont(addFont);

lip=new JLabel("Enter IP Address :");
lip.setFont(addFont);

bok=new JButton("OK");
bcancel=new JButton("CANCEL");

tsn=new JTextField(20);
tip=new JTextField(20);

setSize(300,150);
setVisible(true);

goAhead();
}


public void goAhead()
{

Container contentpanense=getContentPane();

JPanel LabelPanel = new JPanel();
contentpanense.add(LabelPanel, BorderLayout.CENTER);
LabelPanel.setBackground(Color.white);
LabelPanel.setLayout(new GridLayout(2,2));
Border labelPanelBorder = BorderFactory.createTitledBorder("Add New Server");
LabelPanel.setBorder(labelPanelBorder);

LabelPanel.add(lsn);
LabelPanel.add(tsn);

LabelPanel.add(lip);
LabelPanel.add(tip);

JPanel ButtonPanel = new JPanel();
contentpanense.add(ButtonPanel, BorderLayout.SOUTH);

ButtonPanel.add(bok);
ButtonPanel.add(bcancel);

MyEventHandlerNse handler=new MyEventHandlerNse(this);
bok.addActionListener(handler);
bcancel.addActionListener(handler);
}


public void doitnowNse()
{
if(arg.equals("OK"))
{

}

if(arg.equals("CANCEL"))
{
setVisible(false);
}
}
}

class EditServer extends JFrame
{
ServerInterface si;
String arg;
JList IPlist;
JTextField valueField;
JButton bok,bcancel;

public EditServer(String title,ServerInterface ob)
{
super(title);
si=ob;

Container content = getContentPane();

/*
Create the JList, set the numer of visible rows, add a listener,
and put it in a JScrollPane.
*/

String[] s = new String[100];
FileReader fr;
BufferedReader br;
int lineNo = 0,noLines;

try
{
fr = new FileReader("List.txt");
br = new BufferedReader(fr);

while((s[lineNo]=br.readLine()) != null)
lineNo++;

IPlist = new JList(s);

br.close();
fr.close();

Font displayFont = new Font("Courier New", Font.BOLD, 18);
IPlist.setVisibleRowCount(3);
IPlist.setFont(displayFont);
IPlist.addListSelectionListener(new ValueReporter());

JScrollPane listPane = new JScrollPane(IPlist);
JPanel listPanel = new JPanel();
listPanel.setBackground(Color.white);

JLabel valueLabel = new JLabel("Selected IP Address :");
valueLabel.setFont(displayFont);
valueField = new JTextField("None", 15);
valueField.setFont(displayFont);
JPanel valuePanel = new JPanel();
valuePanel.setBackground(Color.white);
Border valuePanelBorder = BorderFactory.createTitledBorder("Selection Of IP Address");
valuePanel.setBorder(valuePanelBorder);
valuePanel.add(valueLabel);
valuePanel.add(valueField);
content.add(valuePanel, BorderLayout.CENTER);

Border listPanelBorder = BorderFactory.createTitledBorder("List Of IP Addresses");
listPanel.setBorder(listPanelBorder);
JLabel label = new JLabel("Select IP Address :");
label.setFont(displayFont);
listPanel.add(label);

listPanel.add(listPane);
content.add(listPanel, BorderLayout.SOUTH);

bok=new JButton("OK");
bcancel=new JButton("CANCEL");

listPanel.add(bok);
listPanel.add(bcancel);

MyEventHandlerEdit handler=new MyEventHandlerEdit(this);
bok.addActionListener(handler);
bcancel.addActionListener(handler);

pack();
setVisible(true);

}

catch(FileNotFoundException fnfe)
{
System.out.println("File not found.... try another");
}

catch(IOException ioe)
{
System.out.println("I/0 Exception while reading file");
}

}

public void doitnowEdit()
{
if(arg.equals("OK"))
{

}
if(arg.equals("CANCEL"))
{
setVisible(false);
}
}

private class ValueReporter implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent lse)
{
if(!lse.getValueIsAdjusting())
valueField.setText(IPlist.getSelectedValue().toString());
}
}
}

class MyEventHandlerNse implements ActionListener
{
NewServerEntry nse;

public MyEventHandlerNse(NewServerEntry ob)
{
nse=ob;
}
public void actionPerformed(ActionEvent ae)
{
String arg = (String)ae.getActionCommand();
nse.arg=arg;
nse.doitnowNse();
}
}

class MyEventHandlerEdit implements ActionListener
{

EditServer es;

public MyEventHandlerEdit(EditServer ob)
{
es=ob;
}
public void actionPerformed(ActionEvent ae)
{
String arg = (String)ae.getActionCommand();
es.arg=arg;
es.doitnowEdit();
}
}

Thanking You
Yatin
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic