What I am trying to do is change the right side of my GUI each time I press a button. First button shows a JLabel second button a JTextField. Expected outcome change in panels. Outcome is that when I press the buttons nothing happens.
package javaapplication37;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.*;
public class Gui extends JFrame {
JTextField f1;
JPanel b, p1, p2;
JPanel p3;
JLabel l1;
JButton b2, b1;
String a;
public Gui() {
a="Input here";
setSize(600,600);
l1=new JLabel("8a petuxei");
b = new JPanel();
p3 = new JPanel();
p1 = new JPanel();
p2 = new JPanel();
b.setLayout(new GridLayout(2, 1));
b1 = new JButton("Eleos");
b2 = new JButton("elpizw");
b.add(b1);
b.add(b2);
b.setSize(150,600);
p1.setSize(450,600);
add(b);
add(p1);
ActionListener pou = new Listener(p1);
b1.addActionListener(pou);
p2.add(l1);
f1=new JTextField(a);
a=f1.getText();
p3.add(f1);
}
public class Listener implements ActionListener {
JPanel k;
public Listener(JPanel k) {
this.k = k;
}
#Override
public void actionPerformed(ActionEvent e) {
k.remove(getContentPane());
k.add(p2);
}
}
public class l implements ActionListener {
JPanel k;
public l(JPanel k) {
this.k = k;
}
#Override
public void actionPerformed(ActionEvent e) {
k.remove(getContentPane());
k.add(p3);
}
}
}
You need to be using a CardLayout. If you need help ask me.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class Gui extends JFrame implements ActionListener {
private JPanel menu = new JPanel();
private CardLayout contentLayout = new CardLayout();
private JPanel content = new JPanel(contentLayout);
private java.util.List<Card> cardList = new ArrayList<>();
public Gui() {
int i;
for (i = 0; i < 10; i++) {
Card card;
if(i % 2 == 0) card = new TextAreaCard("Card " + i, "this is the content for card #" + i);
else card = new LabelCard("Card " + i, "content for Label Card #" + i);
JButton btn = new JButton(card.name);
menu.add(btn);
btn.addActionListener(this);
content.add(card, card.name);
cardList.add(card);
}
menu.setLayout(new GridLayout(i, 1));
add(menu, BorderLayout.WEST);
add(content, BorderLayout.CENTER);
}
#Override
public void actionPerformed(ActionEvent e) {
contentLayout.show(content, e.getActionCommand());
}
class Card extends JPanel{
final String name;
public Card(String name){
this.name = name;
}
}
class TextAreaCard extends Card implements ActionListener {
JTextArea textArea = new JTextArea();
JButton btn = new JButton("OK");
public TextAreaCard(String name, String text) {
super(name);
textArea.setText(text);
setLayout(new BorderLayout());
add(textArea, BorderLayout.CENTER);
add(btn, BorderLayout.SOUTH);
btn.addActionListener(this);
}
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(this, textArea.getText(), "click OK", JOptionPane.NO_OPTION);
}
}
class LabelCard extends Card{
JLabel label = new JLabel();
public LabelCard(String name, String text) {
super(name);
label.setText(text);
add(label);
}
}
public static void main(String [] args){
Gui gui = new Gui();
gui.setSize(600, 500);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
}
}
Related
I wanted to connect jcomp1 to the void class somma
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.event.*;
#SuppressWarnings({ "unused", "serial" })
public class Calcolatrice extends JPanel {
private JButton jcomp1;
public Calcolatrice() {
//construct components
jcomp1 = new JButton ("Somma");
}
void somma(){
String val1 = jcomp5.getText();
String val2 = jcomp6.getText();
String sum = val1 + val2;
System.out.println(sum);
}
And I tried with:
jcomp1.addActionListener(new ActionListener() {
public void somma(ActionEvent e) {
String val1 = jcomp5.getText();
String val2 = jcomp6.getText();
String sum = val1 + val2;
System.out.println(sum);
}
} );
But it doesn't seem to work...
Any ideas?
I just started coding and I thought this was an easy project but I'm already having troubles. For this reason could you explain as clear as possible please? Thank you.
This is an example of how to create a basic java swing frame with an interactive button.
public class Win extends JFrame implements ActionListener {
private JButton btn;
private JTextField tf;
private JTextField tf1;
private Label label;
private Panel panel;
public Win() {
btn = new JButton("Click");
tf = new JTextField(" ");
tf1 = new JTextField(" ");
label = new Label();
label.setPreferredSize(new Dimension(300,100));
panel = new Panel();
btn.addActionListener(this);
panel.add(btn);
panel.add(tf);
panel.add(tf1);
panel.add(label);
this.add(panel);
this.setSize(500, 500);
this.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
label.setText(tf.getText()+" "+tf1.getText());
System.out.println("clicked");
}
}
You just need to instanciate the win class inside your main method.
In this program, I am trying to select images from users using filechooser and then displaying those images. By default I added 2 images. When I add more images it doesn't display?
Below is my entire code
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.io.File;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
public class CLayout {
JFrame frame = new JFrame("CardLayout demo");
JPanel panelCont = new JPanel();
LoginView log = new LoginView();
JPanel Img = new ImageGallery();
CardLayout cl = new CardLayout();
public CLayout() {
panelCont.setLayout(cl);
log.setLayout(new BoxLayout(log, BoxLayout.PAGE_AXIS));
Img.setLayout(new BoxLayout(Img, BoxLayout.PAGE_AXIS));
panelCont.add(log, "1");
panelCont.add(Img, "2");
cl.show(panelCont, "1");
ActionListener loginListener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String userName = log.getUserName();
char[] password = log.getPassword();
String pass=new String(password);
if (LoginView.LOGIN.equals(e.getActionCommand())) {
if(userName.equals("imagegallery")&& pass.equals("12345"))
cl.show(panelCont, "2");
}
}
};
log.addActionListener(loginListener);
frame.add(panelCont);
frame.setSize(800,600);
frame.setTitle(" Image Gallery ");
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new CLayout();
}
});
}
}
class ImageGallery extends JPanel {
private ImageIcon myImage1 = new ImageIcon ("Chrysanthemum.jpg");
private ImageIcon myImage2 = new ImageIcon ("Desert.jpg");
JPanel ImageGallery = new JPanel();
private ImageIcon[] myImages =new ImageIcon[10];
private int curImageIndex=0;
private int count=0;
private int total=1;
public ImageGallery () {
ImageGallery.add(new JLabel (myImage1));
myImages[0]=myImage1;
myImages[1]=myImage2;
add(ImageGallery, BorderLayout.CENTER);
JButton PREVIOUS = new JButton ("Previous");
JButton NEXT = new JButton ("Next");
JDialog.setDefaultLookAndFeelDecorated(true);
JButton button = new JButton("Select File");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser fileChooser = new JFileChooser();
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
System.out.println(selectedFile.getName().toString());
ImageIcon tImage=new ImageIcon("selectedFile.getName().toString()");
System.out.println(myImages.length);
int n=2+count;
myImages[n]=tImage;
total=total+count+1;
count++;
System.out.println(total+" "+count);
}
}
});
JPanel Menu = new JPanel();
Menu.setLayout(new GridLayout(1,3));
add(Menu, BorderLayout.NORTH);
Menu.add(PREVIOUS);
Menu.add(NEXT);
Menu.add(button);
//register listener
PreviousButtonListener PreviousButton = new PreviousButtonListener ();
NextButtonListener NextButton = new NextButtonListener ();
//add listeners to corresponding componenets
PREVIOUS.addActionListener(PreviousButton);
NEXT.addActionListener(NextButton);
}
class PreviousButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(curImageIndex>0 && curImageIndex <=total) {
ImageGallery.remove(0);
curImageIndex=curImageIndex-1;
ImageIcon TheImage= myImages[curImageIndex];
ImageGallery.add(new JLabel (TheImage));
ImageGallery.validate();
ImageGallery.repaint();
} else {
ImageGallery.remove(0);
ImageGallery.add(new JLabel (myImage1));
curImageIndex=0;
ImageGallery.validate();
ImageGallery.repaint();
}
}
}
class NextButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(curImageIndex>=0 && curImageIndex <total){
ImageGallery.remove(0);
curImageIndex = curImageIndex + 1;
ImageIcon TheImage= myImages[curImageIndex];
ImageGallery.add(new JLabel (TheImage));
ImageGallery.validate();
ImageGallery.repaint();
} else {
ImageGallery.remove(0);
ImageGallery.add(new JLabel (myImages[total]));
curImageIndex=total ;
ImageGallery.validate();
ImageGallery.repaint();
}
}
}
}
class LoginView extends JPanel {
JLabel userLabel = new JLabel("User");
JTextField userText = new JTextField(20);
JLabel passwordLabel = new JLabel("Password");
JPasswordField passwordText = new JPasswordField(20);
private final JButton loginButton;
private final JButton registerButton;
public static final String LOGIN = "Login";
public static final String REGISTER = "Regster";
public LoginView() {
setLayout(new GridLayout(0, 2));
userLabel.setBounds(10, 10, 80, 25);
add(userLabel);
userText.setBounds(10, 10, 60, 25);
add(userText);
passwordLabel.setBounds(10, 40, 80, 25);
add(passwordLabel);
passwordText.setBounds(100, 40, 160, 25);
add(passwordText);
loginButton = new JButton("login");
loginButton.setActionCommand(LOGIN);
registerButton = new JButton("register");
registerButton.setActionCommand(REGISTER);
add(loginButton);
}
public void addActionListener(ActionListener listener) {
loginButton.addActionListener(listener);
registerButton.addActionListener(listener);
}
public String getUserName() {
return userText.getText();
}
public char[] getPassword() {
return passwordText.getPassword();
}
}
Problem is in this line
ImageIcon tImage=new ImageIcon("selectedFile.getName().toString()");
you need to remove double-quotes in order to read actual file name.
ImageIcon tImage=new ImageIcon(selectedFile.getName().toString());
Hope this helps.
I am trying to have the number the user inputs into the frame either multiply by 2 or divide by 3 depending on which button they decide to click. I am having an hard time with working out the logic to do this. I know this needs to take place in the actionperformed method.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Quiz4 extends JFrame ActionListener
{
// Global Variable Declarations
// Our list input fields
private JLabel valueLabel = new JLabel("Enter a value between 1 and 20: ");
private JTextField valueField = new JTextField(25);
// create action buttons
private JButton multiButton = new JButton("x2");
private JButton divideButton = new JButton("/3");
private JScrollPane displayScrollPane;
private JTextArea display = new JTextArea(10,5);
// input number
private BufferedReader infirst;
// output number
private NumberWriter outNum;
public Quiz4()
{
//super("List Difference Tool");
getContentPane().setLayout( new BorderLayout() );
// create our input panel
JPanel inputPanel = new JPanel(new GridLayout(1,1));
inputPanel.add(valueLabel);
inputPanel.add(valueField);
getContentPane().add(inputPanel,"Center");
// create and populate our diffPanel
JPanel diffPanel = new JPanel(new GridLayout(1,2,1,1));
diffPanel.add(multiButton);
diffPanel.add(divideButton);
getContentPane().add(diffPanel, "South");
//diffButton.addActionListener(this);
} // Quiz4()
public void actionPerformed(ActionEvent ae)
{
} // actionPerformed()
public static void main(String args[])
{
Quiz4 f = new Quiz4();
f.setSize(1200, 200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter()
{ // Quit the application
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
} // main()
} // end of class
Here's something simpler, but it essentially does what you want out of your program. I added an ActionListener to each of the buttons to handle what I want, which was to respond to what was typed into the textbox. I just attach the ActionListener to the button, and then in the actionPerformed method, I define what I want to happen.
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Quizx extends JFrame {
private JPanel panel;
private JTextField textfield;
private JLabel ansLabel;
public Quizx() {
panel = new JPanel(new FlowLayout());
this.getContentPane().add(panel);
addLabel();
addTextField();
addButtons();
addAnswerLabel();
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setTitle("Quiz 4");
this.setSize(220, 150);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setVisible(true);
}
private void addTextField() {
textfield = new JTextField();
textfield.setColumns(9);
panel.add(textfield);
}
private void addButtons() {
JButton multButton = new JButton("x2");
JButton divButton = new JButton("/3");
panel.add(multButton);
panel.add(divButton);
addMultListener(multButton);
addDivListener(divButton);
}
private void addLabel() {
JLabel valueLabel = new JLabel("Enter a value between 1 and 20: ");
panel.add(valueLabel);
}
private void addAnswerLabel() {
ansLabel = new JLabel();
panel.add(ansLabel);
}
private void addMultListener(JButton button) {
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
ansLabel.setText(String.valueOf(Integer.parseInt(textfield.getText().trim()) * 2));
}
});
}
private void addDivListener(JButton button) {
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
ansLabel.setText(String.valueOf(Double.parseDouble(textfield.getText().trim()) /3));
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Quizx();
}
});
}
}
Hope that helps.
this is my code, I just want to make a receipt that when you check the checkbox theres value in it and it prints the label with the value consecutively
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GUI extends JFrame implements ActionListener
{
private JLabel label;
private JCheckBox checkbox,checkbox2,checkbox3,checkbox4,checkbox5,
checkbox6,checkbox7,checkbox8,checkbox9,
checkbox10,checkbox11,checkbox12,checkbox13,checkbox14,checkbox15,
checkbox16,checkbox17,checkbox18,checkbox19,checkbox20;
private JButton button;
public GUI()
{
Container pane= getContentPane();
pane.setLayout(new GridLayout(15,1));
JPanel jp= new JPanel();
label= new JLabel("McDonald's");
checkbox = new JCheckBox("Cheese Burger");
checkbox2 = new JCheckBox("Big Mac");
checkbox3 = new JCheckBox("Big n' Tasty");
checkbox4 = new JCheckBox("McSpicy");
checkbox5 = new JCheckBox("Quarter Pounder with Cheese");
checkbox6 = new JCheckBox("Double Cheeseburger");
checkbox7 = new JCheckBox("McChicken Sandwich");
checkbox8 = new JCheckBox("Filet-O-Fish");
checkbox9 = new JCheckBox("Cheeseburger Deluxe");
checkbox10 = new JCheckBox("Burger Mcdo");
checkbox11 = new JCheckBox("Chicken Filet with Drinks");
checkbox12 = new JCheckBox("Spaghetti with Drinks");
checkbox13 = new JCheckBox("Hot Fudge Sundae ");
checkbox14 = new JCheckBox("Caramel Sundae");
checkbox15 = new JCheckBox("Large Mcdo French Fries");
checkbox16 = new JCheckBox("Chicken Nuggets with coke");
checkbox17 = new JCheckBox("Coke Float");
checkbox18 = new JCheckBox("Green Apple Float");
checkbox19 = new JCheckBox("Crispy Chicken with rice");
checkbox20 = new JCheckBox("Oreo Sundae");
button = new JButton("Order Now");
add(label);
pane.add(checkbox);
pane.add(checkbox2);
pane.add(checkbox3);
pane.add(checkbox4);
pane.add(checkbox5);
pane.add(checkbox6);
pane.add(checkbox7);
pane.add(checkbox8);
pane.add(checkbox9);
pane.add(checkbox10);
pane.add(checkbox11);
pane.add(checkbox12);
pane.add(checkbox13);
pane.add(checkbox14);
pane.add(checkbox15);
pane.add(checkbox16);
pane.add(checkbox17);
pane.add(checkbox18);
pane.add(checkbox19);
pane.add(checkbox20);
add(button);
add(jp, BorderLayout.SOUTH);
label.setHorizontalAlignment(JLabel.CENTER);
button.setSize(10,10);
button.addActionListener(this);
setSize(500,550);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String gulp = e.getActionCommand();
{
if ( gulp.equals("Order Now"))
{
new Receipt();
setVisible(true);
dispose();
}
}
}
public static void main(String[] args)
{
GUI r= new GUI();
}
}
here is my second code, this is the format of the receipt I want to achieve
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class Receipt extends JFrame
{
private JLabel label,label2,label3,label4,label5,
label6,label7,label8,label9,label10;;
public Receipt()
{
Container pane= getContentPane();
pane.setLayout(new GridLayout(13,1));
label= new JLabel("MC DONALD'S");
label2= new JLabel("McDonald's Tandang Sora");
label3= new JLabel("#22 Tandang Sora Corner, Commonwealth Avenue, Quezon City");
label4= new JLabel("Telephone# (02)86236");
label5= new JLabel("MACHINE SERIAL NUMBER: D123HJ01");
label6= new JLabel("Card Issuer: Sharina Tortoles");
label7= new JLabel("Account Number# 337163990");
label8= new JLabel("February 22, 2014 12:45");
label9= new JLabel("Thank You for choosing Mcdonald's");
label10= new JLabel("Please come again");
label.setHorizontalAlignment(JLabel.CENTER);
label2.setHorizontalAlignment(JLabel.CENTER);
label3.setHorizontalAlignment(JLabel.CENTER);
label4.setHorizontalAlignment(JLabel.CENTER);
label5.setHorizontalAlignment(JLabel.CENTER);
label6.setHorizontalAlignment(JLabel.CENTER);
label7.setHorizontalAlignment(JLabel.CENTER);
label8.setHorizontalAlignment(JLabel.CENTER);
label9.setHorizontalAlignment(JLabel.CENTER);
label10.setHorizontalAlignment(JLabel.CENTER);
pane.add(label, BorderLayout.NORTH);
pane.add(label2);
pane.add(label3);
pane.add(label4);
pane.add(label5);
pane.add(label6);
pane.add(label7);
pane.add(label8);
pane.add(label9);
pane.add(label10);
setSize(500,500);
setVisible(true);
}
public static void main(String[] args)
{
Receipt g= new Receipt();
}
}
Hope that it is clear to you what I want to happen with code
I guess EnumSet is what you are looking for:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
enum Receipt {
CheeseBurger("Cheese Burger"),
BigMac("Big Mac"),
BigNTasty("Big n' Tasty"),
McSpicy("McSpicy"),
QuarterPounderWithCheese("Quarter Pounder with Cheese"),
DoubleCheeseburger("Double Cheeseburger"),
McChickenSandwich("McChicken Sandwich");
//JCheckBox("Filet-O-Fish"),
//JCheckBox("Cheeseburger Deluxe"),
//JCheckBox("Burger Mcdo"),
//JCheckBox("Chicken Filet with Drinks"),
//JCheckBox("Spaghetti with Drinks"),
//JCheckBox("Hot Fudge Sundae "),
//JCheckBox("Caramel Sundae"),
//JCheckBox("Large Mcdo French Fries"),
//JCheckBox("Chicken Nuggets with coke"),
//JCheckBox("Coke Float"),
//JCheckBox("Green Apple Float"),
//JCheckBox("Crispy Chicken with rice"),
//JCheckBox("Oreo Sundae");
private final String str;
private Receipt(String str) {
this.str = str;
}
#Override public String toString() {
return str;
}
}
public class GUI2 {
private JButton button = new JButton(new AbstractAction("Order Now") {
#Override public void actionPerformed(ActionEvent e) {
EnumSet<Receipt> r = EnumSet.noneOf(Receipt.class);
for (ReceiptCheckBox c: list) {
if (c.isSelected()) {
r.add(c.getReceipt());
}
}
JOptionPane.showMessageDialog((JComponent) e.getSource(), r);
}
});
private List<ReceiptCheckBox> list = new ArrayList<>(Receipt.values().length);
public JComponent makeUI() {
JPanel p = new JPanel();
for (Receipt r: Receipt.values()) {
ReceiptCheckBox c = new ReceiptCheckBox(r);
list.add(c);
p.add(c);
}
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel("McDonald's"), BorderLayout.NORTH);
panel.add(button, BorderLayout.SOUTH);
panel.add(p);
return panel;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new GUI2().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class ReceiptCheckBox extends JCheckBox {
private final Receipt receipt;
public ReceiptCheckBox(Receipt receipt) {
super(receipt.toString());
this.receipt = receipt;
}
public Receipt getReceipt() {
return receipt;
}
}
I just created an applet
public class HomeApplet extends JApplet {
private static final long serialVersionUID = -7650916407386219367L;
//Called when this applet is loaded into the browser.
public void init() {
//Execute a job on the event-dispatching thread; creating this applet's GUI.
// setSize(400, 400);
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
private void createGUI() {
RconSection rconSection = new RconSection();
rconSection.setOpaque(true);
// CommandArea commandArea = new CommandArea();
// commandArea.setOpaque(true);
JTabbedPane tabbedPane = new JTabbedPane();
// tabbedPane.setSize(400, 400);
tabbedPane.addTab("Rcon Details", rconSection);
// tabbedPane.addTab("Commad Area", commandArea);
setContentPane(tabbedPane);
}
}
where the fisrt tab is:
package com.rcon;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import com.Bean.RconBean;
import com.util.Utility;
public class RconSection extends JPanel implements ActionListener{
/**
*
*/
private static final long serialVersionUID = -9021500288377975786L;
private static String TEST_COMMAND = "test";
private static String CLEAR_COMMAND = "clear";
private static JTextField ipText = new JTextField();
private static JTextField portText = new JTextField();
private static JTextField rPassText = new JTextField();
// private DynamicTree treePanel;
public RconSection() {
// super(new BorderLayout());
JLabel ip = new JLabel("IP");
JLabel port = new JLabel("Port");
JLabel rPass = new JLabel("Rcon Password");
JButton testButton = new JButton("Test");
testButton.setActionCommand(TEST_COMMAND);
testButton.addActionListener(this);
JButton clearButton = new JButton("Clear");
clearButton.setActionCommand(CLEAR_COMMAND);
clearButton.addActionListener(this);
JPanel panel = new JPanel(new GridLayout(3,2));
panel.add(ip);
panel.add(ipText);
panel.add(port);
panel.add(portText);
panel.add(rPass);
panel.add(rPassText);
JPanel panel1 = new JPanel(new GridLayout(1,3));
panel1.add(testButton);
panel1.add(clearButton);
add(panel);
add(panel1);
// add(panel, BorderLayout.NORTH);
// add(panel1, BorderLayout.SOUTH);
}
#Override
public void actionPerformed(ActionEvent arg0) {
if(arg0.getActionCommand().equals(TEST_COMMAND)){
String ip = ipText.getText().trim();
if(!Utility.checkIp(ip)){
ipText.requestFocusInWindow();
ipText.selectAll();
JOptionPane.showMessageDialog(this,"Invalid Ip!!!");
return;
}
String port = portText.getText().trim();
if(port.equals("") || !Utility.isIntNumber(port)){
portText.requestFocusInWindow();
portText.selectAll();
JOptionPane.showMessageDialog(this,"Invalid Port!!!");
return;
}
String pass = rPassText.getText().trim();
if(pass.equals("")){
rPassText.requestFocusInWindow();
rPassText.selectAll();
JOptionPane.showMessageDialog(this,"Enter Rcon Password!!!");
return;
}
RconBean rBean = RconBean.getBean();
rBean.setIp(ip);
rBean.setPassword(pass);
rBean.setPort(Integer.parseInt(port));
if(!Utility.testConnection()){
rPassText.requestFocusInWindow();
rPassText.selectAll();
JOptionPane.showMessageDialog(this,"Invalid Rcon!!!");
return;
}else{
JOptionPane.showMessageDialog(this,"Correct Rcon!!!");
return;
}
}
else if(arg0.getActionCommand().equals(CLEAR_COMMAND)){
ipText.setText("");
portText.setText("");
rPassText.setText("");
}
}
}
it appears as
is has cropped some data how to display it full and make the applet non resizable as well. i tried setSize(400, 400); but it didnt helped the inner area remains the same and outer boundaries increases
Here's another variation on your layout. Using #Andrew's tag-in-source method, it's easy to test from the command line:
$ /usr/bin/appletviewer HomeApplet.java
// <applet code='HomeApplet' width='400' height='200'></applet>
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class HomeApplet extends JApplet {
#Override
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
#Override
public void run() {
createGUI();
}
});
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
private void createGUI() {
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Rcon1", new RconSection());
tabbedPane.addTab("Rcon2", new RconSection());
this.add(tabbedPane);
}
private static class RconSection extends JPanel implements ActionListener {
private static final String TEST_COMMAND = "test";
private static final String CLEAR_COMMAND = "clear";
private JTextField ipText = new JTextField();
private JTextField portText = new JTextField();
private JTextField rPassText = new JTextField();
public RconSection() {
super(new BorderLayout());
JLabel ip = new JLabel("IP");
JLabel port = new JLabel("Port");
JLabel rPass = new JLabel("Rcon Password");
JButton testButton = new JButton("Test");
testButton.setActionCommand(TEST_COMMAND);
testButton.addActionListener(this);
JButton clearButton = new JButton("Clear");
clearButton.setActionCommand(CLEAR_COMMAND);
clearButton.addActionListener(this);
JPanel panel = new JPanel(new GridLayout(3, 2));
panel.add(ip);
panel.add(ipText);
panel.add(port);
panel.add(portText);
panel.add(rPass);
panel.add(rPassText);
JPanel buttons = new JPanel(); // default FlowLayout
buttons.add(testButton);
buttons.add(clearButton);
add(panel, BorderLayout.NORTH);
add(buttons, BorderLayout.SOUTH);
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println(e);
}
}
}
As I mentioned in a comment, this question is really about how to layout components in a container. This example presumes you wish to add the extra space to the text fields and labels. The size of the applet is set in the HTML.
200x130 200x150
/*
<applet
code='FixedSizeLayout'
width='200'
height='150'>
</applet>
*/
import java.awt.*;
import javax.swing.*;
public class FixedSizeLayout extends JApplet {
public void init() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
initGui();
}
});
}
private void initGui() {
JTabbedPane tb = new JTabbedPane();
tb.addTab("Rcon Details", new RconSection());
setContentPane(tb);
validate();
}
}
class RconSection extends JPanel {
private static String TEST_COMMAND = "test";
private static String CLEAR_COMMAND = "clear";
private static JTextField ipText = new JTextField();
private static JTextField portText = new JTextField();
private static JTextField rPassText = new JTextField();
public RconSection() {
super(new BorderLayout(3,3));
JLabel ip = new JLabel("IP");
JLabel port = new JLabel("Port");
JLabel rPass = new JLabel("Rcon Password");
JButton testButton = new JButton("Test");
testButton.setActionCommand(TEST_COMMAND);
JButton clearButton = new JButton("Clear");
clearButton.setActionCommand(CLEAR_COMMAND);
JPanel panel = new JPanel(new GridLayout(3,2));
panel.add(ip);
panel.add(ipText);
panel.add(port);
panel.add(portText);
panel.add(rPass);
panel.add(rPassText);
JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.CENTER,5,5));
panel1.add(testButton);
panel1.add(clearButton);
add(panel, BorderLayout.CENTER);
add(panel1, BorderLayout.SOUTH);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Container c = new RconSection();
JOptionPane.showMessageDialog(null, c);
}
});
}
}
Size of applet viewer does not depend on your code.
JApplet is not window, so in java code you can't write japplet dimensions. You have to change run settings. I don't know where exactly are in other ide's, but in Eclipse you can change dimensions in Project Properties -> Run/Debug settings -> click on your launch configurations file (for me there were only 1 - main class) -> edit -> Parameters. There you can choose width and height for your applet. save changes and you are good to go