I am trying to pass the table number from the restaurant class into tableLabel which is in the Menu page class. When the code is running the tableLabel returns null. Any help would be appreciated to enable the code when running to return a number in the tableLabel.
Extract from Restaurant Class
public class Restaurant extends JFrame {
private JPanel contentPane;
private JTextField restaurant_Txt;
private JTextField num_Diners;
private JTextField num_Diners_Txt;
private JTextField table_Num_Txt;
private JTextField num_Table;
private JButton num_TableSub_Btn;
private JButton proceed_Menu_Btn;
private JButton MyDocumentListener;
MenuPage parent;
public static String tableNumber;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Restaurant frame = new Restaurant();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Restaurant() {
super("Restaurant");
parent = new MenuPage();
initGUI();
num_Table = new JTextField("NewUser", 10);
}
public void initGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 640, 310);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
table_Num_Txt = new JTextField();
table_Num_Txt.setEditable(false);
table_Num_Txt.setText("Table number ?");
table_Num_Txt.setBounds(145, 164, 112, 26);
contentPane.add(table_Num_Txt);
table_Num_Txt.setColumns(10);
num_Table = new JTextField("");
num_Table.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
if (e.getKeyCode() == 10) {
tableNumber = num_Table.getText();
}
}
});
num_Table.setBounds(334, 161, 83, 26);
contentPane.add(num_Table);
num_Table.setColumns(10);
}
}
Extract from Menu Page class
tableLabel = new JLabel(" : " + Restaurant.tableNumber);
tableLabel.setBounds(16, 6, 61, 16);
contentPane.add(tableLabel);
This may be a stupid question, but are you sure you are pressing enter when being inside the num_Table field inside your GUI since the variable is set when this happens. Also you should probably pass it via the constructor to MainPage, not as a static variable.
I am creating a snakes and ladders game. My issue is that I have two classes, one the main GUI in a JFrame for the game with an image of a snakes and ladders board, and the other a 2D array of a grid which I want to superimpose over the board game, so the squares in the image match the squares of the grid.
I figure I need to call it as an instance of the Grid class, but I cant seem to get it to work (or perhaps, placed in the correct position!). Can anybody help me?
Thanks in advance
GameBoard class:
public class GameBoard extends javax.swing.JFrame {
private JLabel Board;
private JLabel playerNumber;
private ButtonGroup group;
private JButton startButton;
private JRadioButton fourPlayer;
private JRadioButton threePlayer;
private JRadioButton twoPlayer;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
GameBoard inst = new GameBoard();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public GameBoard() {
super();
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
Board = new JLabel();
getContentPane().add(Board);
Board.setText("jLabel1");
Board.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/board.jpg")));
Board.setBounds(199, 0, 742, 484);
}
{
playerNumber = new JLabel();
getContentPane().add(playerNumber);
playerNumber.setText("Number of Players");
playerNumber.setBounds(40, 22, 117, 27);
}
{
twoPlayer = new JRadioButton();
getContentPane().add(twoPlayer);
twoPlayer.setText("Two Player");
twoPlayer.setBounds(40, 55, 93, 20);
}
{
threePlayer = new JRadioButton();
getContentPane().add(threePlayer);
threePlayer.setText("Three Players");
threePlayer.setBounds(40, 76, 88, 20);
}
{
fourPlayer = new JRadioButton();
getContentPane().add(fourPlayer);
fourPlayer.setText("Four Players");
fourPlayer.setBounds(40, 99, 82, 20);
}
{
startButton = new JButton();
getContentPane().add(startButton);
startButton.setText("Start Game");
startButton.setBounds(43, 136, 83, 23);
}
{
//Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(twoPlayer);
group.add(threePlayer);
group.add(fourPlayer);
}
pack();
this.setSize(963, 523);
} catch (Exception e) {
//add your error handling code here
e.printStackTrace();
}
}
}
;
Grid class:
public class Grid {
int[][] multi = {
{0,0,-1,0,0,-1,0,-1,0,0},
{0,0,0,0,0,0,-1,0,0,0},
{0,0,0,0,0,0,0,0,0,1},
{0,-1,0,-1,0,0,0,0,0,0},
{0,0,0,0,0,0,-1,0,0,1},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{1,0,0,0,0,0,0,1,0,0},
{0,0,0,-1,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0,1,0}
};
}
I'm guessing that GameBoard will need an instance Grid in order for it to know where to place the game pieces.
You could change GameBoard so that it required an instance of Grid to be passed to it...
public class GameBoard extends javax.swing.JFrame {
//...
private Grid grid;
public GameBoard(Grid grid) {
this.grid = grid;
//...
Then create and pass an instance of Grid when you create an instance of GameBoard...
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Grid grid = new Grid();
GameBoard inst = new GameBoard(grid);
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
But, I'd also add some functionality to Grid to control how it's modified, but that's me
I cant seem to get it work using your original method. I would like to just simply create an instance but for the life of me I can't seem to get it working
Seems to work okay for me...
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class GameBoard extends javax.swing.JFrame {
private JLabel Board;
private JLabel playerNumber;
private ButtonGroup group;
private JButton startButton;
private JRadioButton fourPlayer;
private JRadioButton threePlayer;
private JRadioButton twoPlayer;
private Grid grid;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Grid grid = new Grid();
GameBoard inst = new GameBoard(grid);
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public GameBoard(Grid grid) {
super();
this.grid = grid;
initGUI();
}
private void initGUI() {
try {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
{
Board = new JLabel();
getContentPane().add(Board);
Board.setText("jLabel1");
Board.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/board.jpg")));
Board.setBounds(199, 0, 742, 484);
}
{
playerNumber = new JLabel();
getContentPane().add(playerNumber);
playerNumber.setText("Number of Players");
playerNumber.setBounds(40, 22, 117, 27);
}
{
twoPlayer = new JRadioButton();
getContentPane().add(twoPlayer);
twoPlayer.setText("Two Player");
twoPlayer.setBounds(40, 55, 93, 20);
}
{
threePlayer = new JRadioButton();
getContentPane().add(threePlayer);
threePlayer.setText("Three Players");
threePlayer.setBounds(40, 76, 88, 20);
}
{
fourPlayer = new JRadioButton();
getContentPane().add(fourPlayer);
fourPlayer.setText("Four Players");
fourPlayer.setBounds(40, 99, 82, 20);
}
{
startButton = new JButton();
getContentPane().add(startButton);
startButton.setText("Start Game");
startButton.setBounds(43, 136, 83, 23);
}
{
//Group the radio buttons.
ButtonGroup group = new ButtonGroup();
group.add(twoPlayer);
group.add(threePlayer);
group.add(fourPlayer);
}
pack();
this.setSize(963, 523);
} catch (Exception e) {
//add your error handling code here
e.printStackTrace();
}
}
}
I am trying to make a program that I enter first name and last name and so on and then being about to view that information again.
I am using Java and Jframes
i have setup my interface with JTextfileds and Jlabels and a button
i am trying to make an array of objects so each array number will contain (firstname, lastname , and so on) and then i am going to print it out into a Jlist. i want to make it so when i prees my button it will input everything in my Jtextinputs in the first array
but i don't know how to do the array with objects i have started one but i need help thanks
i know that i need to use
ArrayList<> mylist = new ArrayList<Data>();
Data myTask = new Data("imput", "input");
myList.add(myTask);
But I don't understand how I use it
Thanks
that is my data.java file
package components;
Import Java.Io.Serializable;
public class Data implements Serializable {
static final long serialVersionUID = 1;
String name;
String description;
public Task(String Fname, String Lname) {
this.name = Fname;
this.description = Lname;
}
public String getName() {
return this.name;
}
public String getDescription() {
Return this.description;
}
interface file
public class DataDemo extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
//Name Label and TextField
protected static JLabel LbName;
protected static JTextField txtInputName;
//Description Label and TextField
protected static JLabel LbDescription;
protected static JTextField txtInputDescription;
//Submit Button
static JButton btnSubmit;
#SuppressWarnings("unchecked")
public DataDemo() {
//Name Label and TextField
LbName = new JLabel("Name:");
txtInputName = new JTextField(200);
//Description Label and TextField
LbDescription = new JLabel("Description:");
txtInputDescription = new JTextField(20);
//Submit Button
btnSubmit = new JButton("Done");
btnSubmit.addActionListener(this);
//Add Components to this container, using the default FlowLayout.
setLayout(null);
//Name
add(LbName);
add(txtInputName);
//Description
add(LbDescription);
add(txtInputDescription);
//button Submit
add(btnSubmit);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == btnSubmit)
}
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("DataDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
DataDemo newContentPane = new DataDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setSize(500, 500);
frame.setVisible(true);
//(x,y,with,height)
//Name interface position
LbName.setBounds(50, 70, 200, 25);
txtInputName.setBounds(200, 70, 200, 25);
//Description interface position
LbDescription.setBounds(50, 100, 200, 25);
txtInputDescription.setBounds(200, 100, 200, 25);
//button Submit
btnSubmit.setBounds(200, 150, 200, 25);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Did you try using
ArrayList<Data> mylist = new ArrayList<Data>();
That should allow you to create a proper Iterator and get your Data-Objects. Otherwise you will have to cast all Objects you retrieve from your list to Data, i.e.
ArrayList<> mylist = new ArrayList<>();
//add your Data-Objects here
for (Object o : mylist)
{
Data d = (Data) o;
//do something with d
}
So I'm starting a new project and I was wondering how can I setup multiple classes for my different JPanels. It looks very messy in one class. Let me show you the example. I would like the JPanel for a menu screen in it's own class.
import java.awt.EventQueue;
public class TakeAwayLogin {
private JFrame frmTakeAwaySystem;
private JTextField textFieldId;
private JPasswordField passwordField;
/**
* Launch the application.
* #return
*/
public void login() {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TakeAwayLogin window = new TakeAwayLogin();
window.frmTakeAwaySystem.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public TakeAwayLogin() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmTakeAwaySystem = new JFrame();
frmTakeAwaySystem.setTitle("Take Away System Alpha");
frmTakeAwaySystem.setBounds(100, 100, 450, 300);
frmTakeAwaySystem.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmTakeAwaySystem.getContentPane().setLayout(new CardLayout(0, 0));
final JPanel panelLogin = new JPanel();
frmTakeAwaySystem.getContentPane().add(panelLogin, "name_254735117500687");
panelLogin.setLayout(null);
panelLogin.setVisible(true);
final JLabel lblIncorrect = new JLabel("Incorrect login, try again");
lblIncorrect.setHorizontalAlignment(SwingConstants.CENTER);
lblIncorrect.setFont(new Font("Tahoma", Font.PLAIN, 9));
lblIncorrect.setForeground(Color.RED);
lblIncorrect.setBounds(148, 74, 139, 14);
panelLogin.add(lblIncorrect);
lblIncorrect.setVisible(false);
final JPanel panelPassword = new JPanel();
frmTakeAwaySystem.getContentPane().add(panelPassword, "name_254738265432897");
panelPassword.setLayout(null);
passwordField = new JPasswordField();
passwordField.setBounds(112, 157, 205, 41);
panelLogin.add(passwordField);
passwordField.setHorizontalAlignment(JPasswordField.CENTER);
JButton btnNewButton = new JButton("Lock Application");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panelPassword.setVisible(false);
panelLogin.setVisible(true);
passwordField.setText("");
textFieldId.disable();
}
});
btnNewButton.setBounds(135, 155, 172, 50);
panelPassword.add(btnNewButton);
panelPassword.setVisible(false);
JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String login = textFieldId.getText();
char[] pass = passwordField.getPassword();
String p = new String(pass);
String password = "pass";
if (login.equalsIgnoreCase("milan") && p.equals(password)) {
panelPassword.setVisible(true);
panelLogin.setVisible(false);
}else {
lblIncorrect.setVisible(true);
}
}
});
btnLogin.setBounds(160, 209, 97, 41);
panelLogin.add(btnLogin);
textFieldId = new JTextField();
textFieldId.setBounds(112, 88, 205, 43);
panelLogin.add(textFieldId);
textFieldId.setColumns(10);
textFieldId.setHorizontalAlignment(JTextField.CENTER);
JLabel lblId = new JLabel("Enter the business login:");
lblId.setHorizontalAlignment(SwingConstants.CENTER);
lblId.setBounds(112, 63, 205, 14);
panelLogin.add(lblId);
JLabel lblEnterYourPassword = new JLabel("Enter your password");
lblEnterYourPassword.setHorizontalAlignment(SwingConstants.CENTER);
lblEnterYourPassword.setBounds(148, 142, 139, 14);
panelLogin.add(lblEnterYourPassword);
JPanel panelPizza = new JPanel();
frmTakeAwaySystem.getContentPane().add(panelPizza, "name_254741096954780");
}
}
So help would be great.
Thanks very much.
I don't know why creating a second class would be necessary. If you just want the code to look less messy, then add comment separators between Panels or something to that effect. I think that splitting up the Panels into their own classes would be just cause you to have to write more calls to be able to create the same functionality. But if you must, then make a class within your initiate class and work from there.
Simplifying, you want something like:
public class MyMainJFrame extends JFrame{
private JPanel panel1 = new MyFooPanel();
private JPanel panel2 = new MyBarPanel();
public MyMainPanel(){
initialize();
}
void initialize(){
//init the JFrame
this.setTitle("Take Away System Alpha");
this.setBounds(100, 100, 450, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new CardLayout(0, 0));
//and add the panels
this.add(panel1);
this.add(panel2);
}
}
// a file for each one
public class MyFooPanel extends JPanel{
private JButton button;
//... add components and logic
}
public class MyBarPanel extends JPanel{
private JTextField field;
//... add components and logic
}
If you want to use specific methods for each custom panel, then change the type of variable for the same name of the custom class.
I have one frame in which one TestArea is there. When I append some string from this class then String is appended but when I want to append String from other class then String is not appended. I created one method to append string in TextArea, when I call this method in this class then string is appended on text Area. But when I call this method from other Class then String is not appended on TextArea.
Code (MainClass):
public class MainClass {
private JFrame frame;
private TextArea textArea;
private Font font;
private JButton button1;
private JButton button2;
private SecondClass secondClass;
public MainClass() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
frame = new JFrame("XXX");
frame.setBounds(200, 200, 600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
button1 = new JButton("Button1");
font = new Font("Arial", Font.BOLD, 13);
button1.setFont(font);
button1.setBounds(4, 4, 289, 30);
button2 = new JButton("Button2");
button2.setFont(font);
button2.setBounds(300, 4, 289, 30);
font = null;
textArea = new TextArea();
textArea.setBounds(4, 38, 585, 322);
textArea.setEnabled(true);
font = new Font("Arial", Font.PLAIN, 13);
textArea.setFont(font);
frame.add(button1);
frame.add(button2);
frame.add(textArea);
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
textArea.append("*** I am in actionPerformed() ***\n");
appendToTextArea("Call from actionPerformed() method\n");
}
});
button2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
secondClass = new SecondClass();
secondClass.printOnTextArea();
}
});
} catch (Exception e) {
textArea.append(e.toString());
}
}
public void appendToTextArea(String str) {
System.out.println(str+"\n");
textArea.append(str+"\n"); //this line not work when I call this method from other class
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MainClass window = new MainClass();
window.frame.setVisible(true);
}
});
}
}
Code(SecondClass):
import com.grissserver.MainClass;
public class SecondClass extends MainClass{
void printOnTextArea() {
System.out.println("*** printOnTextArea() ***");
super.appendToTextArea("call from Second Class in printOnTextArea()");
}
}
Please give some Idea, why this is not working.
I think the problem is that the way you try to paint to the text area is wrong.
In your action method you create a new object of SecondClass which extends MainClass. This means this object has its own textarea object. But this new object (frame) is not displayed, because you only call setVisibile in MainClass#main, and hence you cannot see the displayed text!
In short: There are two different text areas! And one of them is not visible
The SecondClass has its own textArea. So you may need to pass MainClass's textArea to SecondClass.
public class SecondClass {
private TextArea tArea;
SecondClass(TextArea ta) {
tArea = ta;
}
void printOnTextArea() {
System.out.println("*** printOnTextArea() ***");
tArea.append("call from Second Class in printOnTextArea()");
}
}
You should change your MainClass like this.
....
button2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
secondClass = new SecondClass(textArea);
secondClass.printOnTextArea();
}
});
....
hope this helps...