No Components are showing up on JFrame GUI - java

I'm fairly new to Swing and GUIs, and so far, only the window will appear, but none of the components will be visible. What can I do about this? Is there something wrong with the visibility or is it with a container?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PhoneCaller
{
JButton button1;
JButton button2;
JButton button3;
JButton button4;
JButton button5;
JButton button6;
JButton button7;
JButton button8;
JButton button9;
JButton buttonDash;
JButton button0;
JButton dialButton;
String phoneNum = "";
public static void main (String[] args)
{
new PhoneCaller();
}
public PhoneCaller()
{
JFrame myFrame = new JFrame();
myFrame.setTitle("Dialer");
myFrame.setSize(200, 250);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel myPanel = new JPanel(new BorderLayout(10,10));
myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.Y_AXIS));
myFrame.add(myPanel);
JPanel myPanel2 = new JPanel();
myPanel2.setLayout(new FlowLayout());
JLabel lab = new JLabel("Enter the number to dial");
myPanel2.add(lab);
JPanel myPanel3 = new JPanel();
myPanel3.setLayout(new GridLayout(4,3,5,5));
button1 = new JButton ("1");
myPanel3.add(button1);
button2 = new JButton ("2");
myPanel3.add(button2);
button3 = new JButton ("3");
myPanel3.add(button3);
button4 = new JButton ("4");
button5 = new JButton ("5");
button6 = new JButton ("6");
button7 = new JButton ("7");
button8 = new JButton ("8");
button9 = new JButton ("9");
button0 = new JButton ("0");
buttonDash = new JButton ("-");
myPanel3.add(button4);
myPanel3.add(button5);
myPanel3.add(button6);
myPanel3.add(button7);
myPanel3.add(button8);
myPanel3.add(button9);
myPanel3.add(button0);
myPanel3.add(buttonDash);
myFrame.setVisible(true);
}
}

I think you forgot to add myPanel2 and myPanel3 inside myPanel.
myPanel.add(myPanel2);
myPanel.add(myPanel3);

You forgot to add myPanel2 and myPanel3 to your JFrame, add the following snippet to the end of your code
myFrame.add(myPanel2);
myFrame.add(myPanel3);

Related

How to separate a JTextField from the rest of the GridLayout

