No output is shown - java

I made a frame and showing a JOptionPane on click of a button but my code is compiling but not generating any output.
I copy the JOptionPane content from -how to make Dialogs blog.
Here is my code:
No output is shown on screen.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class DialogDemo extends JFrame {
private static final long serialVersionUID = 1L;
private JButton btnNext =null;
private JPanel contentPane;
private JFrame frame = null;
public DialogDemo() {
initialize();
}
public void initialize() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(600, 335, 200, 150);
getContentPane();
getBtnNext().addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(frame,"Eggs are not supposed to be green.");
}
});
}
public JPanel getContentPane() {
if (contentPane == null) {
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
contentPane.add(getBtnNext());
}
return contentPane;
}
public JButton getBtnNext() {
if (btnNext == null) {
btnNext = new JButton("Next");
btnNext.setBounds(20, 50, 150, 25);
}
return btnNext;
}
public static void main(String[] args) {
DialogDemo dd=new DialogDemo();
}
}

set visibility of frame to true :)
setVisible(true);

You forgot to set it visible.
Add this line:
setVisible(true);.
Without this, not frame will be displayed.

Related

Is it possible to make a toggleable button that changes text when clicked

I have made a JFrame that shows a start button, and changes to stop when clicked. How to make it so that it changes its text to start when stop is clicked. Here is the source code:
public class FRMCountdown extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FRMCountdown frame = new FRMCountdown();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public FRMCountdown() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton Start_Stop_btn = new JButton("Start");
Start_Stop_btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Start_Stop_btn.setText("Stop");
}
});
Start_Stop_btn.setBounds(10, 188, 89, 23);
contentPane.add(Start_Stop_btn);
}
}
Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section.
Swing was designed to be used with layout managers. I used a FlowLayout to place one JButton. Null layouts and absolute positioning lead to problems.
Java field names start with a lower case letter, Java method names start with a lower case letter. Java class names start with an upper case letter.
Here's the modified code.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ToggleJButton {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new ToggleJButton();
}
});
}
public ToggleJButton() {
JFrame frame = new JFrame("Toggle JButton");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 100, 5, 100));
JButton startStopButton = new JButton("Start");
startStopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JButton button = (JButton) event.getSource();
String text = button.getText();
if (text.contentEquals("Start")) {
text = "Stop";
} else {
text = "Start";
}
button.setText(text);
}
});
panel.add(startStopButton);
return panel;
}
}

jtextfield increment by 1 starting from B3001 when click add button in swing

My bill number(using jtextfield) must auto increment by 1 after clicking submit button. I need sequtenial values in format like B3001,B3002, and so on.... Kindly plz help me
public static class SequentialNumber
{
private static int currentNumber=3000;
public static String GetNextNumber()
{
currentNumber++;
return "B"+currentNumber;
}
}
public Printbill() {
contentPane1 = new JPanel();
contentPane1.setBackground(Color.WHITE);
contentPane1.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane1.setLayout(null);
dcfield = new JTextField();
dcfield.setBounds(553, 109, 86, 20);
contentPane1.add(dcfield);
dcfield.setColumns(10);
String ContractNo=SequentialNumber.GetNextNumber();
dcfield.setText(ContractNo);
JButton btnAdd = new JButton("ADD");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Printbill p=new Printbill();
dispose();
p.setVisible(true);
SequentialNumber s=new SequentialNumber();
String stt=s.GetNextNumber();
dcfield.setText(stt);
}
});
}
}
Here is the working code
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.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
public class Printbill extends JFrame {
public static class SequentialNumber {
private static int currentNumber = 3000;
public static String GetNextNumber() {
currentNumber++;
return "B" + currentNumber;
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Printbill();
}
});
}
public Printbill() {
JPanel contentPane1 = new JPanel();
contentPane1.setBackground(Color.WHITE);
contentPane1.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane1.setLayout(new BorderLayout(5, 5));
final JTextField dcfield = new JTextField();
contentPane1.add(dcfield);
dcfield.setColumns(10);
String contractNo = SequentialNumber.GetNextNumber();
dcfield.setText(contractNo);
JButton btnAdd = new JButton("ADD");
contentPane1.add(btnAdd, BorderLayout.EAST);
getContentPane().add(contentPane1);
btnAdd.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
String stt = SequentialNumber.GetNextNumber();
dcfield.setText(stt);
}
});
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
}
What's wrong with your code:
You've tried to use absolute layout (setLayout(null)). It's bad practice, so I've change the layout to BorderLayout. After that I've added pack() to set the appropriate size for window (this method works only with a non-null layout). For more information read here.
I've changed action listener so it only update text field.
SwingUtilities.invokeLater is required for correct start of Swing application. All widget initialisation/manipulation must be performed in Event Dispatcher Thread (EDT). SwingUtilities.invokeLater allows to call a piece of code in EDT.

