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
Related
I'm creating a simple UI using GUI objects to input data but whenever I add the center panel to the contentPane, my JLabels disappear. Additionally, the JTextFields, JComboBoxs and JButtons don't respond to clicking or entering keystrokes. If I don't add the centerPanel, or add it and start the applet with width and height parameters of 1 and 1, everything works perfectly.
When I stretch out the screen, after adding the center panel to a normal run configuration, the objects will appear outside of the initial window. I've defined all of the objects as private instance variables before the listed code, so that isn't the issue. Please help, I'm baffled!
Here's my code:
public void begin() {
// creates the GUI Objects for the northPanel
northPanel = new JPanel();
northPanel.setLayout(new GridLayout(1, 4));
searchBox = new JTextField();
searchBoxLabel = new JLabel("Search ID #:");
search = new JButton("Search");
search.addActionListener(this);
northPanel.add(searchBoxLabel);
northPanel.add(searchBox);
northPanel.add(search);
// creates the GUI OBjects for the southPanel
southDivider = new JPanel();
southDivider.setLayout(new GridLayout(2, 1));
southPanel = new JPanel();
southPanel.setLayout(new GridLayout(1, 3));
enter = new JButton("Enter");
incrementInfo = new JButton("Increment ID");
setCurrentTimeDate = new JButton("Current Time/Date");
findRate = new JButton("Find Yield Rate");
findRate.addActionListener(this);
enter.addActionListener(this);
incrementInfo.addActionListener(this);
setCurrentTimeDate.addActionListener(this);
southPanel.add(findRate);
southPanel.add(setCurrentTimeDate);
southPanel.add(incrementInfo);
southPanel.add(enter);
messageLabel = new JLabel("Welcome to the Stringer Application");
southDivider.add(southPanel);
southDivider.add(messageLabel);
// create the GUI objects on the eastPanel
eastPanel = new JPanel();
eastPanel.setLayout(new GridLayout(5, 2));
cellType = new JComboBox();
cellType.addItem("Rect");
cellType.addItem("Cham");
cellTypeLabel = new JLabel("Cell Type:");
ecaCode = new JComboBox();
ecaCode.addItem("A");
ecaCode.addItem("B");
ecaCodeLabel = new JLabel("ECA Code:");
ecaSyringeNum = new JTextField();
ecaSyringeNumLabel = new JLabel("Eca Syringe #:");
passFail = new JComboBox();
passFail.addItem("Pass");
passFail.addItem("Fail");
passFailLabel = new JLabel("Pass/Fail:");
operator = new JTextField();
operatorLabel = new JLabel("Operator:");
cellType.addActionListener(this);
ecaCode.addActionListener(this);
passFail.addActionListener(this);
eastPanel.add(operatorLabel);
eastPanel.add(operator);
eastPanel.add(cellTypeLabel);
eastPanel.add(cellType);
eastPanel.add(ecaCodeLabel);
eastPanel.add(ecaCode);
eastPanel.add(ecaSyringeNumLabel);
eastPanel.add(ecaSyringeNum);
eastPanel.add(passFailLabel);
eastPanel.add(passFail);
// create the GUI objects on the westPanel
westPanel = new JPanel();
westPanel.setLayout(new GridLayout(6, 2));
yieldLabel = new JLabel("Current Yield:");
yieldValueLabel = new JLabel("Select Date/Times");
yieldAfterDate = new JTextField();
yieldAfterTime = new JTextField();
yieldBeforeDate = new JTextField();
yieldBeforeTime = new JTextField();
yieldAfterDateLabel = new JLabel("After Date:");
yieldAfterTimeLabel = new JLabel("After Time:");
yieldBeforeDateLabel = new JLabel("Before Date:");
yieldBeforeTimeLabel = new JLabel("Before Time:");
setBeforeToCurrentLabel = new JLabel("<html>'Set to Current' for <br> Current Date/Time</html>");
fillBeforeWithCurrent = new JButton("Set to Current");
fillBeforeWithCurrent.addActionListener(this);
westPanel.add(yieldLabel);
westPanel.add(yieldValueLabel);
westPanel.add(yieldAfterDateLabel);
westPanel.add(yieldAfterDate);
westPanel.add(yieldAfterTimeLabel);
westPanel.add(yieldAfterTime);
westPanel.add(yieldBeforeDateLabel);
westPanel.add(yieldBeforeDate);
westPanel.add(yieldBeforeTimeLabel);
westPanel.add(yieldBeforeTime);
westPanel.add(setBeforeToCurrentLabel);
westPanel.add(fillBeforeWithCurrent);
// create the GUI objects for the centerPanel
centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(3, 4));
date = new JTextField(getCurrentDate());
dateLabel = new JLabel("Date:");
time = new JTextField(getCurrentTime());
timeLabel = new JLabel("Time:");
stringID = new JTextField();
stringIDLabel = new JLabel("String ID:");
cellLot = new JTextField();
cellLotLabel = new JLabel("Cell Lot #:");
cellEff = new JTextField();
cellEffLabel = new JLabel("Cell Eff:");
comments = new JTextField();
commentsLabel = new JLabel("Comments:");
centerPanel.add(dateLabel);
centerPanel.add(date);
centerPanel.add(timeLabel);
centerPanel.add(time);
centerPanel.add(stringIDLabel);
centerPanel.add(stringID);
centerPanel.add(cellLotLabel);
centerPanel.add(cellLot);
centerPanel.add(cellEffLabel);
centerPanel.add(cellEff);
centerPanel.add(commentsLabel);
centerPanel.add(comments);
// add the panel's to the contentPane
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.add(northPanel, BorderLayout.NORTH);
contentPane.add(southDivider, BorderLayout.SOUTH);
contentPane.add(eastPanel, BorderLayout.EAST);
contentPane.add(westPanel, BorderLayout.WEST);
contentPane.validate();
}
I have tested your code and it is working with no problems.
Most likely you are adding components some where else to the container, and by default any added components goes to center, which cause the old center panel to be removed , and the the swing frame to draw dirty area.
EDIT:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Test2 extends JFrame {
public Test2() {
begin();
}
public void begin() {
// creates the GUI Objects for the northPanel
JPanel northPanel = new JPanel();
northPanel.setLayout(new GridLayout(1, 4));
JTextField searchBox = new JTextField();
JLabel searchBoxLabel = new JLabel("Search ID #:");
JButton search = new JButton("Search");
northPanel.add(searchBoxLabel);
northPanel.add(searchBox);
northPanel.add(search);
// creates the GUI OBjects for the southPanel
JPanel southDivider = new JPanel();
southDivider.setLayout(new GridLayout(2, 1));
JPanel southPanel = new JPanel();
southPanel.setLayout(new GridLayout(1, 3));
JButton enter = new JButton("Enter");
JButton incrementInfo = new JButton("Increment ID");
JButton setCurrentTimeDate = new JButton("Current Time/Date");
JButton findRate = new JButton("Find Yield Rate");
southPanel.add(findRate);
southPanel.add(setCurrentTimeDate);
southPanel.add(incrementInfo);
southPanel.add(enter);
JLabel messageLabel = new JLabel("Welcome to the Stringer Application");
southDivider.add(southPanel);
southDivider.add(messageLabel);
// create the GUI objects on the eastPanel
JPanel eastPanel = new JPanel();
eastPanel.setLayout(new GridLayout(5, 2));
JComboBox cellType = new JComboBox();
cellType.addItem("Rect");
cellType.addItem("Cham");
JLabel cellTypeLabel = new JLabel("Cell Type:");
JComboBox ecaCode = new JComboBox();
ecaCode.addItem("A");
ecaCode.addItem("B");
JLabel ecaCodeLabel = new JLabel("ECA Code:");
JTextField ecaSyringeNum = new JTextField();
JLabel ecaSyringeNumLabel = new JLabel("Eca Syringe #:");
JComboBox passFail = new JComboBox();
passFail.addItem("Pass");
passFail.addItem("Fail");
JLabel passFailLabel = new JLabel("Pass/Fail:");
JTextField operator = new JTextField();
JLabel operatorLabel = new JLabel("Operator:");
eastPanel.add(operatorLabel);
eastPanel.add(operator);
eastPanel.add(cellTypeLabel);
eastPanel.add(cellType);
eastPanel.add(ecaCodeLabel);
eastPanel.add(ecaCode);
eastPanel.add(ecaSyringeNumLabel);
eastPanel.add(ecaSyringeNum);
eastPanel.add(passFailLabel);
eastPanel.add(passFail);
// create the GUI objects on the westPanel
JPanel westPanel = new JPanel();
westPanel.setLayout(new GridLayout(6, 2));
JLabel yieldLabel = new JLabel("Current Yield:");
JLabel yieldValueLabel = new JLabel("Select Date/Times");
JTextField yieldAfterDate = new JTextField();
JTextField yieldAfterTime = new JTextField();
JTextField yieldBeforeDate = new JTextField();
JTextField yieldBeforeTime = new JTextField();
JLabel yieldAfterDateLabel = new JLabel("After Date:");
JLabel yieldAfterTimeLabel = new JLabel("After Time:");
JLabel yieldBeforeDateLabel = new JLabel("Before Date:");
JLabel yieldBeforeTimeLabel = new JLabel("Before Time:");
JLabel setBeforeToCurrentLabel = new JLabel("<html>'Set to Current' for <br> Current Date/Time</html>");
JButton fillBeforeWithCurrent = new JButton("Set to Current");
westPanel.add(yieldLabel);
westPanel.add(yieldValueLabel);
westPanel.add(yieldAfterDateLabel);
westPanel.add(yieldAfterDate);
westPanel.add(yieldAfterTimeLabel);
westPanel.add(yieldAfterTime);
westPanel.add(yieldBeforeDateLabel);
westPanel.add(yieldBeforeDate);
westPanel.add(yieldBeforeTimeLabel);
westPanel.add(yieldBeforeTime);
westPanel.add(setBeforeToCurrentLabel);
westPanel.add(fillBeforeWithCurrent);
// create the GUI objects for the centerPanel
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(3, 4));
JTextField date = new JTextField();
JLabel dateLabel = new JLabel("Date:");
JTextField time = new JTextField();
JLabel timeLabel = new JLabel("Time:");
JTextField stringID = new JTextField();
JLabel stringIDLabel = new JLabel("String ID:");
JTextField cellLot = new JTextField();
JLabel cellLotLabel = new JLabel("Cell Lot #:");
JTextField cellEff = new JTextField();
JLabel cellEffLabel = new JLabel("Cell Eff:");
JTextField comments = new JTextField();
JLabel commentsLabel = new JLabel("Comments:");
centerPanel.add(dateLabel);
centerPanel.add(date);
centerPanel.add(timeLabel);
centerPanel.add(time);
centerPanel.add(stringIDLabel);
centerPanel.add(stringID);
centerPanel.add(cellLotLabel);
centerPanel.add(cellLot);
centerPanel.add(cellEffLabel);
centerPanel.add(cellEff);
centerPanel.add(commentsLabel);
centerPanel.add(comments);
// add the panel's to the contentPane
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.add(northPanel, BorderLayout.NORTH);
contentPane.add(southDivider, BorderLayout.SOUTH);
contentPane.add(eastPanel, BorderLayout.EAST);
contentPane.add(westPanel, BorderLayout.WEST);
// contentPane.validate();
setSize(812, 514);
}
public static void main(String[] args) {
Test2 t=new Test2();
// t.begin();
t.setVisible(true);
}
}
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();
}
}
}
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 am working on a project for my Java class and cannot get my clear button to work. More specifically, I am having an issue implementing Action and ItemListeners. My understanding is that I need to use ActionListener for my clear button, but I need to use ItemListener for my ComboBoxes. I am very new to Java and this is an intro class. I haven't even begun to code the submit button and ComboBoxes and am a little overwhelmed. For now, I could use help figuring out why my clear function will not work. Any help is greatly appreciated. Thanks in advance.
Here is my updated code after suggestions:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class cousinsTree extends JApplet
{
Container Panel;
JButton submitButton;
JButton clearButton;
JTextField firstName;
JTextField lastName;
JTextField Address;
JTextField City;
JMenuBar menuBar;
JTextField Total;
JComboBox Service;
JComboBox howOften;
JComboBox numTrees;
LayoutManager setLayout;
String[] TreeList;
String[] numList;
String[] oftenList;
#Override
public void init()
{
Panel = getContentPane();
this.setLayout(new FlowLayout());
TreeList= new String[3];
TreeList [0] = "Trim";
TreeList [1] = "Chemical Spray";
TreeList [2] = "Injection";
numList = new String[3];
numList [0] = "0-5";
numList [1] = "6-10";
numList [2] = "11 >";
oftenList = new String[3];
oftenList [0] = "Monthly";
oftenList [1] = "Quarterly";
oftenList [2] = "Annually";
Panel.setBackground (Color.green);
submitButton = new JButton("Submit");
submitButton.setPreferredSize(new Dimension(100,30));
clearButton.addActionListener(new clrButton());
clearButton = new JButton("Clear");
clearButton.setPreferredSize(new Dimension(100,30));
firstName = new JTextField("", 10);
JLabel lblFirstName = new JLabel("First Name");
lastName = new JTextField("", 10);
JLabel lblLastName = new JLabel("Last Name");
Address = new JTextField("", 15);
JLabel lblAddress = new JLabel("Address");
City = new JTextField("Columbus", 10);
JLabel lblCity = new JLabel("City");
Total = new JTextField("", 10);
JLabel lblTotal = new JLabel("Total");
//Service = new TextField("Service (Trim, Chemical Spray, or Injection).", 20);
JLabel lblService = new JLabel("Service");
Service=new JComboBox(TreeList);
JLabel lblhowOften = new JLabel("How often?");
howOften = new JComboBox(oftenList);
JLabel lblnumTrees = new JLabel("Number of Trees");
numTrees = new JComboBox(numList);
/* Configuration */
//add items to panel
Panel.add(lblFirstName);
Panel.add(firstName);
Panel.add(lblLastName);
Panel.add(lastName);
Panel.add(lblAddress);
Panel.add(Address);
Panel.add(lblCity);
Panel.add(City);
Panel.add(lblnumTrees);
Panel.add(numTrees);
Panel.add(lblService);
Panel.add(Service);
Panel.add(lblhowOften);
Panel.add(howOften);
Panel.add(submitButton);
Panel.add(clearButton);
Panel.add(lblTotal);
Panel.add(Total);
this.setSize(new Dimension(375, 275));
this.setLocation(0,0);
Service.setSelectedIndex (1);
howOften.setSelectedIndex (1);
numTrees.setSelectedIndex (1);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menuFile = new JMenu("File", true);
menuFile.setMnemonic(KeyEvent.VK_F);
menuFile.setDisplayedMnemonicIndex(0);
menuBar.add(menuFile);
JMenu menuSave = new JMenu("Save", true);
menuSave.setMnemonic(KeyEvent.VK_S);
menuSave.setDisplayedMnemonicIndex(0);
menuBar.add(menuSave);
JMenu menuExit = new JMenu("Exit", true);
menuExit.setMnemonic(KeyEvent.VK_X);
menuExit.setDisplayedMnemonicIndex(0);
menuBar.add(menuExit);
}
class clrButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
// clearButton.addActionListener(this);
firstName.setText("");
lastName.setText("");
Address.setText("");
City.setText("");
}
}
class subButton implements ItemListener {
public void itemStateChanged(ItemEvent e) {
submitButton.addItemListener(this);
Service.addItemListener(this);
numTrees.addItemListener(this);
howOften.addItemListener(this);
}
}
}
I was able to get it to work...Thank you all for your help. I removed the class and it worked.
Here is the working code:
[Code]
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
public class cousinsTree extends JApplet implements ActionListener
{
Container Panel;
JButton submitButton;
JButton clearButton;
JTextField firstName;
JTextField lastName;
JTextField Address;
JTextField City;
JMenuBar menuBar;
JTextField Total;
JComboBox Service;
JComboBox howOften;
JComboBox numTrees;
LayoutManager setLayout;
String[] TreeList;
String[] numList;
String[] oftenList;
public void init()
{
Panel = getContentPane();
this.setLayout(new FlowLayout());
TreeList= new String[3];
TreeList [0] = "Trim";
TreeList [1] = "Chemical Spray";
TreeList [2] = "Injection";
numList = new String[3];
numList [0] = "0-5";
numList [1] = "6-10";
numList [2] = "11 >";
oftenList = new String[3];
oftenList [0] = "Monthly";
oftenList [1] = "Quarterly";
oftenList [2] = "Annually";
Panel.setBackground (Color.green);
submitButton = new JButton("Submit");
submitButton.setPreferredSize(new Dimension(100,30));
clearButton = new JButton("Clear");
clearButton.addActionListener(this);
clearButton.setPreferredSize(new Dimension(100,30));
firstName = new JTextField("", 10);
JLabel lblFirstName = new JLabel("First Name");
lastName = new JTextField("", 10);
JLabel lblLastName = new JLabel("Last Name");
Address = new JTextField("", 15);
JLabel lblAddress = new JLabel("Address");
City = new JTextField("Columbus", 10);
JLabel lblCity = new JLabel("City");
Total = new JTextField("", 10);
JLabel lblTotal = new JLabel("Total");
//Service = new TextField("Service (Trim, Chemical Spray, or Injection).", 20);
JLabel lblService = new JLabel("Service");
Service=new JComboBox(TreeList);
JLabel lblhowOften = new JLabel("How often?");
howOften = new JComboBox(oftenList);
JLabel lblnumTrees = new JLabel("Number of Trees");
numTrees = new JComboBox(numList);
/* Configuration */
//add items to panel
Panel.add(lblFirstName);
Panel.add(firstName);
Panel.add(lblLastName);
Panel.add(lastName);
Panel.add(lblAddress);
Panel.add(Address);
Panel.add(lblCity);
Panel.add(City);
Panel.add(lblnumTrees);
Panel.add(numTrees);
Panel.add(lblService);
Panel.add(Service);
Panel.add(lblhowOften);
Panel.add(howOften);
Panel.add(submitButton);
Panel.add(clearButton);
Panel.add(lblTotal);
Panel.add(Total);
this.setSize(new Dimension(375, 275));
this.setLocation(0,0);
Service.setSelectedIndex (1);
howOften.setSelectedIndex (1);
numTrees.setSelectedIndex (1);
JMenuBar menuBar = new JMenuBar();
setJMenuBar(menuBar);
JMenu menuFile = new JMenu("File", true);
menuFile.setMnemonic(KeyEvent.VK_F);
menuFile.setDisplayedMnemonicIndex(0);
menuBar.add(menuFile);
JMenu menuSave = new JMenu("Save", true);
menuSave.setMnemonic(KeyEvent.VK_S);
menuSave.setDisplayedMnemonicIndex(0);
menuBar.add(menuSave);
JMenu menuExit = new JMenu("Exit", true);
menuExit.setMnemonic(KeyEvent.VK_X);
menuExit.setDisplayedMnemonicIndex(0);
menuBar.add(menuExit);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == clearButton) {
firstName.setText("");
lastName.setText("");
Address.setText("");
City.setText("");
}
}
}
[/Code]
Add following line
clearButton.addActionListener(new clrButton());
class clrButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
// clearButton.addActionListener(this); Comment it
// if(e.getSource() == clearButton){-> this line don't need.
firstName.setText("");
lastName.setText("");
Address.setText("");
City.setText("");
}
}
You are doing good. But just place the following code
clearButton.addActionListener(new clrButton());
Before and outside of class clrButton
Also try placing these statements outside of class subButton
submitButton.addItemListener(new subButton());
Service.addItemListener(new anotherClass());
numTrees.addItemListener(new anotherClass());
howOften.addItemListener(new anotherClass());
You need to add actionlistener to your buttons. so, the action events will be propogated to the listener where you do your logic of the action. so, you have to addActionListener to your button
clearButton.addActionListener(new clrButton());
i.e in your init() method
clearButton = new JButton("Clear");
clearButton.setPreferredSize(new Dimension(100,30));
//add this below line to add action listener to the button
clearButton.addActionListener(new clrButton());
Onemore line to be removed is clearButton.addActionListener(this); from your action performed method
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");