I'm trying to make a calculator as a fun project. But as I try to make it look like a... calculator, it just turns out like one big grid, as follows:
I've tried to follow along with whatever I found on the Internet, but that was a big bust.
Can anyone help me out trying to separate the JTextField so it doesn't do this, and it can be in it's own row?
Here's the code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame {
public Main() {
// Row = ->
// Column = ^
Container cp = getContentPane();
cp.setLayout(new GridLayout(3, 0));
JTextField txtCalc = new JTextField("0");
txtCalc.setHorizontalAlignment(SwingConstants.RIGHT);
JButton btn0 = new JButton("0");
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");
JButton btn10 = new JButton("10");
cp.add(txtCalc);
cp.add(btn0);
cp.add(btn1);
cp.add(btn2);
cp.add(btn3);
cp.add(btn4);
cp.add(btn5);
cp.add(btn6);
cp.add(btn7);
cp.add(btn8);
cp.add(btn9);
cp.add(btn10);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Calculator");
setSize(600, 600);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(Main::new);
}
}
Edit:
Now I have the answer! This is my fixed code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame {
public Main() {
Container cp = getContentPane();
cp.setLayout(new GridLayout(3, 0));
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel buttonPanel = new JPanel(new GridLayout(3, 0));
setContentPane(border);
JTextField txtCalc = new JTextField("0");
txtCalc.setHorizontalAlignment(SwingConstants.RIGHT);
txtCalc.setEditable(true);
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");
JButton btn0 = new JButton("0");
mainPanel.add(buttonPanel, BorderLayout.CENTER);
mainPanel.add(txtCalc, BorderLayout.NORTH);
buttonPanel.add(btn1);
buttonPanel.add(btn2);
buttonPanel.add(btn3);
buttonPanel.add(btn4);
buttonPanel.add(btn5);
buttonPanel.add(btn6);
buttonPanel.add(btn7);
buttonPanel.add(btn8);
buttonPanel.add(btn9);
buttonPanel.add(btn0);
public static void main(String[] args) {
SwingUtilities.invokeLater(Main::new);
}
}
Have your main panel use a BorderLayout. Put the JTextField in the North position. Put your buttons in a panel that uses GridLayout, then add that panel to your main panel in the Center position.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame {
public Main() {
JPanel mainPanel = new JPanel(new BorderLayout());
setContentPane(mainPanel);
JTextField txtCalc = new JTextField("0");
txtCalc.setHorizontalAlignment(SwingConstants.RIGHT);
JPanel buttonPanel = new JPanel(new GridLayout(3, 0));
JButton btn0 = new JButton("0");
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
JButton btn7 = new JButton("7");
JButton btn8 = new JButton("8");
JButton btn9 = new JButton("9");
buttonPanel.add(txtCalc);
buttonPanel.add(btn0);
buttonPanel.add(btn1);
buttonPanel.add(btn2);
buttonPanel.add(btn3);
buttonPanel.add(btn4);
buttonPanel.add(btn5);
buttonPanel.add(btn6);
buttonPanel.add(btn7);
buttonPanel.add(btn8);
buttonPanel.add(btn9);
mainPanel.add(txtCalc, BorderLayout.NORTH);
mainPanel.add(buttonPanel, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Calculator");
setSize(600, 600);
setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(Main::new);
}
}

How is it possible to add a container for border layout?

I want to add labels and buttons above and below the border layout. How can I do that? Here is what I did:
import java.awt.*;
import javax.swing.*;
class homework{
public static void main(String[] args) {
JFrame frame= new JFrame("border layout");
frame.setVisible(true);
JLabel label=new JLabel("Container of BorderLayout");
JButton button1 = new JButton("NORTH");
JButton button2 = new JButton("SOUTH");
JButton button3 = new JButton("EAST");
JButton button5 = new JButton("CENTER");
JButton button4 = new JButton("WEST");
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
frame.add(panel2);
label.setLayout(new FlowLayout(0));
panel2.add(label);
panel1.setLayout(new BorderLayout());
panel1.add(button1,BorderLayout.NORTH);
panel1.add(button2,BorderLayout.SOUTH);
panel1.add(button3,BorderLayout.EAST);
panel1.add(button4,BorderLayout.WEST);
panel1.add(button5,BorderLayout.CENTER);
frame.add(panel1);
frame.pack();
}
}
Above and below of border layout, set new 2 containers (for example JPanel) and make them flow layout. enter image description here
JPanel panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.NORTH);
JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton_1);
JPanel panel_1 = new JPanel();
frame.getContentPane().add(panel_1, BorderLayout.SOUTH);
JLabel lblNewLabel = new JLabel("New label");
panel_1.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("New label");
panel_1.add(lblNewLabel_1);
add something like that before frame.pack(); code.
Actually what do you mean by above and below? Do you mean north and south? If It is you should have something like this enter image description here
and you should write code this way
import java.awt.*;
class homework{
public static void main(String[] args) {
JFrame frame= new JFrame("border layout");
frame.setVisible(true);
JLabel label=new JLabel("Container of BorderLayout");
JButton button3 = new JButton("EAST");
JButton button5 = new JButton("CENTER");
JButton button4 = new JButton("WEST");
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
frame.getContentPane().add(panel2);
label.setLayout(new FlowLayout(0));
panel2.add(label);
panel1.setLayout(new BorderLayout());
panel1.add(button3,BorderLayout.EAST);
panel1.add(button4,BorderLayout.WEST);
panel1.add(button5,BorderLayout.CENTER);
frame.getContentPane().add(panel1);
JPanel panel = new JPanel();
panel1.add(panel, BorderLayout.NORTH);
JLabel lblNewLabel = new JLabel("New label");
panel.add(lblNewLabel);
JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
panel.add(rdbtnNewRadioButton);
JPanel panel_1 = new JPanel();
panel1.add(panel_1, BorderLayout.SOUTH);
JLabel lblNewLabel_1 = new JLabel("New label");
panel_1.add(lblNewLabel_1);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("New radio button");
panel_1.add(rdbtnNewRadioButton_1);
frame.pack();
}
}
In two conditions, you should add two containers into your code and make them flow layout.