How to open a new JPanel with a JButton?

I am trying to code a program with multiple screens, however, I do not want to use tabbed panes. I have looked at using multiple JPanels with the card layout and the methods are simply not working. What I need to be able to do is load the new JPanel when a button is clicked. Here is my code:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class IA extends JFrame {
private JPanel contentPane;
private JPanel home;
private JPanel clients;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
IA frame = new IA();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public IA() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new CardLayout(0, 0));
JPanel home = new JPanel();
contentPane.add(home, "name_714429679706141");
home.setLayout(null);
JButton btnClients = new JButton("Clients");
btnClients.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
home.setVisible(false);
clients.setVisible(true);
}
});
btnClients.setBounds(160, 108, 89, 23);
home.add(btnClients);
JPanel clients = new JPanel();
contentPane.add(clients, "name_714431450350356");
clients.setLayout(null);
JButton btnHome = new JButton("Home");
btnHome.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clients.setVisible(false);
home.setVisible(true);
}
});
btnHome.setBounds(169, 107, 89, 23);
clients.add(btnHome);
}
}
The problem is that you have duplicate variables home and clients .
The folllowing is your modified code to fix that, with comments on the changed lines (five lines total) :
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class IA extends JFrame {
private final JPanel contentPane;
// private final JPanel home; // REMOVED
// private JPanel clients; // REMOVED
/**
* Launch the application.
*/
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
IA frame = new IA();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public IA() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new CardLayout(0, 0));
final JPanel home = new JPanel();
contentPane.add(home, "name_714429679706141");
home.setLayout(null);
final JPanel clients = new JPanel(); // MOVED UP
contentPane.add(clients, "name_714431450350356"); // MOVED UP
clients.setLayout(null); // MOVED UP
JButton btnClients = new JButton("Clients");
btnClients.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
home.setVisible(false);
clients.setVisible(true);
}
});
btnClients.setBounds(160, 108, 89, 23);
home.add(btnClients);
JButton btnHome = new JButton("Home");
btnHome.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
clients.setVisible(false);
home.setVisible(true);
}
});
btnHome.setBounds(169, 107, 89, 23);
clients.add(btnHome);
}
}
I would take a look at this post, however I have a feeling you'll need to use a actionlistener to get this done...
Java Swing. Opening a new JPanel from a JButton and making the buttons pretty
I would of left this as a comment but apparently you need 50 rep for that...
This link might be more helpful.. How to open a new window by clicking a button
When the following code is invoked the clients variable equals to null.
JButton btnClients = new JButton("Clients");
btnClients.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
home.setVisible(false);
clients.setVisible(true);
}
});
Write this:
JPanel clients = new JPanel();
contentPane.add(clients, "name_714431450350356");
clients.setLayout(null);
JButton btnHome = new JButton("Home");
btnHome.setBounds(169, 107, 89, 23);
clients.add(btnHome);
before you add the Action Listener

How to pass value JList item from JFrame into another JFrame (just value)?

