I'm trying to create a JAR of the given class.
I am using a tool called JARBuilder as Eclipse was of no help in creating JAR files.
When I try to create the JAR, it says that main method could be found, despite the fact that the main method is clearly defined.
Can someone please advise?
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.UnknownHostException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class Login extends JFrame {
private JPanel contentPane;
private JTextField textField_1;
private JTextField textField_2;
private String name;
private String address;
private int port;
private JTextField textField;
public Login() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 400, 348);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblUsername = new JLabel("Username");
lblUsername.setBounds(158, 44, 72, 16);
contentPane.add(lblUsername);
textField_1 = new JTextField();
textField_1.setEditable(false);
textField_1.setBounds(135, 111, 116, 22);
contentPane.add(textField_1);
textField_1.setColumns(10);
JLabel lblServerIp = new JLabel("Server IP");
lblServerIp.setBounds(158, 96, 56, 16);
contentPane.add(lblServerIp);
textField_2 = new JTextField();
textField_2.setEditable(false);
textField_1.setText("122.15.200.115");
textField_2.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent key) {
}
});
textField_2.setBounds(135, 162, 116, 22);
textField_2.setText("8000");
contentPane.add(textField_2);
textField_2.setColumns(10);
JLabel lblPort = new JLabel("Port");
lblPort.setBounds(174, 146, 56, 16);
contentPane.add(lblPort);
JButton btnLogin = new JButton("Login");
btnLogin.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent arg0) {
newWindow();
}
});
btnLogin.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
newWindow();
}
});
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnLogin.setBounds(145, 197, 97, 43);
contentPane.add(btnLogin);
textField = new JTextField();
textField.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent key) {
if(key.getKeyCode()==KeyEvent.VK_ENTER)
{
if(!(textField.getText().equals("")))
{
name=textField.getText();
address=textField_1.getText();
port=Integer.parseInt(textField_2.getText());
try {
Chat_window window=new Chat_window(name, address, port);
dispose();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
});
textField.setColumns(10);
textField.setBounds(135, 60, 116, 22);
contentPane.add(textField);
JLabel lblAuthorprashantPandey = new JLabel("Author:Prashant Pandey");
lblAuthorprashantPandey.setBounds(125, 253, 186, 16);
contentPane.add(lblAuthorprashantPandey);
}
public void newWindow(){
name=textField.getText();
address=textField_1.getText();
port=Integer.parseInt(textField_2.getText());
try {
Chat_window window=new Chat_window(name, address, port);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dispose();
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login frame = new Login();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Related
I tried setting JLabel to being visible only after I select a certain JRadioButton. The program is getting an error if I set lblNewLabel_1.setVisible(true) in the action performed of a radio button. It worked with the text field, but with this not. What can I do? Is it different with the label? Is there any advice someone can offer me?
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Window;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.ButtonGroup;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Fereastra extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private final ButtonGroup buttonGroup = new ButtonGroup();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Fereastra frame = new Fereastra();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Fereastra() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 751, 565);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
JTextArea textArea = new JTextArea();
textArea.setBounds(12, 13, 447, 251);
contentPane.add(textArea);
JRadioButton rdbtnNewRadioButton = new JRadioButton("Cautarea pe nivel");
rdbtnNewRadioButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(rdbtnNewRadioButton.isSelected())
{
textField.setVisible(false);
textField_1.setVisible(false);
}
}
});
buttonGroup.add(rdbtnNewRadioButton);
rdbtnNewRadioButton.setBounds(488, 55, 185, 25);
contentPane.add(rdbtnNewRadioButton);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("Cautarea in adancime");
rdbtnNewRadioButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(rdbtnNewRadioButton_1.isSelected())
{
textField.setVisible(true);
textField_1.setVisible(true);
}
else
{
textField.setVisible(false);
textField_1.setVisible(false);
}
}
});
buttonGroup.add(rdbtnNewRadioButton_1);
rdbtnNewRadioButton_1.setBounds(488, 96, 237, 25);
contentPane.add(rdbtnNewRadioButton_1);
JRadioButton rdbtnNewRadioButton_2 = new JRadioButton("Costul uniform");
rdbtnNewRadioButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(rdbtnNewRadioButton_2.isSelected())
{
textField.setVisible(false);
textField_1.setVisible(false);
}
}
});
buttonGroup.add(rdbtnNewRadioButton_2);
rdbtnNewRadioButton_2.setBounds(488, 138, 127, 25);
contentPane.add(rdbtnNewRadioButton_2);
JRadioButton rdbtnNewRadioButton_3 = new JRadioButton("Adancime iterativa");
rdbtnNewRadioButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(rdbtnNewRadioButton_3.isSelected())
{
textField_1.setVisible(true);
textField.setVisible(true);
}
else
{
textField_1.setVisible(false);
textField.setVisible(false);
}
}
});
buttonGroup.add(rdbtnNewRadioButton_3);
rdbtnNewRadioButton_3.setBounds(488, 179, 237, 25);
contentPane.add(rdbtnNewRadioButton_3);
JRadioButton rdbtnNewRadioButton_4 = new JRadioButton("Greedy");
rdbtnNewRadioButton_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(rdbtnNewRadioButton_4.isSelected())
{
textField.setVisible(false);
textField_1.setVisible(false);
}
}
});
buttonGroup.add(rdbtnNewRadioButton_4);
rdbtnNewRadioButton_4.setBounds(488, 221, 127, 25);
contentPane.add(rdbtnNewRadioButton_4);
JLabel lblNewLabel = new JLabel("Alegeti:");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel.setBounds(488, 13, 84, 33);
contentPane.add(lblNewLabel);
textField = new JTextField();
textField.setBounds(402, 277, 116, 22);
contentPane.add(textField);
textField.setColumns(10);
textField.setVisible(false);
textField_1 = new JTextField();
textField_1.setBounds(246, 277, 116, 22);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_1.setVisible(false);
JLabel lblNewLabel_1 = new JLabel("Introduceti valorile dorite:");
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_1.setBounds(22, 277, 212, 21);
contentPane.add(lblNewLabel_1);
lblNewLabel_1.setVisible(false);
}
}
It's because your label is declared in function context, put the declaration next the textbox declaration and it should work fine
I'm trying to make a chat application with Java; I chose to use JTextPane to display messages, because I read that it supports alignment. My problem is that I want to know how to align text in it. Like when I'm the sender, the sent message will be aligned in right; and when I'm the receiver it will be aligned in left.
Here is the code I wrote, but it aligns the whole text in right or left:
package memory;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import java.awt.Toolkit;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextPane;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;
import javax.swing.ScrollPaneConstants;
import java.awt.Font;
#SuppressWarnings("serial")
public class ChatFrame extends JFrame {
private JPanel contentPane;
private JTextPane textPane= new JTextPane();;
private String msg=null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ChatFrame frame = new ChatFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//setRemote Text
//false->me true->him
public void setTextToTextPane(String txt,boolean received) throws BadLocationException{
StyledDocument doc=textPane.getStyledDocument();
SimpleAttributeSet left = new SimpleAttributeSet();
StyleConstants.setAlignment(left, StyleConstants.ALIGN_LEFT);
SimpleAttributeSet right = new SimpleAttributeSet();
StyleConstants.setAlignment(right, StyleConstants.ALIGN_RIGHT);
if(received==false){
doc.insertString(doc.getLength(), txt="\n", right);
doc.setParagraphAttributes(doc.getLength(), 1, right, false);
}else{
doc.insertString(doc.getLength(), txt="\n", left);
doc.setParagraphAttributes(doc.getLength(), 1, left, false);
}
}
/**
* Create the frame.
*/
public ChatFrame() {
setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\PC-HOME\\Desktop\\design\\Speech Bubble-41.png"));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 299, 380);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
JScrollPane scrollPane_1 = new JScrollPane();
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 272, GroupLayout.PREFERRED_SIZE)
.addComponent(scrollPane_1, GroupLayout.PREFERRED_SIZE, 272, GroupLayout.PREFERRED_SIZE))
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
.addComponent(scrollPane_1, GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 65, GroupLayout.PREFERRED_SIZE))
);
textPane.setFont(new Font("Consolas", Font.PLAIN, 14));
textPane.setEditable(false);
scrollPane_1.setViewportView(textPane);
JTextArea textArea = new JTextArea();
textArea.setFont(new Font("Consolas", Font.PLAIN, 14));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.addKeyListener(new KeyAdapter() {
#SuppressWarnings("unchecked")
#Override
public void keyPressed(KeyEvent event) {
if(event.getKeyCode()==KeyEvent.VK_ENTER){
if(event.isShiftDown())
textArea.setText(textArea.getText()+"\n");
else{
msg=textArea.getText();
event.consume();
textArea.setText(null);
try {
//HelperMethods.sendMessageSocket(msg,port);
setTextToTextPane(msg,false);
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ArrayList<String> sendMsg=new ArrayList<>();
sendMsg.add(getTitle());
sendMsg.add(msg);
StaticData.sendMsg.add(sendMsg);
}
}
}
});
addWindowListener(new WindowListener() {
#Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
#Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
#Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
#Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
#Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
}
#Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
String name=getTitle();
StaticData.frameMap.remove(name);
}
#Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
});
scrollPane.setViewportView(textArea);
panel.setLayout(gl_panel);
}
}
Does anyone know how to do that??
In "setTextToTextPane" in your code sample you're inserting "txt="\n"". This should be ""txt + "\n". Without this change it won't printing anything.
What you're doing in "setTextToPane" is correct in terms of setting the attributes. If you do a unit test it correctly positions the text left or right depending on the value of "received".
I have three different frames:
welcome.java
Register.java
LoginForm.java
If I click on Login button login page must open(i.e LoginForm.java) and if I click on Register button register page must open
//welcome.java
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
public class welcome extends JFrame
{
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
welcome window = new welcome();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public welcome() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnLogIn = new JButton("LOG IN");
btnLogIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnLogIn.setBounds(200, 69, 117, 25);
frame.getContentPane().add(btnLogIn);
JButton btnRegister = new JButton("Register");
btnRegister.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnRegister.setBounds(200, 138, 117, 25);
frame.getContentPane().add(btnRegister);
JLabel lblNewToSvk = new JLabel("New to SVK Polytechnic ?");
lblNewToSvk.setBounds(12, 143, 191, 15);
frame.getContentPane().add(lblNewToSvk);
}
}
.
`//LoginForm.java
import java.awt.EventQueue;
import javax.swing.JScrollPane;
public class LoginForm extends JPanel implements ItemListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
private JFrame frame;
private JTextField textField;
private JPasswordField pwdJjh;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginForm window = new LoginForm();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public LoginForm() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblStudentLogin = new JLabel("Login ");
lblStudentLogin.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 30));
lblStudentLogin.setHorizontalAlignment(SwingConstants.CENTER);
lblStudentLogin.setBounds(94, 12, 265, 36);
frame.getContentPane().add(lblStudentLogin);
JLabel lblUserName = new JLabel("USER NAME");
lblUserName.setFont(new Font("Dialog", Font.BOLD, 15));
lblUserName.setBounds(12, 74, 111, 15);
frame.getContentPane().add(lblUserName);
textField = new JTextField();
textField.setFont(new Font("Dialog", Font.PLAIN, 15));
textField.setBounds(141, 70, 160, 22);
frame.getContentPane().add(textField);
textField.setColumns(10);
JLabel lblPassword = new JLabel("PASSWORD");
lblPassword.setFont(new Font("Dialog", Font.BOLD, 15));
lblPassword.setBounds(12, 116, 111, 15);
frame.getContentPane().add(lblPassword);
pwdJjh = new JPasswordField();
pwdJjh.setFont(new Font("Dialog", Font.PLAIN, 15));
pwdJjh.setBounds(141, 114, 160, 22);
frame.getContentPane().add(pwdJjh);
JLabel lblUserType = new JLabel("USER TYPE");
lblUserType.setFont(new Font("Dialog", Font.BOLD, 15));
lblUserType.setBounds(12, 154, 111, 15);
frame.getContentPane().add(lblUserType);
JComboBox<String> cm = new JComboBox<String>();
cm.setBounds(141, 148, 160, 24);
cm.addItem("User");
cm.addItem("Admin");
cm.addItemListener(this);
setLayout(null);
frame.getContentPane().add(cm);
JButton btnLogin = new JButton("Login ");
btnLogin.setBounds(41, 199, 117, 25);
frame.getContentPane().add(btnLogin);
JButton btnCancle = new JButton("Cancel");
btnCancle.setBounds(209, 199, 117, 25);
frame.getContentPane().add(btnCancle);
}
#Override
public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub
}
}
.
//Register.java
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
public class Register {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Register window = new Register();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Register() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel lblRegisterForm = new JLabel("Register Form");
lblRegisterForm.setForeground(Color.BLUE);
lblRegisterForm.setFont(new Font("Dialog", Font.BOLD | Font.ITALIC, 25));
lblRegisterForm.setBounds(119, 12, 209, 25);
frame.getContentPane().add(lblRegisterForm);
JLabel lblSelectTheUser = new JLabel("Select the user type");
lblSelectTheUser.setFont(new Font("Dialog", Font.BOLD, 15));
lblSelectTheUser.setBounds(22, 83, 192, 25);
frame.getContentPane().add(lblSelectTheUser);
JComboBox cb1 = new JComboBox();
cb1.setModel(new DefaultComboBoxModel(new String[] {"Student", "Lecturer", "Office staff", "HOD"}));
cb1.setBounds(213, 83, 151, 24);
frame.getContentPane().add(cb1);
}
public void setVisible(boolean b) {
// TODO Auto-generated method stub
}
public void setTitle(String string) {
// TODO Auto-generated method stub
}
public void setSize(int i, int j) {
// TODO Auto-generated method stub
}
public void setLocationRelativeTo(Object object) {
// TODO Auto-generated method stub
}
public void setDefaultCloseOperation(int exitOnClose) {
// TODO Auto-generated method stub
}
}
You can use
frame.setVisible(true);
to make a JFrame (form) visible.
Line 132 JPanel bookInventory = new Inventory(); of the attached code is reporting the error "Type mismatch: cannot convert from Inventory to JPanel" - any suggestions what's causing this please?
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.SystemColor;
import java.awt.Toolkit;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import java.awt.GridLayout;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.BevelBorder;
public class LibSys extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
//private JPanel panelContent;
private static LibSys frame;
private JPanel panelTools;
private JScrollPane scrollPane;
public static int level = 0;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new LibSys(0);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LibSys(int n) {
level = n;
setTitle("Library Management System");
setIconImage(Toolkit.getDefaultToolkit().getImage(LibSys.class.getResource("/resources/Books-2-icon64.png")));
setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 744);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menu = new JMenu("File");
JMenuItem exit = new JMenuItem("Exit");
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
dispose();
}
});
JSeparator sep = new JSeparator();
JMenuItem logout = new JMenuItem("Logout");
logout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Login login = new Login();
login.setVisible(true);
dispose();
}
});
menu.add(exit);
menu.add(sep);
menu.add(logout);
menuBar.add(menu);
contentPane = new JPanel();
contentPane.setBackground(SystemColor.text);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
panelTools = new JPanel();
contentPane.add(panelTools, BorderLayout.WEST);
JButton btnToolBookLending = new JButton("Book Lending");
btnToolBookLending.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
JPanel bookLend = new BookLending();
//scrollPane.repaint();
scrollPane.getViewport().removeAll();
scrollPane.setViewportView(bookLend);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
panelTools.setLayout(new GridLayout(8, 1, 0, 20));
btnToolBookLending.setHorizontalAlignment(SwingConstants.LEFT);
btnToolBookLending.setIcon(new ImageIcon(LibSys.class.getResource("/resources/book-48.png")));
panelTools.add(btnToolBookLending);
//panelContent = new JPanel();
//contentPane.add(panelContent, BorderLayout.CENTER);
//panelContent.setLayout(new FlowLayout(BorderLayout.CENTER, 5, 5));
JButton btnToolInventory = new JButton("Inventory");
btnToolInventory.setHorizontalAlignment(SwingConstants.LEFT);
btnToolInventory.setIcon(new ImageIcon(LibSys.class.getResource("/resources/Inventory-icon-32.png")));
btnToolInventory.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
JPanel bookInventory = new Inventory();
scrollPane.getViewport().removeAll();
//System.out.println(bookInventory);
scrollPane.setViewportView(bookInventory);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
panelTools.add(btnToolInventory);
//panelTools.add(btnNewButton_1, "2, 2, left, center");
JButton btnBooksReg = new JButton("Book Registration");
btnBooksReg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
JPanel bookReg = new BookRegistration();
scrollPane.getViewport().removeAll();
scrollPane.setViewportView(bookReg);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnBooksReg.setHorizontalAlignment(SwingConstants.LEFT);
btnBooksReg.setIcon(new ImageIcon(LibSys.class.getResource("/resources/book-add-icon-32.png")));
panelTools.add(btnBooksReg);
JButton btnToolMemberProfile = new JButton("Member Profile");
btnToolMemberProfile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
JPanel bookMemberProfile = new MemberProfile();
scrollPane.getViewport().removeAll();
scrollPane.setViewportView(bookMemberProfile);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnToolMemberProfile.setIcon(new ImageIcon(LibSys.class.getResource("/resources/App-login-manager-icon32.png")));
btnToolMemberProfile.setHorizontalAlignment(SwingConstants.LEFT);
panelTools.add(btnToolMemberProfile);
JButton btnToolMemberReg = new JButton("Member Registration");
btnToolMemberReg.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
JPanel MemberReg = new Registration();
scrollPane.getViewport().removeAll();
scrollPane.setViewportView(MemberReg);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnToolMemberReg.setIcon(new ImageIcon(LibSys.class.getResource("/resources/user_male_2_add.png")));
btnToolMemberReg.setHorizontalAlignment(SwingConstants.LEFT);
panelTools.add(btnToolMemberReg);
JButton btnToolConfig = new JButton("Configuration");
btnToolConfig.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
JPanel adminPanel = new AdminPanel();
scrollPane.getViewport().removeAll();
scrollPane.setViewportView(adminPanel);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btnToolConfig.setIcon(new ImageIcon(LibSys.class.getResource("/resources/settings.png")));
btnToolConfig.setHorizontalAlignment(SwingConstants.LEFT);
panelTools.add(btnToolConfig);
JButton btnLogout = new JButton("Logout");
btnLogout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Login login = new Login();
login.setVisible(true);
dispose();
}
});
btnLogout.setIcon(new ImageIcon(LibSys.class.getResource("/resources/12345678.png")));
btnLogout.setHorizontalAlignment(SwingConstants.LEFT);
panelTools.add(btnLogout);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setBackground(SystemColor.window);
lblNewLabel.setIcon(new ImageIcon(LibSys.class.getResource("/resources/top-banner.png")));
lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
contentPane.add(lblNewLabel, BorderLayout.NORTH);
scrollPane = new JScrollPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
JPanel panel = new JPanel();
scrollPane.setColumnHeaderView(panel);
panel.setLayout(new BorderLayout(0, 0));
JButton btnClose = new JButton("");
btnClose.setPreferredSize(new Dimension(33, 33));
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(scrollPane.getViewport().getComponentCount()>0){
scrollPane.getViewport().remove(scrollPane.getViewport().getComponent(0));
scrollPane.getViewport().repaint();
}
}
});
btnClose.setIcon(new ImageIcon(LibSys.class.getResource("/resources/2015.png")));
panel.add(btnClose, BorderLayout.EAST);
}
}
make sure that Inventory extends JPanel
this makes Inventory to behave as a subclass of a JPanel, and thus you can use it in the same way.
I'm using invokelater to update a value on button click, but the value is only changing when I run another instance of the GUI. I'm running the invokelater method outside the main method so I'm not sure why the value won't update. Any help is appreciated.
private void threadStart() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
testLabel.setText(CN);
}
});
}
I'm using the created method here:
searchComputerButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
threadStart();
String line;
BufferedWriter bw = null;
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(tempFile));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
etc...
You can view the full class here:
import java.awt.EventQueue;
import javax.naming.InvalidNameException;
import javax.naming.ldap.LdapName;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.GridLayout;
import javax.naming.ldap.LdapName;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Color;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JTextArea;
import javax.swing.JTable;
public class MISControlPanel {
JFrame frame;
static JTextField textField;
private JTextField textField_1;
final JTextArea textArea = new JTextArea();
JLabel selectedComputerFromAD = new JLabel("testing");
String sCurrentLine = null;
String CN = null;
JLabel testLabel = new JLabel("test");
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MISControlPanel window = new MISControlPanel();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*
* #throws IOException
*
* #wbp.parser.entryPoint
*/
public MISControlPanel() throws IOException {
initialize();
}
/**
* Initialize the contents of the frame.
*
* #throws IOException
*/
private void initialize() throws IOException {
frame = new JFrame();
frame.getContentPane().setBackground(Color.LIGHT_GRAY);
frame.getContentPane().setForeground(Color.RED);
frame.setBounds(100, 100, 658, 618);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setTitle("MIS Advanced Computerers");
frame.setResizable(false);
FileWriter fw = new FileWriter("C:\\Users\\anoc5f\\Desktop\\Output.txt");
File tempFile = new File("myTempFile.txt");
JButton searchComputerButton = new JButton("Search");
searchComputerButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
threadStart();
String line;
BufferedWriter bw = null;
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(tempFile));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String s = null;
Process p = null;
/*
* try { // p = Runtime.getRuntime().exec(
* "cmd /c start c:\\computerQuery.bat computerName"); } catch
* (IOException e1) { // TODO Auto-generated catch block
* e1.printStackTrace(); }
*/
try {
p = Runtime.getRuntime().exec("c:\\computerQuery.bat");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
StringBuffer sbuffer = new StringBuffer();
BufferedReader in = new BufferedReader(new InputStreamReader(p
.getInputStream()));
try {
while ((line = in.readLine()) != null) {
System.out.println(line);
// textArea.append(line);
String dn = "CN=FDCD111304,OU=Workstations,OU=SIM,OU=Accounts,DC=FL,DC=NET";
LdapName ldapName = new LdapName(dn);
String commonName = (String) ldapName.getRdn(
ldapName.size() - 1).getValue();
}
ComputerQuery.sendParam();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InvalidNameException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally
{
try {
fw.close();
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try {
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ComputerQuery.sendParam();
}
});
try (BufferedReader br = new BufferedReader(new FileReader(
"resultofbatch.txt"))) {
final Pattern PATTERN = Pattern.compile("CN=([^,]+).*");
try {
while ((sCurrentLine = br.readLine()) != null) {
String[] tokens = PATTERN.split(","); // This will return
// you a array,
// containing the
// string array
// splitted by what
// you write inside
// it.
// should be in your case the split, since they are
// seperated by ","
// System.out.println(sCurrentLine);
CN = sCurrentLine.split("CN=", -1)[1].split(",", -1)[0];
System.out.println(CN);
testLabel.setText(CN);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
// SwingUtilities.invokeLater(updateCN());
searchComputerButton.setBounds(419, 19, 89, 20);
frame.getContentPane().add(searchComputerButton);
textField = new JTextField();
textField.setBounds(285, 19, 124, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
JLabel lblComputerName = new JLabel("Computer Name:");
lblComputerName.setForeground(Color.BLACK);
lblComputerName.setFont(new Font("Tahoma", Font.BOLD, 14));
lblComputerName.setBounds(159, 13, 124, 29);
frame.getContentPane().add(lblComputerName);
JLabel lblSelectedComputer = new JLabel("Selected Computer:");
lblSelectedComputer.setFont(new Font("Tahoma", Font.BOLD, 14));
lblSelectedComputer.setBounds(205, 78, 143, 30);
frame.getContentPane().add(lblSelectedComputer);
java.net.InetAddress localMachine = null;
try {
localMachine = java.net.InetAddress.getLocalHost();
} catch (UnknownHostException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
textArea.setBackground(Color.GRAY);
textArea.setForeground(Color.WHITE);
textArea.setFont(textArea.getFont().deriveFont(Font.BOLD));
textArea.setBounds(10, 387, 632, 191);
textArea.setEditable(false); // might cause design view not to come up
frame.getContentPane().add(textArea);
JButton btnNewButton = new JButton("SSO REPAIR");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnNewButton.setBounds(36, 183, 132, 23);
frame.getContentPane().add(btnNewButton);
JLabel lblNewLabel = new JLabel("Batch File Fixes");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 12));
lblNewLabel.setBounds(53, 158, 115, 14);
frame.getContentPane().add(lblNewLabel);
JLabel lblIpAddress = new JLabel("IP Address:");
lblIpAddress.setFont(new Font("Tahoma", Font.BOLD, 14));
lblIpAddress.setBounds(261, 104, 102, 22);
frame.getContentPane().add(lblIpAddress);
JLabel label = null;
try {
label = new JLabel(Inet4Address.getLocalHost().getHostAddress()); // Get
// User
// ID
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
label.setFont(new Font("Tahoma", Font.BOLD, 14));
label.setForeground(Color.RED);
label.setBounds(349, 105, 99, 21);
frame.getContentPane().add(label);
JLabel lblPcCommunication = new JLabel("PC Communication");
lblPcCommunication.setFont(new Font("Tahoma", Font.BOLD, 12));
lblPcCommunication.setBounds(279, 158, 115, 14);
frame.getContentPane().add(lblPcCommunication);
JButton btnPingComputer = new JButton("PING");
btnPingComputer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnPingComputer.setBounds(306, 183, 64, 23);
frame.getContentPane().add(btnPingComputer);
JButton remoteAssistanceButton = new JButton(
"Remote Assistance by PC Name");
remoteAssistanceButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
remoteAssistanceButton.setBounds(215, 251, 246, 23);
frame.getContentPane().add(remoteAssistanceButton);
JButton btnPingIndefinetely = new JButton("PING INDEFINETELY");
btnPingIndefinetely.setBounds(260, 217, 156, 23);
frame.getContentPane().add(btnPingIndefinetely);
JButton btnRdcByIp = new JButton("RDC by IP Address");
btnRdcByIp.setBounds(261, 353, 156, 23);
frame.getContentPane().add(btnRdcByIp);
JButton btnRdcByName = new JButton("RDC by PC Name");
btnRdcByName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnRdcByName.setBounds(248, 319, 180, 23);
frame.getContentPane().add(btnRdcByName);
JLabel lblLoggedInOpid = new JLabel("Logged in OPID:");
lblLoggedInOpid.setFont(new Font("Tahoma", Font.BOLD, 14));
lblLoggedInOpid.setBounds(228, 127, 135, 20);
frame.getContentPane().add(lblLoggedInOpid);
JLabel lblNewLabel_1 = new JLabel(System.getProperty("user.name")
.toUpperCase());
lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 14));
lblNewLabel_1.setForeground(Color.RED);
lblNewLabel_1.setBounds(348, 130, 86, 14);
frame.getContentPane().add(lblNewLabel_1);
JButton btnHiddenShare = new JButton("HIDDEN SHARE");
btnHiddenShare.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnHiddenShare.setBounds(484, 183, 132, 23);
frame.getContentPane().add(btnHiddenShare);
JLabel lblAdditionalTools = new JLabel("Tools");
lblAdditionalTools.setFont(new Font("Tahoma", Font.BOLD, 12));
lblAdditionalTools.setBounds(526, 157, 126, 20);
frame.getContentPane().add(lblAdditionalTools);
JButton btnNewButton_1 = new JButton("Remote Assistance by IP Address");
btnNewButton_1.setBounds(215, 285, 246, 23);
frame.getContentPane().add(btnNewButton_1);
JButton gpUpdate = new JButton("GP UPDATE");
gpUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
gpUpdate.setBounds(36, 217, 132, 23);
frame.getContentPane().add(gpUpdate);
JButton btnForced = new JButton("NURMED REG FIX");
btnForced.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnForced.setBounds(36, 251, 132, 23);
frame.getContentPane().add(btnForced);
JButton btnCleaConsoler = new JButton("CLEAR");
btnCleaConsoler.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.setText("");
}
});
btnCleaConsoler.setBounds(563, 353, 79, 23);
frame.getContentPane().add(btnCleaConsoler);
JLabel lblOpid = new JLabel("Find Computer by OPID:");
lblOpid.setFont(new Font("Tahoma", Font.BOLD, 14));
lblOpid.setBounds(104, 53, 180, 14);
frame.getContentPane().add(lblOpid);
textField_1 = new JTextField();
textField_1.setBounds(285, 50, 124, 20);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
JButton btnNewButton_2 = new JButton("Search");
btnNewButton_2.setBounds(419, 50, 89, 23);
frame.getContentPane().add(btnNewButton_2);
testLabel.setForeground(Color.RED);
testLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
testLabel.setBounds(358, 78, 150, 24);
frame.getContentPane().add(testLabel);
}
private void threadStart() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
testLabel.setText(CN);
}
});
}
}