Trying to layout JPanels. Java program layout changes every time I run

First time posting so go easy on me.
I am new to Java and am trying to get 3 JPanels to line up on top of each other. The first image is how I want it to look and it does sometimes when I run the program but as you can see by the other images it doesn't line up every time I run it. Sometimes not even showing some of the images/components.
So how can I get three JPanels to line up one after the other vertically?
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
public class FrameMain {
static final int MY_MINIMUM = 0;
static final int MY_MAXIMUM = 100;
public static void main(String[] args) {
JFrame frame1 = new JFrame("Harvest Frame Test");
frame1.setVisible(true);
frame1.setSize(800,700);
frame1.setResizable(false);
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Container Panel
JPanel container = new JPanel();
container.setSize(800,700);
container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
frame1.add(container);
//First Panel
JPanel panel1 = new JPanel();
panel1.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0
container.add(panel1);
JButton button1 = new JButton("Add Water");
panel1.add(button1);
JButton button2 = new JButton("Add Food");
panel1.add(button2);
JButton button3 = new JButton("Add Medicine");
panel1.add(button3);
ImageIcon image = new ImageIcon("C:/Users/Nick/Documents/EclipseArt/plant.gif");
JLabel imagelabel = new JLabel(image);
panel1.add(imagelabel);
JProgressBar pbar = new JProgressBar();
pbar.setMinimum(MY_MINIMUM);
pbar.setMaximum(MY_MAXIMUM);
// add to JPanel
panel1.add(pbar);
// Second Panel
JPanel panel2 = new JPanel();
panel2.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0
container.add(panel2);
JButton button4 = new JButton("Add Water");
panel2.add(button4);
JButton button5 = new JButton("Add Food");
panel2.add(button5);
JButton button6 = new JButton("Add Medicine");
panel2.add(button6);
ImageIcon image1 = new ImageIcon("C:/Users/Nick/Documents/EclipseArt/plant.gif");
JLabel imagelabel1 = new JLabel(image1);
panel2.add(imagelabel1);
JProgressBar pbar1 = new JProgressBar();
pbar1.setMinimum(MY_MINIMUM);
pbar1.setMaximum(MY_MAXIMUM);
// add to JPanel
panel2.add(pbar1);
// Third Panel
JPanel panel3 = new JPanel();
panel3.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0
container.add(panel3);
JButton button7 = new JButton("Add Water");
panel3.add(button7);
JButton button8 = new JButton("Add Food");
panel3.add(button8);
JButton button9 = new JButton("Add Medicine");
panel3.add(button9);
ImageIcon image2 = new ImageIcon("C:/Users/Nick/Documents/EclipseArt/plant.gif");
JLabel imagelabel2 = new JLabel(image2);
panel3.add(imagelabel2);
JProgressBar pbar2 = new JProgressBar();
pbar2.setMinimum(MY_MINIMUM);
pbar2.setMaximum(MY_MAXIMUM);
// add to JPanel
panel3.add(pbar2);
}
//static class Action implements ActionListener {
//public void actionPerformed (ActionEvent e){
//}
//}
}
Move the frame1.setVisible(true); all the way to the bottom. Changing Components on a frame that is already visible can cause issues.

Java swing, out of scope variable and usage of buttons