I have write a test with two class.
The first JPanel, Gestion: JFrame with jlist + button (the button open the Jlist 2, PanelTest)
The second JPanel, PanelTest: JFrame and I want to recover in String, the select value item in the JFrame Gestion (JList)
How to do that ?
Gestion.java:
package IHM;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.List;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JList;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
public class Gestion extends JFrame {
private DocumentListener myListener;
public String test;
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Gestion frame = new Gestion();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public String getTest() {
return test;
}
/**
* Create the frame.
* #throws Exception
*/
public Gestion() throws Exception {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
final PanelTest panel2 = new PanelTest();
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
String choix[] = {" Pierre", " Paul", " Jacques", " Lou", " Marie"};
final JList list = new JList(choix);
panel.add(list);
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent arg0) {
test = (String) list.getSelectedValue();
System.out.println(test);
// PanelTest.setValue(test);
}
});
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.SOUTH);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
new PanelTest().setVisible(true);
fermerFenetre();
}
});
panel_1.add(btnNewButton);
}
public void fermerFenetre(){
this.setVisible(false);
}
}
PanelTest.java
package IHM;
import java.awt.BorderLayout;
public class PanelTest extends JFrame {
public String tyty;
private JPanel contentPane;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PanelTest frame = new PanelTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public PanelTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
textField = new JTextField();
contentPane.add(textField, BorderLayout.WEST);
textField.setColumns(10);
}
}
Suggestions:
Make your list variable a field, not a local variable, or else make it a final local variable so that it is accessible inside of the anonymous ActionListener.
Obtain the selected list item in your ActionListener where you launch the 2nd window.
Pass that String into your PanelTest object via a String parameter.
The second window should be a dialog such as a JDialog, not a JFrame.
As an aside, you'll rarely want to have your GUI classes extend top level windows such as JFrames or JDialogs as that greatly limits the flexibility of your GUI code.
For example,
import java.awt.Component;
import java.awt.Dialog.ModalityType;
import java.awt.Window;
import java.awt.event.ActionEvent;
import javax.swing.*;
#SuppressWarnings("serial")
public class Gestion2 extends JPanel {
private static final String CHOIX[] = { " Pierre", " Paul", " Jacques",
" Lou", " Marie" };
private JList<String> choixList = new JList<>(CHOIX);
public Gestion2() {
JPanel listPanel = new JPanel();
listPanel.add(new JScrollPane(choixList));
JPanel btnPanel = new JPanel();
btnPanel.add(new JButton(new ListSelectAction("Select Item and Press")));
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
add(listPanel);
add(btnPanel);
}
private class ListSelectAction extends AbstractAction {
public ListSelectAction(String name) {
super(name);
}
#Override
public void actionPerformed(ActionEvent e) {
String selectedItem = choixList.getSelectedValue();
if (selectedItem != null) {
PanelTest2 panelTest2 = new PanelTest2(selectedItem);
Component component = (Component) e.getSource();
Window win = SwingUtilities.getWindowAncestor(component);
// JOptionPane example
JOptionPane.showMessageDialog(win, panelTest2,
"JOptionPane Example", JOptionPane.PLAIN_MESSAGE);
// or JDialog example
JDialog dialog = new JDialog(win, "JDialog Example",
ModalityType.APPLICATION_MODAL);
dialog.add(panelTest2);
dialog.pack();
dialog.setLocationRelativeTo(win);
dialog.setVisible(true);
}
}
}
private static void createAndShowGui() {
Gestion2 mainPanel = new Gestion2();
JFrame frame = new JFrame("Gestion2");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
#SuppressWarnings("serial")
class PanelTest2 extends JPanel {
private String selectedItem;
private JTextField textField = new JTextField(10);
public PanelTest2(String selectedItem) {
this.selectedItem = selectedItem;
textField.setText(selectedItem);
add(new JLabel("Selected Item:"));
add(textField);
}
public String getSelectedItem() {
return selectedItem;
}
}

Background color does not change in bluej

I have written a code that has a label and a button in a frame. I have also changed the background but it never changes.
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
public class Frames
{
JFrame Main_Menu=new JFrame("MAIN MENU");JFrame CIRCUMFERENCE=new JFrame("CIRCUMFERENCE");
JFrame AREA=new JFrame("AREA");JFrame PERIMETER=new JFrame("PERIMETER");JFrame SETS=new JFrame("SETS");
JFrame FUNDAMENTAL_OPRATIONS=new JFrame("FUNDAMENTAL OPRATIONS");JFrame POWER_AND_ROOTS=new JFrame("POWER_AND_ROOTS");
void Main_Menu()
{
JPanel contentPane = (JPanel) Main_Menu.getContentPane();
contentPane.setLayout(new BorderLayout(10,10));
contentPane.setBorder(new EmptyBorder(300, 150, 300, 150));
contentPane.setLayout(new GridLayout(4, 4));
JPanel buttonPanel = new JPanel(new GridLayout(8,8));
contentPane.add(Labels.Main_MENU,BorderLayout.NORTH);
contentPane.add(Buttons.SETS,BorderLayout.SOUTH);
Main_Menu.setBackground(Color.YELLOW);
Main_Menu.pack();
Main_Menu.setVisible(true);
}
}
You should actually be setting the background color of the content pane via getContentPane().setBackground(Color.YELLOW):
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
public class Frames extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new Frames();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Frames() {
setSize(new Dimension(100, 100));
setTitle("MAIN MENU");
getContentPane().setBackground(Color.YELLOW);
setVisible(true);
}
}
Also, consider using variable naming conventions; for instance, Main_Menu should be named as mainMenu.

Categories