I am learning Java and working on a project. I want to add a new panel as the button is clicked. It is working, but if user clicks button again it adds the panel again. I want to remove the existing panel before adding new one. I have tried remove(westPanel); revalidate(); at the start of my actionPerformed method, but its not working.
Can someone please help?
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyJpanel extends JPanel implements ActionListener
{ //declaring some buttons
private JButton newAccountButton, deleteAccountButton, searchButton, showButton, depositButton, withdrawButton;
protected JPanel westPanel, panel;
JLabel message = new JLabel("Please Use Buttons For Interation");
public MyJpanel()
{
newAccountButton = new JButton("Create Account");
deleteAccountButton = new JButton("Delete Account");
searchButton = new JButton("Search Account");
showButton = new JButton("Show Accounts");
depositButton = new JButton("Deposit");
withdrawButton = new JButton("Withdraw");
setBackground(Color.GRAY);
add(newAccountButton);
add(deleteAccountButton);
add(searchButton);
add(showButton);
add(depositButton);
add(withdrawButton);
newAccountButton.addActionListener(this);
}
#Override
public void actionPerformed(ActionEvent e)
{ westPanel= new JPanel();
if (e.getSource()==newAccountButton)
{ getRootPane().remove(westPanel);
revalidate();
JLabel accountNumber = new JLabel("Account Number");
JTextField tfaccountnumber = new JTextField(10);
JLabel name = new JLabel("Owner Name");
JTextField tfName = new JTextField(10);
JLabel password = new JLabel("Password");
JTextField tfPassword = new JTextField(10);
JLabel deposit = new JLabel("Deposit Amount");
JTextField tfDeposit = new JTextField(10);
westPanel.setLayout(new GridLayout(4,2));
westPanel.setBackground(getBackground());
westPanel.add(accountNumber);
westPanel.add(tfaccountnumber);
westPanel.add(name);
westPanel.add(tfName);
westPanel.add(password);
westPanel.add(tfPassword);
westPanel.add(deposit);
westPanel.add(tfDeposit);
add(westPanel);
revalidate();
}
}
}
Related
I'm currently working on a project and whenever I click the jbutton on jframe 2 (the 2nd jframe after the log in) "set appointments"/btn1, it won't show the other jframe which is jframe3.
The program itself works but the button won't show the other jframe.
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.Container;
public class me {
public static void main (String [] args) {
JFrame jframe = new JFrame();
jframe.setSize(450,350);
jframe.getContentPane().setBackground(Color.WHITE);
ImageIcon c = new ImageIcon("teethlogo5.png");
JLabel bi = new JLabel("",c,JLabel.RIGHT);
bi.setBounds(25,35,400,40);
jframe.add(bi);
ImageIcon a = new ImageIcon("teethlogo2.png");
JLabel si = new JLabel("",a,JLabel.RIGHT);
si.setBounds(50,90,100,120);
jframe.add(si);
JLabel jl1 = new JLabel("Username:");
jl1.setBounds(190,100,100,50);
jframe.add(jl1);
JTextField uss = new JTextField();
uss.setBounds(270,110,120,30);
jframe.add(uss);
JLabel jl2 = new JLabel("Password:");
jl2.setBounds(190,150,100,50);
jframe.add(jl2);
JPasswordField pss = new JPasswordField();
pss.setBounds(270,160,120,30);
jframe.add(pss);
JButton enter = new JButton("log in");
enter.setBounds(250,210,100,40);
jframe.add(enter);
enter.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String userText;
String pwdText;
userText = uss.getText();
pwdText = String.valueOf(pss.getPassword());
if (userText.equals("user") && pwdText.equals("pass")) {
JOptionPane.showMessageDialog(null, "LoginSuccessful","Message",JOptionPane.PLAIN_MESSAGE);
jframe.setVisible(false);
JFrame jframe2 = new JFrame();
jframe2.setSize(850,560);
jframe2.getContentPane().setBackground(Color.WHITE);
ImageIcon b = new ImageIcon("teethlogo4.png");
JLabel sii = new JLabel("",b,JLabel.RIGHT);
sii.setBounds(10,0,600,100);
jframe2.add(sii);
JButton btn1 = new JButton("Set an Appointment");
btn1.setBounds(100,100,150,30);
jframe2.add(btn1);
JButton btn2 = new JButton("View Appointments");
btn2.setBounds(270,100,150,30);
jframe2.add(btn2);
btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if (btn1.isSelected()){
jframe2.setVisible(false);
JFrame jframe3 = new JFrame();
jframe3.setSize(850,560);
jframe3.getContentPane().setBackground(Color.WHITE);
jframe3.setLayout(null);
jframe3.setVisible(true);
jframe3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
});
jframe2.setLayout(null);
jframe2.setVisible(true);
jframe2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
else {
JOptionPane.showMessageDialog(null, "Invalid Username or Password","Message",JOptionPane.PLAIN_MESSAGE);
uss.setText(null);
pss.setText(null);
}
}
});
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setResizable(false);
jframe.setLayout(null);
jframe.setVisible(true);
}
}
I'm a beginner in java and I really want to know how to fix this problem.
You're shooting your own self in the foot with:
if (btn1.isSelected()){
// ...
}
There is no need for this block of code -- a button isn't "selected" unless it extends from JToggleButton (such as JCheckBox) and has been checked, something that a JButton does not allow, and what is more, it is preventing the listener's code that it holds from running. Solution: just get rid of it.
I am trying to make a calculator program where click a button and then it would add to the Java Text Field(Not the Java Text Area), and I use the ActionEvent e to figure out the action. Then using the actionCommand where I get the actionListener I try to add the text to the java text field. And somehow it is not updating into the java text field. Is this a problem with repaint or something.
Here's my code
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.io.Writer;
import java.util.ArrayList;
public class Calculator extends JFrame implements ActionListener {
String actionCommand = "";
ArrayList<Integer> numberSet1 = new ArrayList<Integer>();
ArrayList<Integer> numberSet2 = new ArrayList<Integer>();
JLabel jl = new JLabel();
JPanel jp = new JPanel();
int number1 = 0;
int number2 = 0;
JTextField jtf = new JTextField();
JButton btn1;
JTextArea jta = new JTextArea();
public Calculator() {
super("Calculator");
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(600,600);
jp.setLayout(new BorderLayout());
JTextField jtf = new JTextField("This is the text field");
jtf.setSize(getWidth(),50);
add(jtf, BorderLayout.NORTH);
jtf.setBackground(Color.YELLOW);
jtf.setEditable(false);
JPanel jp = new JPanel();
Font myFont = new Font("Serif", Font.BOLD, 32);
jp.setLayout(new GridLayout(4,4));
JButton butt0 = new JButton("0");
butt0.setFont(myFont);
JButton butt1 = new JButton("1");
butt1.setFont(myFont);
JButton butt2 = new JButton("2");
butt2.setFont(myFont);
JButton butt3 = new JButton("3");
butt3.setFont(myFont);
JButton butt4 = new JButton("4");
butt4.setFont(myFont);
JButton butt5 = new JButton("5");
butt5.setFont(myFont);
JButton butt6 = new JButton("6");
butt6.setFont(myFont);
JButton butt7 = new JButton("7");
butt7.setFont(myFont);
JButton butt8 = new JButton("8");
butt8.setFont(myFont);
JButton butt9 = new JButton("9");
butt9.setFont(myFont);
JButton butt10 = new JButton("+");
butt10.setFont(myFont);
JButton butt11 = new JButton("-");
butt11.setFont(myFont);
JButton butt12 = new JButton("*");
butt12.setFont(myFont);
JButton butt13 = new JButton("/");
butt13.setFont(myFont);
JButton butt14 = new JButton("=");
butt14.setFont(myFont);
JButton butt15 = new JButton("clear");
butt15.setFont(myFont);
butt1.setBackground(Color.cyan);
butt2.setBackground(Color.cyan);
butt3.setBackground(Color.cyan);
butt4.setBackground(Color.cyan);
butt5.setBackground(Color.cyan);
butt6.setBackground(Color.cyan);
butt7.setBackground(Color.cyan);
butt8.setBackground(Color.cyan);
butt9.setBackground(Color.cyan);
butt10.setBackground(Color.red);
butt11.setBackground(Color.red);
butt12.setBackground(Color.red);
butt13.setBackground(Color.red);
butt14.setBackground(Color.red);
butt15.setBackground(Color.red);
butt0.setBackground(Color.cyan);
butt10.setForeground(Color.lightGray);
butt11.setForeground(Color.lightGray);
butt12.setForeground(Color.lightGray);
butt13.setForeground(Color.lightGray);
butt14.setForeground(Color.lightGray);
butt15.setForeground(Color.lightGray);
jp.add(butt0);
jp.add(butt1);
jp.add(butt2);
jp.add(butt3);
jp.add(butt4);
jp.add(butt5);
jp.add(butt6);
jp.add(butt7);
jp.add(butt8);
jp.add(butt9);
jp.add(butt10);
jp.add(butt11);
jp.add(butt12);
jp.add(butt13);
jp.add(butt14);
jp.add(butt15);
butt0.addActionListener(this);
butt0.setActionCommand("0");
butt1.addActionListener(this);
butt1.setActionCommand("1");
butt2.addActionListener(this);
butt2.setActionCommand("2");
butt3.addActionListener(this);
butt3.setActionCommand("3");
butt4.addActionListener(this);
butt4.setActionCommand("4");
butt5.addActionListener(this);
butt5.setActionCommand("5");
butt6.addActionListener(this);
butt6.setActionCommand("6");
butt7.addActionListener(this);
butt7.setActionCommand("7");
butt8.addActionListener(this);
butt8.setActionCommand("8");
butt9.addActionListener(this);
butt9.setActionCommand("9");
butt10.addActionListener(this);
butt10.setActionCommand("+");
butt11.addActionListener(this);
butt11.setActionCommand("-");
butt12.addActionListener(this);
butt12.setActionCommand("*");
butt13.addActionListener(this);
butt13.setActionCommand("/");
butt14.addActionListener(this);
butt14.setActionCommand("=");
butt15.addActionListener(this);
butt15.setActionCommand("clear");
add(jp, BorderLayout.CENTER);
setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
actionCommand = e.getActionCommand();
System.out.println("Clicked" + actionCommand);
jtf.setText(actionCommand);
}
public void phase1() {
while (!(actionCommand.equals("+")) || !(actionCommand.equals("-")) || !(actionCommand.equals("*")) || !(actionCommand.equals("/")) || !(actionCommand.equals("clear")) || !(actionCommand.equals("="))) {
numberSet1.add(Integer.parseInt(actionCommand));
}
for(int i = 0; i < numberSet1.size(); i++) {
}
}
public void phase2() {
}
public int calculations() {
return 0;
}
public static void main(String[] args) {
Calculator calc = new Calculator();
}
}
You have two different JTextField variables that are both named jtf.
The first is an instance variable, which you have declared with JTextField jtf = new JTextField();, and is accessible from anywhere in the class.
The second is a local variable, which you have declared with JTextField jtf = new JTextField("This is the text field");, and is only accessible in the constructor of Calculator.
The problem is that the second jtf (the local variable) is being added to the UI, while the first jtf (the instance variable) is what's being updated by the action event.
To fix this, change this (near the top of the class): JTextField jtf = new JTextField();
to this: JTextField jtf;
And then change this (in the constructor):
JTextField jtf = new JTextField("This is the text field");
to this: jtf = new JTextField("This is the text field");
Then, you'll only have one jtf variable (which will be an instance variable), and your action event should work.
Just remove this line:
JTextField jtf = new JTextField("This is the text field");
from constructor.
The first is an instance variable, JTextField e.g. jtf is accessible from anywhere in the class.
I have this code. When I run it, it displays a JFrame "menu" with two choices. When I click on a button it now displays the menu AND the new JFrame in the same window. And when I click on the other button, it adds with the existing things displayed on the same Window and adds and add inside. I'm thinking of using just one window/JFrame. How do I clear out the past JFrame? Is this possible?
package finals;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Finals extends JFrame implements ActionListener{
JLabel In1, In2, choice,menu;
JTextField In, Inn, choicee, info, menuu, lol,loll;
JButton PtoS, StoP,stop,ptos;
public Finals(){
setTitle("Menu");
setSize(200,150);
menu = new JLabel("Select a Program:");
PtoS = new JButton("Proposition to Sentence");
StoP = new JButton("Sentence to Proposition");
Container pane = getContentPane();
pane.setLayout(new FlowLayout());
pane.add(menu);
pane.add(PtoS);
pane.add(StoP);
PtoS.addActionListener(this);
StoP.addActionListener(this);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == StoP){
setTitle("Sentence to Proposition");
setSize(500,500);
In1 = new JLabel("Enter First Sentence:", SwingConstants.RIGHT);
choice = new JLabel("Enter Operator:", SwingConstants.RIGHT);
In2 = new JLabel("Enter Second Sentence", SwingConstants.RIGHT);
lol = new JTextField("[a]= AND [b]= OR [c]= NOT");
lol.setEditable(false);
loll = new JTextField();
loll.setEditable(false);
In = new JTextField();
choicee = new JTextField();
Inn = new JTextField();
Container cont = getContentPane();
cont.setLayout(new GridLayout(5,5));
cont.add(In1);
cont.add(In);
cont.add(lol);
cont.add(loll);
cont.add(choice);
cont.add(choicee);
cont.add(In2);
cont.add(Inn);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
else if(e.getSource() == PtoS){
setTitle("Proposition to Sentence");
setSize(500,500);
In1 = new JLabel("Enter p:", SwingConstants.RIGHT);
choice = new JLabel("Enter Operator:", SwingConstants.RIGHT);
In2 = new JLabel("Enter q:", SwingConstants.RIGHT);
lol = new JTextField("[a]= AND [b]= OR [c]= NOT");
lol.setEditable(false);
loll = new JTextField();
loll.setEditable(false);
In = new JTextField();
choicee = new JTextField();
Inn = new JTextField();
Container cont = getContentPane();
cont.setLayout(new GridLayout(5,2));
cont.add(In1);
cont.add(In);
cont.add(lol);
cont.add(loll);
cont.add(choice);
cont.add(choicee);
cont.add(In2);
cont.add(Inn);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
public static void main(String[] args){
Finals X = new Finals();
}
}
I have several JPanels that need to be displayed at the same time. They do display when I press the Port Settings button but I need there to be a title above each of the panels so the user will know which option they are selecting. My code as well as a screenshot is below.
package myGUI;
import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TestApplication implements ActionListener {
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setSize(3000, 3000);
frame.setTitle("RBA Test Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JButton select1 = new JButton("Select");
JButton select2 = new JButton("Select");
JButton select3 = new JButton("Select");
JButton select4 = new JButton("Select");
//Make the drop down lists
String[] baudrates = {"57600", "115200", "128000", "256000"};
JComboBox baudlist = new JComboBox(baudrates);
String[] baudrates2 = {"57600", "115200", "128000", "256000"};
JComboBox baudlist2 = new JComboBox(baudrates2);
String[] bytesizes = {"7", "8"};
JComboBox bytelist = new JComboBox(bytesizes);
String[] bytesizes2 = {"7", "8"};
JComboBox bytelist2 = new JComboBox(bytesizes2);
String[] stopbit = {"1", "2"};
JComboBox stoplist = new JComboBox(stopbit);
String[] stopbit2 = {"1", "2"};
JComboBox stoplist2 = new JComboBox(stopbit2);
String[] flows = {"None", "Hardware","Xon", "Xoff"};
JComboBox flowlist = new JComboBox(flows);
String[] flows2 = {"None", "Hardware","Xon", "Xoff"};
JComboBox flowlist2 = new JComboBox(flows2);
String[] paritys = {"None", "Even", "Odd"};
JComboBox paritylist = new JComboBox(paritys);
String[] paritys2 = {"None", "Even", "Odd"};
JComboBox paritylist2 = new JComboBox(paritys2);
JLabel ipLabel = new JLabel("IP Address: ");
JLabel connectLabel = new JLabel("Connect Time: ");
JLabel sendLabel = new JLabel("Send Time Out: ");
JLabel receiveLabel = new JLabel("Receive Time Out: ");
JLabel portLabel = new JLabel("Port: ");
JLabel baudrate = new JLabel("Baud Rate: ");
JLabel bytesize = new JLabel("Byte Size: ");
JLabel stopbits = new JLabel("Stop Bits: ");
JLabel flow = new JLabel("Flow Con...: ");
JLabel parity = new JLabel("Parity: ");
JLabel stoLabel = new JLabel("Send Time Out: ");
JLabel rtoLabel = new JLabel("Receive Time Out: ");
JLabel portLabel2 = new JLabel("Port: ");
JLabel baudrate2 = new JLabel("Baud Rate: ");
JLabel bytesize2 = new JLabel("Byte Size: ");
JLabel stopbits2 = new JLabel("Stop Bits: ");
JLabel flow2 = new JLabel("Flow Con...: ");
JLabel parity2 = new JLabel("Parity: ");
JLabel stoLabel2 = new JLabel("Send Time Out: ");
JLabel rtoLabel2 = new JLabel("Receive Time Out: ");
JLabel portLabel3 = new JLabel("Port: ");
JLabel vendor = new JLabel("Vendor ID: ");
JLabel product = new JLabel("Product ID: ");
JLabel stoLabel3 = new JLabel("Send Time Out: ");
JLabel rtoLabel3 = new JLabel("Receive Time Out: ");
JLabel logLabel = new JLabel("Input / Output Log");
JTextField ip = new JTextField(10);
ip.setText("192.168.0.102");
JTextField ct = new JTextField(10);
ct.setText("5000");
JTextField rto = new JTextField(10);
rto.setText("5000");
JTextField sto = new JTextField(10);
sto.setText("5000");
JTextField port = new JTextField(10);
port.setText("12000");
JTextField sendto = new JTextField(10);
JTextField reto = new JTextField(10);
JTextField comport = new JTextField(10);
JTextField sendto2 = new JTextField(10);
JTextField reto2 = new JTextField(10);
JTextField comport2 = new JTextField(10);
JTextField vendorid = new JTextField(10);
JTextField productid = new JTextField(10);
JTextField sendtime = new JTextField(10);
JTextField receiveto = new JTextField(10);
JTextArea logbox = new JTextArea() {
#Override
public java.awt.Dimension getPreferredSize() {
return new Dimension(300, 450);
};
};
logLabel.setFont(new java.awt.Font("Tahoma", 3, 18));
logLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
logLabel.setText("Input / Output Log");
logbox.add(logLabel);
//Add components to the panels
final JPanel ethernetSettings = new JPanel();
ethernetSettings.setLayout(new GridLayout(6, 2));
ethernetSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
ethernetSettings.add(ipLabel);
ethernetSettings.add(ip);
ethernetSettings.add(connectLabel);
ethernetSettings.add(ct);
ethernetSettings.add(receiveLabel);
ethernetSettings.add(rto);
ethernetSettings.add(sendLabel);
ethernetSettings.add(sto);
ethernetSettings.add(portLabel);
ethernetSettings.add(port);
ethernetSettings.add(select1);
final JPanel usbHIDSettings = new JPanel();
usbHIDSettings.setLayout(new GridLayout(5, 2));
usbHIDSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
usbHIDSettings.add(vendor);
usbHIDSettings.add(vendorid);
usbHIDSettings.add(product);
usbHIDSettings.add(productid);
usbHIDSettings.add(stoLabel3);
usbHIDSettings.add(sendtime);
usbHIDSettings.add(rtoLabel3);
usbHIDSettings.add(receiveto);
usbHIDSettings.add(select2);
final JPanel usbCDCSettings = new JPanel();
usbCDCSettings.setLayout(new GridLayout(9, 2));
usbCDCSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
usbCDCSettings.add(baudrate2);
usbCDCSettings.add(baudlist);
usbCDCSettings.add(bytesize2);
usbCDCSettings.add(bytelist);
usbCDCSettings.add(stopbits2);
usbCDCSettings.add(stoplist);
usbCDCSettings.add(flow2);
usbCDCSettings.add(flowlist);
usbCDCSettings.add(parity2);
usbCDCSettings.add(paritylist);
usbCDCSettings.add(stoLabel2);
usbCDCSettings.add(sendto2);
usbCDCSettings.add(rtoLabel2);
usbCDCSettings.add(reto2);
usbCDCSettings.add(portLabel3);
usbCDCSettings.add(comport2);
usbCDCSettings.add(select3);
final JPanel rsSettings = new JPanel();
rsSettings.setLayout(new GridLayout(9, 2));
rsSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
rsSettings.add(baudrate);
rsSettings.add(baudlist2);
rsSettings.add(bytesize);
rsSettings.add(bytelist2);
rsSettings.add(stopbits);
rsSettings.add(stoplist2);
rsSettings.add(flow);
rsSettings.add(flowlist2);
rsSettings.add(parity);
rsSettings.add(paritylist2);
rsSettings.add(stoLabel);
rsSettings.add(sendto);
rsSettings.add(rtoLabel);
rsSettings.add(reto);
rsSettings.add(portLabel2);
rsSettings.add(comport);
rsSettings.add(select4);
final JPanel PortSettings = new JPanel();
PortSettings.setLayout(new GridLayout(1, 4));
PortSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
PortSettings.add(ethernetSettings);
PortSettings.add(rsSettings);
PortSettings.add(usbCDCSettings);
PortSettings.add(usbHIDSettings);
JButton portsettings = new JButton("Port Settings");
portsettings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog port = new JDialog(frame);
port.setTitle("Port Settings");
port.setSize(400, 400);
port.add(PortSettings);
port.pack();
port.setVisible(true);
}
});
JButton online = new JButton("Go Online");
JButton offline = new JButton("Go Offline");
JButton status = new JButton("Status");
JButton reboot = new JButton("Reboot");
JButton account = new JButton("Account");
account.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog accountDialog = new JDialog(frame);
accountDialog.setTitle("Account");
accountDialog.setSize(400, 400);
accountDialog.add(accountPanel);
accountDialog.pack();
accountDialog.setVisible(true);
}
});
JButton amount = new JButton("Amount");
amount.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog amount2 = new JDialog(frame);
amount2.setTitle("Amount");
amount2.setSize(400, 400);
amount2.add(amountPanel);
amount2.pack();
amount2.setVisible(true);
}
});
JButton reset = new JButton("Reset");
JButton approvordecl = new JButton("Approve / Decline");
approvordecl.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog apprv = new JDialog(frame);
apprv.setTitle("Approve / Decline");
apprv.setSize(400, 400);
apprv.add(apprvordecl);
apprv.pack();
apprv.setVisible(true);
}
});
JButton test = new JButton("Test Button #1");
JButton testing = new JButton("Test Button #2");
JRadioButton button = new JRadioButton("Radio Button");
JRadioButton button2 = new JRadioButton("Radio Button");
JCheckBox checkbox = new JCheckBox("Check Box");
JCheckBox checkbox2 = new JCheckBox("Check Box");
ButtonGroup approvegroup = new ButtonGroup();
approvegroup.add(apprve);
approvegroup.add(decline);
JPanel newButtonPanel = new JPanel();
newButtonPanel.add(online);
newButtonPanel.add(offline);
newButtonPanel.add(status);
newButtonPanel.add(reboot);
newButtonPanel.add(account);
newButtonPanel.add(amount);
newButtonPanel.add(reset);
newButtonPanel.add(approvordecl);
newButtonPanel.add(logLabel);
JPanel testPanel = new JPanel();
testPanel.add(button);
testPanel.add(button2);
testPanel.add(checkbox2);
JPanel posPanel = new JPanel();
posPanel.add(test);
posPanel.add(testing);
posPanel.add(checkbox);
JPanel llpPanel = new JPanel();
llpPanel.setLayout(new BorderLayout());
llpPanel.add(newButtonPanel, BorderLayout.PAGE_START);
llpPanel.add(logLabel, BorderLayout.CENTER);
llpPanel.add(new JScrollPane(logbox), BorderLayout.PAGE_END);
JPanel buttonPanel = new JPanel();
buttonPanel.add(initialize);
buttonPanel.add(connect);
buttonPanel.add(disconnect);
buttonPanel.add(shutdown);
buttonPanel.add(portsettings);
frame.add(buttonPanel);
frame.add(buttonPanel, BorderLayout.NORTH);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("LLP", null, llpPanel, "Low Level Protocol");
tabbedPane.addTab("POS",null, posPanel, "Point Of Sale");
tabbedPane.addTab("Test", null, testPanel, "Test");
JPanel tabsPanel = new JPanel(new BorderLayout());
tabsPanel.add(tabbedPane);
frame.add(tabsPanel, BorderLayout.CENTER);
frame.pack();
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
For titles on JPanel, you can use Title Border as below.
TitledBorder title = BorderFactory.createTitledBorder("YOUR_TITLE");
YOURPANEL.setBorder(title);
You could try creating a border above each of the panels like this example:
Make a JPanel border with title like in Firefox
Try this:
myPanel.setBorder(BorderFactory.createTitledBorder("MyTitle"));
I think you want to set the title on the frame and not the panel. JPanels don't have titles, but their parent frames do.
Try this:
SwingUtilities.getRoot(yourPanel).setTitle("SomeTitle");
I am making a better version of an application that was given to me. I still need to have the same components as the program that was given to me, I just need to reorganize it. I was also told not to copy any of the original code and do it from scratch. The first screenshot shows the textfield I need to add to my program from the original program. The second screenshot is my program. I am not sure if a JTextField would be best for this. My code is below. I have tried to add a JTextField with a JLabel and center it but when I run the program it covers everything else in the program and only shows a little white box.
import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
public class TestApplication implements ActionListener {
public static void main(String[] args) {
JLabel input = new JLabel();
final JFrame frame = new JFrame();
frame.setSize(1000, 1000);
frame.setTitle("RBA Test Application");
frame.add(input);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// Make all necessary buttons
JButton next = new JButton("Next");
JButton save = new JButton("Save");
JButton save2 = new JButton("Save");
JButton save3 = new JButton("Save");
JButton save4 = new JButton("Save");
JButton aok = new JButton("OK");
JButton bok = new JButton("OK");
JButton cok = new JButton("OK");
JButton acancel = new JButton("Cancel");
JButton bcancel = new JButton("Cancel");
JButton ccancel = new JButton("Cancel");
JButton dcancel = new JButton("Cancel");
JButton ecancel = new JButton("Cancel");
JButton fcancel = new JButton("Cancel");
JButton gcancel = new JButton("Cancel");
JButton hcancel = new JButton("Cancel");
//Make the drop down lists
String[] baudrates = {"57600", "115200", "128000", "256000"};
JComboBox baudlist = new JComboBox(baudrates);
String[] baudrates2 = {"57600", "115200", "128000", "256000"};
JComboBox baudlist2 = new JComboBox(baudrates2);
String[] bytesizes = {"7", "8"};
JComboBox bytelist = new JComboBox(bytesizes);
String[] bytesizes2 = {"7", "8"};
JComboBox bytelist2 = new JComboBox(bytesizes2);
String[] stopbit = {"1", "2"};
JComboBox stoplist = new JComboBox(stopbit);
String[] stopbit2 = {"1", "2"};
JComboBox stoplist2 = new JComboBox(stopbit2);
String[] flows = {"None", "Hardware","Xon", "Xoff"};
JComboBox flowlist = new JComboBox(flows);
String[] flows2 = {"None", "Hardware","Xon", "Xoff"};
JComboBox flowlist2 = new JComboBox(flows2);
String[] paritys = {"None", "Even", "Odd"};
JComboBox paritylist = new JComboBox(paritys);
String[] paritys2 = {"None", "Even", "Odd"};
JComboBox paritylist2 = new JComboBox(paritys2);
//Make all necessary labels
JLabel cardLabel = new JLabel("Card Number: ");
JLabel expLabel = new JLabel("Exp. Date (MM/YY): ");
JLabel cvvLabel = new JLabel("CVV: ");
JLabel ipLabel = new JLabel("IP Address: ");
JLabel connectLabel = new JLabel("Connect Time: ");
JLabel sendLabel = new JLabel("Send Time Out: ");
JLabel receiveLabel = new JLabel("Receive Time Out: ");
JLabel portLabel = new JLabel("Port: ");
JLabel baudrate = new JLabel("Baud Rate: ");
JLabel bytesize = new JLabel("Byte Size: ");
JLabel stopbits = new JLabel("Stop Bits: ");
JLabel flow = new JLabel("Flow Con..: ");
JLabel parity = new JLabel("Parity: ");
JLabel stoLabel = new JLabel("Send Time Out: ");
JLabel rtoLabel = new JLabel("Receive Time Out: ");
JLabel portLabel2 = new JLabel("Port: ");
JLabel baudrate2 = new JLabel("Baud Rate: ");
JLabel bytesize2 = new JLabel("Byte Size: ");
JLabel stopbits2 = new JLabel("Stop Bits: ");
JLabel flow2 = new JLabel("Flow Con..: ");
JLabel parity2 = new JLabel("Parity: ");
JLabel stoLabel2 = new JLabel("Send Time Out: ");
JLabel rtoLabel2 = new JLabel("Receive Time Out: ");
JLabel portLabel3 = new JLabel("Port: ");
JLabel vendor = new JLabel("Vendor ID: ");
JLabel product = new JLabel("Product ID: ");
JLabel stoLabel3 = new JLabel("Send Time Out: ");
JLabel rtoLabel3 = new JLabel("Receive Time Out: ");
JLabel amountLabel = new JLabel("Amount: ");
JLabel textLabel = new JLabel("Display Text: ");
//Make all necessary TextFields
JTextField card = new JTextField(10);
JTextField expDate = new JTextField(10);
JTextField cvv = new JTextField(10);
JTextField ip = new JTextField(10);
JTextField ct = new JTextField(10);
JTextField rto = new JTextField(10);
JTextField sto = new JTextField(10);
JTextField port = new JTextField(10);
JTextField sendto = new JTextField(10);
JTextField reto = new JTextField(10);
JTextField comport = new JTextField(10);
JTextField sendto2 = new JTextField(10);
JTextField reto2 = new JTextField(10);
JTextField comport2 = new JTextField(10);
JTextField vendorid = new JTextField(10);
JTextField productid = new JTextField(10);
JTextField sendtime = new JTextField(10);
JTextField receiveto = new JTextField(10);
JTextField amountbox = new JTextField(10);
JTextField textBox = new JTextField(10);
//Add components to the panels
final JPanel ethernetSettings = new JPanel();
ethernetSettings.setLayout(new GridLayout(6, 2));
ethernetSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
ethernetSettings.add(ipLabel);
ethernetSettings.add(ip);
ethernetSettings.add(connectLabel);
ethernetSettings.add(ct);
ethernetSettings.add(receiveLabel);
ethernetSettings.add(rto);
ethernetSettings.add(sendLabel);
ethernetSettings.add(sto);
ethernetSettings.add(portLabel);
ethernetSettings.add(port);
ethernetSettings.add(save);
ethernetSettings.add(ecancel);
final JPanel usbHIDSettings = new JPanel();
usbHIDSettings.setLayout(new GridLayout(5, 2));
usbHIDSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
usbHIDSettings.add(vendor);
usbHIDSettings.add(vendorid);
usbHIDSettings.add(product);
usbHIDSettings.add(productid);
usbHIDSettings.add(stoLabel3);
usbHIDSettings.add(sendtime);
usbHIDSettings.add(rtoLabel3);
usbHIDSettings.add(receiveto);
usbHIDSettings.add(save4);
usbHIDSettings.add(hcancel);
final JPanel usbCDCSettings = new JPanel();
usbCDCSettings.setLayout(new GridLayout(9, 2));
usbCDCSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
usbCDCSettings.add(baudrate2);
usbCDCSettings.add(baudlist);
usbCDCSettings.add(bytesize2);
usbCDCSettings.add(bytelist);
usbCDCSettings.add(stopbits2);
usbCDCSettings.add(stoplist);
usbCDCSettings.add(flow2);
usbCDCSettings.add(flowlist);
usbCDCSettings.add(parity2);
usbCDCSettings.add(paritylist);
usbCDCSettings.add(stoLabel2);
usbCDCSettings.add(sendto2);
usbCDCSettings.add(rtoLabel2);
usbCDCSettings.add(reto2);
usbCDCSettings.add(portLabel3);
usbCDCSettings.add(comport2);
usbCDCSettings.add(save3);
usbCDCSettings.add(gcancel);
final JPanel rsSettings = new JPanel();
rsSettings.setLayout(new GridLayout(9, 2));
rsSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
rsSettings.add(baudrate);
rsSettings.add(baudlist2);
rsSettings.add(bytesize);
rsSettings.add(bytelist2);
rsSettings.add(stopbits);
rsSettings.add(stoplist2);
rsSettings.add(flow);
rsSettings.add(flowlist2);
rsSettings.add(parity);
rsSettings.add(paritylist2);
rsSettings.add(stoLabel);
rsSettings.add(sendto);
rsSettings.add(rtoLabel);
rsSettings.add(reto);
rsSettings.add(portLabel2);
rsSettings.add(comport);
rsSettings.add(save2);
rsSettings.add(fcancel);
JRadioButton ethernet = new JRadioButton("Ethernet");
ethernet.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog esettings = new JDialog(frame);
esettings.setTitle("Ethernet Settings");
esettings.add(ethernetSettings);
esettings.setSize(400, 400);
esettings.pack();
esettings.setVisible(true);
}
});
JRadioButton rs = new JRadioButton("RS232");
rs.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog rsettings = new JDialog(frame);
rsettings.setTitle("RS232 Settings");
rsettings.add(rsSettings);
rsettings.pack();
rsettings.setVisible(true);
}
});
JRadioButton usbcdc = new JRadioButton("USB_CDC");
usbcdc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog usbc = new JDialog(frame);
usbc.setTitle("USB_CDC Settings");
usbc.add(usbCDCSettings);
usbc.setSize(400, 400);
usbc.pack();
usbc.setVisible(true);
}
});
JRadioButton usbhid = new JRadioButton("USB_HID");
usbhid.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog usbh = new JDialog(frame);
usbh.setTitle("USB_HID Settings");
usbh.add(usbHIDSettings);
usbh.setSize(400, 400);
usbh.pack();
usbh.setVisible(true);
}
});
final JPanel PortSettings = new JPanel();
PortSettings.setLayout(new GridLayout(3, 4));
PortSettings.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
PortSettings.add(ethernet);
PortSettings.add(rs);
PortSettings.add(usbcdc);
PortSettings.add(usbhid);
PortSettings.add(next);
PortSettings.add(bcancel);
final JPanel accountPanel = new JPanel();
accountPanel.setLayout(new GridLayout(4, 2));
accountPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
accountPanel.add(cardLabel);
accountPanel.add(card);
accountPanel.add(expLabel);
accountPanel.add(expDate);
accountPanel.add(cvvLabel);
accountPanel.add(cvv);
accountPanel.add(bok);
accountPanel.add(ccancel);
JRadioButton apprve = new JRadioButton("Approve");
JRadioButton decline = new JRadioButton("Decline");
final JPanel apprvordecl = new JPanel();
apprvordecl.setLayout(new GridLayout(3, 2));
apprvordecl.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
apprvordecl.add(apprve);
apprvordecl.add(decline);
apprvordecl.add(textLabel);
apprvordecl.add(textBox);
apprvordecl.add(aok);
apprvordecl.add(acancel);
final JPanel amountPanel = new JPanel();
amountPanel.setLayout(new GridLayout(2, 2));
amountPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
amountPanel.add(amountLabel);
amountPanel.add(amountbox);
amountPanel.add(cok);
amountPanel.add(dcancel);
JButton initialize = new JButton("Initialize");
JButton connect = new JButton("Connect");
JButton disconnect = new JButton("Disconnect");
JButton shutdown = new JButton("Shut Down");
JButton portsettings = new JButton("Port Settings");
portsettings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog port = new JDialog(frame);
port.setTitle("Port Settings");
port.setSize(400, 400);
port.add(PortSettings);
port.pack();
port.setVisible(true);
}
});
JButton online = new JButton("Go Online");
JButton offline = new JButton("Go Offline");
JButton status = new JButton("Status");
JButton reboot = new JButton("Reboot");
JButton account = new JButton("Account");
account.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog accountDialog = new JDialog(frame);
accountDialog.setTitle("Account");
accountDialog.setSize(400, 400);
accountDialog.add(accountPanel);
accountDialog.pack();
accountDialog.setVisible(true);
}
});
JButton amount = new JButton("Amount");
amount.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog amount2 = new JDialog(frame);
amount2.setTitle("Amount");
amount2.setSize(400, 400);
amount2.add(amountPanel);
amount2.pack();
amount2.setVisible(true);
}
});
JButton reset = new JButton("Reset");
JButton approvordecl = new JButton("Approve / Decline");
approvordecl.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog apprv = new JDialog(frame);
apprv.setTitle("Approve / Decline");
apprv.setSize(400, 400);
apprv.add(apprvordecl);
apprv.pack();
apprv.setVisible(true);
}
});
JButton test = new JButton("Test Button #1");
JButton testing = new JButton("Test Button #2");
JRadioButton button = new JRadioButton("Radio Button");
JRadioButton button2 = new JRadioButton("Radio Button");
JCheckBox checkbox = new JCheckBox("Check Box");
JCheckBox checkbox2 = new JCheckBox("Check Box");
ButtonGroup group = new ButtonGroup();
group.add(usbhid);
group.add(usbcdc);
group.add(ethernet);
group.add(rs);
ButtonGroup approvegroup = new ButtonGroup();
approvegroup.add(apprve);
approvegroup.add(decline);
JPanel testPanel = new JPanel();
testPanel.add(button);
testPanel.add(button2);
testPanel.add(checkbox2);
JPanel posPanel = new JPanel();
posPanel.add(test);
posPanel.add(testing);
posPanel.add(checkbox);
JPanel llpPanel = new JPanel();
llpPanel.add(online);
llpPanel.add(offline);
llpPanel.add(status);
llpPanel.add(reboot);
llpPanel.add(account);
llpPanel.add(amount);
llpPanel.add(reset);
llpPanel.add(approvordecl);
JPanel buttonPanel = new JPanel();
buttonPanel.add(initialize);
buttonPanel.add(connect);
buttonPanel.add(disconnect);
buttonPanel.add(shutdown);
buttonPanel.add(portsettings);
frame.add(buttonPanel);
frame.add(buttonPanel, BorderLayout.NORTH);
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("LLP", null, llpPanel, "Low Level Protocol");
tabbedPane.addTab("POS",null, posPanel, "Point Of Sale");
tabbedPane.addTab("Test", null, testPanel, "Test");
JPanel tabsPanel = new JPanel(new BorderLayout());
tabsPanel.add(tabbedPane);
frame.add(tabsPanel, BorderLayout.CENTER);
frame.pack();
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
go for JTextArea
A JTextArea is a multi-line area that displays plain text.
http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/JTextArea.html
For required layout refer Layout Manager by Oracle