I am trying to make an actionlistener, for the buttons I have created and display each one a textfield much like a calculator upon each entry of a number.
However under the actionPerformed method, it seems like button1 and all of them seem to be out of scope, I can't declare a button static now can I?
I need to overcome the out of scope issue any takers?
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import layout.TableLayout;
public class example extends JFrame implements ActionListener
{
public static void main (String args[])
{
new example();
}
public example ()
{
super("The Power of Preferred Sizes");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
double size[][] = {{75, 75, 75, 75, 75}, // Columns
{75, 75, 75, 75, 75,75}}; // Rows
pane.setLayout(new TableLayout(size));
TableLayout layout = new TableLayout(size);
pane.setLayout (layout);
// Create all controls
String label[] = {"1", "2", "3", "4", "5","6","7", "8","9","0","PLS","RES"};
JButton button[] = new JButton[label.length];
JTextField upperText = new JTextField();
JTextField bottomText = new JTextField();
// textfieldName.setSize(10, 10);
pane.add(upperText, "0, 0, 4, 0");
pane.add(bottomText, "0, 5, 4, 0");
JButton button1 = new JButton(label[0]);
JButton button2 = new JButton(label[1]);
JButton button3 = new JButton(label[2]);
JButton button4 = new JButton(label[3]);
JButton button5 = new JButton(label[4]);
JButton button6 = new JButton(label[5]);
JButton button7 = new JButton(label[6]);
JButton button8 = new JButton(label[7]);
JButton button9 = new JButton(label[8]);
JButton button10 = new JButton(label[9]);
JButton button11= new JButton(label[10]);
JButton button12 = new JButton(label[11]);
pane.add(button1, "1,1");//1
pane.add(button2, "2,1");//2
pane.add(button3, "3,1");//3
pane.add(button4, "1,2");//
pane.add(button5, "2,2");
pane.add(button6, "3,2");
pane.add(button7, "1,3");
pane.add(button8, "2,3");
pane.add(button9, "3,3");
pane.add(button10, "1,4");
pane.add(button11, "2,4");
pane.add(button12, "3,4");
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
button10.addActionListener(this);
button11.addActionListener(this);
button12.addActionListener(this);
pack();
setResizable(false);
show();
}
public void actionPerformed(ActionEvent e) {
//code that reacts to the action...
Object source = e.getSource();
if(source == button1){
upperText.append("1");
}
}
}
You can declare them as class fields, so they will be accessible within the whole class:
public class example extends JFrame implements ActionListener {
JButton button1;
JButton button2;
JButton button3;
JButton button4;
JButton button5;
JButton button6;
JButton button7;
JButton button8;
JButton button9;
JButton button10:
JButton button11;
JButton button12;
// Constructor
// Methods
}
Also you should consider using an array to hold the 12 buttons.

Adding a JPanel to an ActionListener

