I'm trying to make a GUI window in Java with 4 tabs at the top Home, Booking, Guest, Room. However, the problem is that I'm not sure how to implement buttons in specific tab.
More specific, I made a class called GuestTab and I made 1 button and 1 textfield, but I don't know how can I convey those informations to a Guest tab.
So, if I wasn't so clear, when I click on Guest tab I want to have buttons and text fields that I made in GuestTab class.
I'll put the code of "SEP" class, where I have my GUI main design and "GuestTab" class where I'm adding stuff for Guest tab.
SEP.java:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class SEP extends JFrame
{
private GuestTab GuestTab;
private JTabbedPane tPane;
private MyButtonListener buttonListener;
private MyTabListener tabListener;
private JMenuBar menuBar;
private JMenu fileMenu;
private JMenuItem exitMenuItem;
public SEP()
{
super("Deer Alley Hotel");
buttonListener = new MyButtonListener();
tabListener = new MyTabListener();
exitMenuItem = new JMenuItem("Exit");
exitMenuItem.addActionListener(buttonListener);
fileMenu = new JMenu("File");
setJMenuBar(menuBar);
tPane = new JTabbedPane();
tPane.addTab(" Home ", new JPanel(
new FlowLayout()));
tPane.addTab(" Booking ", new JPanel(
new FlowLayout()));
tPane.addTab(" Guest ", GuestTab);
tPane.addTab(" Room ", new JPanel(new FlowLayout()));
tPane.addChangeListener(tabListener);
add(tPane);
setSize(575, 452);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
private class MyButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == exitMenuItem)
{
int choice = JOptionPane.showConfirmDialog(null,
"Do you really want to exit the program?", "Exit",
JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION)
{
System.exit(0);
}
}
}
}
public class MyTabListener implements ChangeListener
{
public void stateChanged(ChangeEvent e)
{
}
}
}
GuestTab.java:
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GuestTab extends JPanel
{
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JPanel panel1;
private JPanel panel2;
private JTextField text;
public GuestTab()
{
panel1 = new JPanel();
button1 = new JButton("Edit Note");
button2 = new JButton("Check out");
button3 = new JButton("Edit Form");
button4 = new JButton("Search");
text = new JTextField(15);
add(panel1);
panel1.setPreferredSize(new Dimension(280, 300));
panel1.add(button4);
panel1.add(text);
setVisible(true);
}
}
You aren't defining your GuestTab properly.
In this line:
tPane.addTab("Guest", guestTab);
Change it to:
tPane.addTab("Guest", new GuestTab());
Or you can initialize the JPanel. You never actually do this you just say
there is a guest tab but you never do anything with it so you can also do:
private GuestTab guestTab;
and then later:
guestTab = new GuestTab();
Side note, never use the same case for the variable definition and class call. Make sure you are using proper camel case.
Related
as you see I have made a very simple program where I input text on a textfield, then press the button to print it on the console:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class Main {
private static class Window extends JFrame {
private static class TextField extends JTextField {
private TextField() {
setVisible(true);
setBounds(5, 0, 190,20);
}
}
public class Button extends JButton implements ActionListener {
private Button() {
setText("print");
addActionListener(this);
setVisible(true);
setBounds(5, 35, 190,20);
}
public void actionPerformed(ActionEvent e) {
TextField textField = new TextField();
String input = textField.getText();
System.out.println(input);
}
}
However, while I click the button, it adds blank lines to the console, without the text I have written in it.
Your actionPerformed method creates a new TextField instance and prints the text from that new instance.
A very simple example that works as intended:
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TextAndButtonExample {
public static void main( String[] args ) {
EventQueue.invokeLater(() -> new TextAndButtonExample().start());
}
void start() {
JTextField textField = new JTextField();
JButton button = new JButton(new AbstractAction("Print") {
public void actionPerformed( ActionEvent e ) {
System.out.println(textField.getText());
}
});
JPanel controlPane = new JPanel(new GridLayout(2, 1));
controlPane.add(textField);
controlPane.add(button);
JPanel contentPane = new JPanel();
contentPane.add(controlPane);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(contentPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Note that I did not extend any Swing components and that I did use a layout manager instead of explicitly setting bounds for components.
I have created a JFrame with 3 JTextFields, a button and a label in the centre of the page. I will create a key where if the user enters 'a' in all three textfields and hits the button, the color of the text in the label should change to a different colour for example red.
The main problem I have is linkning my textfields and the button so they work together.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CE203_2017_Ex1 extends JFrame
{
public static void window()
{
//Button
JButton but = new JButton("Submit");
//JNumberTextField
JTextField rgb1 = new JTextField("",3);
JTextField rgb2 = new JTextField("",3);
JTextField rgb3 = new JTextField("",3);
//JLabel
JLabel text1 = new JLabel("hello my name is adam ", JLabel.CENTER);
text1.setForeground(Color.BLUE);
text1.setAlignmentX(0);
text1.setAlignmentY(0);
//JFrame
JFrame window1 = new JFrame("Adam");
window1.setVisible(true);
window1.setSize(500,500);
window1.add(text1);
//JPanel
JPanel butPanel = new JPanel();
butPanel.add(rgb1);
butPanel.add(rgb2);
butPanel.add(rgb3);
butPanel.add(but);
window1.add(butPanel, BorderLayout.SOUTH);
window1.add(text1);
Here is my actionlistener for the button
but.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JTextField input = (JTextField) e.getSource();
String passy = input.getText();
String p = new String (passy);
if (rgb1.equals("a")&& rgb2.equals("a")&& rgb3.equals("a")){
text1.setForeground(Color.WHITE);
}
else{
JOptionPane.showMessageDialog(null, "nocorrect");}}});
}
public static void main( String[] args )
{
window();
}
}
You want to test the text value in the textFields not the objects.
if (rgb1.getText().equals("a")&& rgb2.getText().equals("a")&& rgb3.getText().equals("a")) {
text1.setForeground(Color.WHITE);
}
Here's an example
My Jtextfields and Jbuttons are being duplicated in the same window, and appear to function exactly the same.
This is probably an easy fix, but as you can tell I'm pretty awful at coding.
(oh and some of the names for variables and such are placeholders :p)
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.GridLayout;
import java.awt.Font;
import java.awt.FlowLayout;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.JTextField;
import javax.swing.JLabel;
public class Adding extends JFrame {
public Adding(Heavy_Lifting lifting) {
addUI(lifting);
}
public void addUI(final Heavy_Lifting lifting) {
setLayout(new FlowLayout());
JButton addButton = new JButton("Enter");
JButton backButton = new JButton("Quit");
final JTextField eInput = new JTextField("Enter english name");
final JTextField mInput = new JTextField("Enter maori name");
final JTextField dInput = new JTextField("Enter description");
//add(addButton);
//add(eInput);
//add(mInput);
//add(backButton);
//add(dInput);
Dimension x = new Dimension(500, 50);
//addButton.setText("Enter");
addButton.setPreferredSize(x);
//backButton.setText("Quit");
backButton.setPreferredSize(x);
//eInput.setText("Enter english name");
eInput.setPreferredSize(x);
//mInput.setText("Enter maori name");
mInput.setPreferredSize(x);
//dInput.setText("Enter description");
dInput.setPreferredSize(x);
add(addButton);
add(eInput);
add(mInput);
add(dInput);
add(backButton);
addButton.addActionListener(new ActionListener() {#
Override
public void actionPerformed(ActionEvent e) {
String mname = mInput.getText();
String ename = eInput.getText();
String desc = dInput.getText();
PeePee p = new PeePee(mname);
Description d = new Description(desc);
if (lifting.allChar(ename, p)) {
lifting.insert(ename, p);
lifting.insert(ename, d);
eInput.setText("1");
mInput.setText("2");
dInput.setText("3");
} else {
eInput.setText("4");
mInput.setText("5");
dInput.setText("6");
}
}
});
backButton.addActionListener(new ActionListener() {#
Override
public void actionPerformed(ActionEvent event) {
setVisible(false);
}
});
setTitle("placeholder");
setSize(550, 300);
setMinimumSize(new Dimension(550, 300));
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
}
}
This looks like your public method addUI is called twice, maybe by another class. Try switch it to private and see if it still runs and produces the same visual output.
I have to create a Library for lending Media Items. So far I have the GUI to look how it should, but now I've hit a brick wall with what to do next. When the user clicks the 'Add' button, two prompts should open asking the user for the Title and then the Format, but I have no clue what to do to achieve it. Any help would be great!
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
public class FinalView extends JFrame{
private JButton add;
private JButton checkOut;
private JButton checkIn;
private JButton delete;
private JScrollPane display;
private JList jlist;
Scanner input = new Scanner(System.in);
public FinalView(){
setTitle("My Library");
setSize(500,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//change later for save feature per printout
setLocationRelativeTo(null);
setLayout(new BorderLayout());
JPanel buttonPanel = new JPanel();
add(buttonPanel, BorderLayout.SOUTH);
buttonPanel.setLayout(new GridLayout(1, 4, 8, 8));
add = new JButton("Add");
checkOut = new JButton("Check Out");
checkIn = new JButton("Check In");
delete = new JButton("Delete");
buttonPanel.add(add);
buttonPanel.add(checkOut);
buttonPanel.add(checkIn);
buttonPanel.add(delete);
String[] movie = {"Star Wars", "StarTrek"};
Library call = new Library();
jlist = new JList(movie);
jlist.setVisibleRowCount(4);
jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
add(new JScrollPane(jlist));
setVisible(true);
ActionListener listener = null;
add.addActionListener(listener);
}
public void actionPerformed(ActionEvent e) {
JOptionPane.showInputDialog("Title:");
String title = input.nextLine();
JOptionPane.showInputDialog("Format:");
String format = input.nextLine();
addNewItem(title, format);
}
private void addNewItem(String title, String format) {
MediaItem lib = new MediaItem(title, format);
// TODO Auto-generated method stub
}
public static void main(String[] args) {
new FinalView();
}
}
I'm writing a simple program using CardLayout. The main screen should display a button which when pressed would go to the next screen which contains another button for another screen. My problem is that when I run my program the screen is black. I tried following tutorials online to write my own program but I don't seem to find the problem with my code. I don't get any errors when run. Here is my code
//using CardLayout to change screen when action is performed
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Popup;
import javax.swing.JOptionPane;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
public class CL extends JFrame {
JPanel cardPanel;
JPanel cardPanelA;
JPanel cardPanelB;//to set different screens
CardLayout cl;
private JButton button1;
private JButton button2;
private JButton change;
private JLabel label;
private JTextField textField1;
private JTextField textField2;
JButton button;
public CL() {
super("This is a sample");
cardPanel = new JPanel();
cardPanelA = new JPanel();
cardPanelB = new JPanel();
cl = new CardLayout();
cardPanel.setLayout(cl);
button1 = new JButton("button1");
button2 = new JButton("button2");
change = new JButton("change screen");
label = new JLabel("this is a label");
textField1 = new JTextField(10);
textField2 = new JTextField("enter text", 6);
cardPanelA.add(change);
cardPanelA.add(label);
cardPanelA.add(textField1);
cardPanelA.add(textField2);
cardPanelB.add(button1);
cardPanelB.add(button2);
cardPanel.add(cardPanelA);
cardPanel.add(cardPanelB);
JPanel panel1 = new JPanel();
button = new JButton("initial button");
panel1.add(button);
theHandler handler = new theHandler();//make action listener
change.addActionListener(handler);
button1.addActionListener(handler);
button2.addActionListener(handler);
button.addActionListener(handler);
/*
getContentPane().add(panel1, BorderLayout.NORTH);
getContentPane().add(cardPanelA, BorderLayout.NORTH);
getContentPane().add(cardPanelB, BorderLayout.NORTH);
*/
}
private class theHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource() == button) {
cl.show(cardPanel, "Panel A");
}
if (event.getSource() == change) {
cl.show(cardPanelB, "panelB");
}
if (event.getSource() == button2) {
cl.show(cardPanel, "PanelA");
}
if (event.getSource() == button1) {
JOptionPane.showMessageDialog(null, "this is the second screen");
}
}
}
}
/*way to use CardLayout: create a CardLayout manager and create a bunch of different JPanels which
* would each be a different screen. Make a panel that stores the CardLayout as the layout.
* Add the different elements to each Panel(buttons, textfields) and then add the panels to the JPanel that stores
* the CardLayout
*/
import javax.swing.JFrame;
public class CardTest {
public static void main(String[] args) {
CL object = new CL();
object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
object.setSize(400, 400);
object.setVisible(true);
}
}
It might be something simple but I'm not sure of what it is. Some advice would be appreciated.
Make sure you add your panels to the frame
add(cardPanel);
Without that no components will be shown