I have created a JPanel that has all the JRadioButtons on it that I need (it is called PortSettings). I also have a button, called port settings, when the user clicks the button, I need the JPanel to come up and display the radio buttons. I have tried to add the JPanel to the actionlistener but it doesn't work. My code is below. I have deleted all other ActionListener's from the other buttons except for the portsettings buttons. If this question is confusing I'm sorry. It's really hard to explain what I need to do. I have uploaded a drawing of what the panel will look like as well as a screenshot of my program.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
public class TestApplication implements ActionListener {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(1000, 1000);
frame.setTitle("RBA Test Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JTextArea text = new JTextArea();
JLabel logLabel = new JLabel("Input/Output Log");
JRadioButton apprve = new JRadioButton("Approve");
JRadioButton decline = new JRadioButton("Decline");
JRadioButton ethernet = new JRadioButton("Ethernet");
JRadioButton rs = new JRadioButton("RS232");
JRadioButton usbcdc = new JRadioButton("USB_CDC");
JRadioButton usbhid = new JRadioButton("USB_HID");
JButton next = new JButton("Next");
JButton ok = new JButton("OK");
JButton cancel = new JButton("Cancel");
JPanel PortSettings = new JPanel();
PortSettings.add(ethernet);
PortSettings.add(rs);
PortSettings.add(usbcdc);
PortSettings.add(usbhid);
PortSettings.add(next);
PortSettings.add(cancel);
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) {
}
});
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");
JButton amount = new JButton("Amount");
JButton reset = new JButton("Reset");
JButton approvordecl = new JButton("Approve / Decline");
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");
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 textPanel = new JPanel(new BorderLayout());
textPanel.add(logLabel);
frame.add(logLabel);
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
}
}
I have tried to add a JFrame to the ActionListener then add the JPanel to the JFrame but nothing happens when I click the Port Settings button. Also, when I tried to add the JPanel to the JFrame it told me to put final in front of JPanel PortSettings = new JPanel();. Here is the code.
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
public class TestApplication implements ActionListener {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(1000, 1000);
frame.setTitle("RBA Test Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JTextArea text = new JTextArea();
JLabel logLabel = new JLabel("Input/Output Log");
JRadioButton apprve = new JRadioButton("Approve");
JRadioButton decline = new JRadioButton("Decline");
JRadioButton ethernet = new JRadioButton("Ethernet");
JRadioButton rs = new JRadioButton("RS232");
JRadioButton usbcdc = new JRadioButton("USB_CDC");
JRadioButton usbhid = new JRadioButton("USB_HID");
JButton next = new JButton("Next");
JButton ok = new JButton("OK");
JButton cancel = new JButton("Cancel");
final JPanel PortSettings = new JPanel();
PortSettings.add(ethernet);
PortSettings.add(rs);
PortSettings.add(usbcdc);
PortSettings.add(usbhid);
PortSettings.add(next);
PortSettings.add(cancel);
JPanel accountButton = new JPanel();
accountButton.add(ok);
accountButton.add(cancel);
JPanel apprvordecl = new JPanel();
apprvordecl.add(apprve);
apprvordecl.add(decline);
JPanel amountButton = new JPanel();
amountButton.add(ok);
amountButton.add(cancel);
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) {
JFrame port = new JFrame("Port Settings");
port.add(PortSettings);
frame.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");
JButton amount = new JButton("Amount");
JButton reset = new JButton("Reset");
JButton approvordecl = new JButton("Approve / Decline");
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");
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 textPanel = new JPanel(new BorderLayout());
textPanel.add(logLabel);
frame.add(logLabel);
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
}
}
You´re on the right track, but you do not want to add your PortSettings panel to a new JFrame but somewhere on your previously built one, assigned to the local variable frame. So your action listener should rather be
portsettings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.add(PortSettings, BorderLayout.SOUTH);
frame.pack();
}
});
(This is assuming that you actually want to add it to the frame at that instant and not add it invisibly right from the start and turn it visible, like #Aleksei suggested.)
The error message about final is because you use PortSettings in an (anonymous) inner class - viz., the ActionListener. In my proposed modification the same goes for frame, so you need to adapt its declaration as well:
final JFrame frame = new JFrame();
The reason why is quite technical and beside the point right now: just do it.
If instead you want the panel to appear in a separate window, you need a JDialog for that, not a second JFrame:
portsettings.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JDialog dialog = new JDialog(frame);
dialog.add(PortSettings);
dialog.pack();
dialog.setVisible(true);
}
});
Take a look at the JOptionPane class for a rich choice of ways to get more functionality out of dialogs.
Just add the action listener to all your buttons.
like this:
yourButton.addActionListener(this);
Do that for all the buttons.
Then take your TestPalication class's actionPreformed method and do whatever:
#Override
public void actionPerformed(ActionEvent arg0) {
((JRadioButton) arg0.getSource()).setTitle("Clicked!");
}
Your question was a little bit confusing but I hope this clarifies a little bit.

Categories