Hi I am working on an innovation in my company to handle the hits from the server so for that user has to add the service names in my application which is of monitoring application. So i have kept a button which will give the jtextfields counting to 9 so during submit button, how will i get the values from all the textboxes, below is my code,
package com.Lawrence;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Sample2 implements ActionListener
{
JFrame mainFrame;
static int count = 0;
static int width_textfield = 10;
static int height_textfield = 40;
static int height = 0;
JButton addTextField,submit;
JTextField virtualDirectories;
JLabel virtualDirectoriesName;
ArrayList<String> texts = new ArrayList<String>();
public Sample2()
{
mainFrame = new JFrame("Add Virtual Directory");
mainFrame.setSize(640,640);
mainFrame.setResizable(false);
mainFrame.setLayout(null);
addTextField = new JButton();
addTextField.setText("Add Virtual directory");
addTextField.setBounds(10, 10, 200, 25);
addTextField.addActionListener(this);
mainFrame.add(addTextField);
submit = new JButton();
submit.setText("Submit");
submit.setBounds(180, 560, 100, 25);
submit.addActionListener(this);
mainFrame.add(submit);
mainFrame.setVisible(true);
height = mainFrame.getHeight();
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()== "Add Virtual directory")
{
if(height_textfield <= height-80)
{
virtualDirectoriesName = new JLabel("Virtual Directory"+"\t"+":");
virtualDirectoriesName.setBounds(10,height_textfield,200, 25);
mainFrame.add(virtualDirectoriesName);
virtualDirectories = new JTextField();
virtualDirectories.setBounds(150,height_textfield,200,25);
mainFrame.add(virtualDirectories);
texts.add(virtualDirectories.getText());
count++;
//width_textfield++;
height_textfield = height_textfield+60;
mainFrame.revalidate();
mainFrame.repaint();
//http://www.dreamincode.net/forums/topic/381446-getting-the-values-from-mutiple-textfields-in-java-swing/
}
else
{
JOptionPane.showMessageDialog(mainFrame, "can only add"+count+"virtual Directories");
}
}
if(e.getActionCommand() == "Submit")
{
ArrayList<String> texts = new ArrayList<String>();
for(int i = 0; i< count;i++)
{
texts.add(virtualDirectories.getText());
}
System.out.println(texts.size());
System.out.println(texts.toString());
}
}
}
So i need to get the values from those textboxes and add it to an arraylist and then processing it to enter into my server for parsing the log files.So please explain me how to do it
You could store every textfield into an arraylist, just like you did with the texts. Also, please take a look at how to use layout managers.
ArrayList<JTextField> fields = new ArrayList<JTextField>();
fields.add(virtualDirectories);
for (int i = 0; i < count; i++) {
texts.add(fields.get(i).getText());
}
Edit:
This is a version of your code using layout managers. (plus the lines above, of course)
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Sample2 implements ActionListener {
JFrame mainFrame;
JPanel bottom;
JPanel center;
JPanel centerPanel1;
JPanel centerPanel2;
static int count = 0;
static int width_textfield = 10;
static int height_textfield = 40;
static int height = 0;
JButton addTextField, submit;
JTextField virtualDirectories;
JLabel virtualDirectoriesName;
ArrayList<JTextField> fields = new ArrayList<JTextField>();
ArrayList<String> texts = new ArrayList<String>();
int maxFields = 10;
public Sample2() {
mainFrame = new JFrame("Add Virtual Directory");
mainFrame.setSize(640, 640);
mainFrame.setResizable(false);
addTextField = new JButton();
addTextField.setText("Add Virtual directory");
addTextField.setBounds(10, 10, 200, 25);
addTextField.addActionListener(this);
submit = new JButton();
submit.setText("Submit");
submit.setBounds(180, 560, 100, 25);
submit.addActionListener(this);
center = new JPanel(new GridLayout(1, 2));
centerPanel1 = new JPanel(new GridLayout(maxFields, 1, 0, 20));
centerPanel2 = new JPanel();
center.add(centerPanel1);
center.add(centerPanel2);
bottom = new JPanel(new FlowLayout());
bottom.add(addTextField);
bottom.add(submit);
mainFrame.getContentPane().add(bottom, BorderLayout.SOUTH);
mainFrame.getContentPane().add(center, BorderLayout.CENTER);
mainFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand() == "Add Virtual directory") {
if (count < maxFields) {
JPanel p = new JPanel(new GridLayout(1, 2));
virtualDirectoriesName = new JLabel("Virtual Directory" + "\t" + ":");
virtualDirectories = new JTextField();
p.add(virtualDirectoriesName);
p.add(virtualDirectories);
centerPanel1.add(p);
texts.add(virtualDirectories.getText());
fields.add(virtualDirectories);
count++;
// width_textfield++;
height_textfield = height_textfield + 60;
mainFrame.revalidate();
mainFrame.repaint();
// http://www.dreamincode.net/forums/topic/381446-getting-the-values-from-mutiple-textfields-in-java-swing/
} else {
JOptionPane.showMessageDialog(mainFrame, "can only add " + maxFields + " virtual Directories");
}
}
if (e.getActionCommand() == "Submit") {
ArrayList<String> texts = new ArrayList<String>();
for (int i = 0; i < count; i++) {
texts.add(fields.get(i).getText());
}
System.out.println(texts.size());
System.out.println(texts.toString());
}
}
public static void main(String[] args) {
new Sample2();
}
}
Related
package SOS;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class BoardPanel {
private JFrame frame;
private void createAndDisplayGui() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createTopPanel(), BorderLayout.PAGE_START);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private void createBoard(ActionEvent event) {
Object source = event.getSource();
if (source instanceof JTextField) {
JTextField textField = (JTextField) source;
String text = textField.getText();
int dimension = Integer.parseInt(text);
JPanel board = new JPanel(new GridLayout(dimension, dimension));
for (int row = 0; row < dimension; row++) {
for (int col = 0; col < dimension; col++) {
JLabel square = new JLabel(" ");
square.setBackground(Color.white);
square.setOpaque(true);
square.setBorder(BorderFactory.createLineBorder(Color.black));
board.add(square);
}
}
frame.add(board, BorderLayout.CENTER);
frame.pack();
}
}
private JPanel createTopPanel() {
JRadioButton optionS = new JRadioButton("S");
JRadioButton optionO = new JRadioButton("O");
ButtonGroup group = new ButtonGroup();
group.add(optionS);
group.add(optionO);
JPanel topPanel = new JPanel();
JLabel label = new JLabel("Board size:");
topPanel.add(label);
JTextField boardSize = new JTextField(6);
boardSize.addActionListener(this::createBoard);
topPanel.add(boardSize);
topPanel.add(optionS, BorderLayout.NORTH);
topPanel.add(optionO, BorderLayout.CENTER);
return topPanel;
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new BoardPanel().createAndDisplayGui());
}
}
I know I am supposed to make an action listener for each radio button and that seems pretty straightforward, but I am confused on how I would do that and then translate that to placing an S or O on the game boards given cell depending on what is selected. I think the more confusing part is being able to draw in the given cell either the S or the O depending on what is selected. This is for an SOS game, sort of like tic tac toe. I tried following a simple tic tac toe example but got lost as there is no radio buttons like this and I am using a different createboard method.
You can declare your radioButton globally so you can check the selected status. Then in a listener in your cells check that and place the appropriate letter:
package SOS;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class BoardPanel {
private JFrame frame;
private JRadioButton optionS;
private JRadioButton optionO;
private void createAndDisplayGui() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(createTopPanel(), BorderLayout.PAGE_START);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private void createBoard(ActionEvent event) {
Object source = event.getSource();
if (source instanceof JTextField) {
JTextField textField = (JTextField) source;
String text = textField.getText();
int dimension = Integer.parseInt(text);
JPanel board = new JPanel(new GridLayout(dimension, dimension));
for (int row = 0; row < dimension; row++) {
for (int col = 0; col < dimension; col++) {
JLabel square = new JLabel(" ");
square.setBackground(Color.white);
square.setOpaque(true);
square.setBorder(BorderFactory.createLineBorder(Color.black));
board.add(square);
square.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
square.setText(optionS.isSelected() ? " S " : " O ");
}
});
}
}
frame.getContentPane().add(board, BorderLayout.CENTER);
frame.pack();
}
}
private JPanel createTopPanel() {
optionS = new JRadioButton("S");
optionO = new JRadioButton("O");
ButtonGroup group = new ButtonGroup();
group.add(optionS);
group.add(optionO);
JPanel topPanel = new JPanel();
JLabel label = new JLabel("Board size:");
topPanel.add(label);
JTextField boardSize = new JTextField(6);
boardSize.addActionListener(this::createBoard);
topPanel.add(boardSize);
topPanel.add(optionS, BorderLayout.NORTH);
topPanel.add(optionO, BorderLayout.CENTER);
return topPanel;
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new BoardPanel().createAndDisplayGui());
}
}
There are few ways you might be able to do this, personally, I like to decouple the workflows and remove dependencies where I can.
For example, your createBoard method is making an assumption about how the dimension value is captured by the user. What happens if you want to change that workflow to use a JSpinner or JCombobox? You'd then have to modify this method as well.
Better to pass the method the information it needs to do its job. In fact, I'd make it return an instance of JPanel, so as to remove all the dependencies, after all createBoard should do just that, nothing else.
I've changed the workflow so there is now a dedicated "create" button, this will collect the information it needs in order to be able to create the board itself. Not as dynamic as the other approach, but it gives the user time to consider their inputs
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class BoardPanel {
private JFrame frame;
enum State {
S, O;
}
private JPanel board;
private void createAndDisplayGui() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createTopPanel(), BorderLayout.PAGE_START);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createBoard(int dimension, State state) {
JPanel board = new JPanel(new GridLayout(dimension, dimension));
for (int row = 0; row < dimension; row++) {
for (int col = 0; col < dimension; col++) {
JLabel square = new JLabel(" " + state.name() + " ");
square.setBackground(Color.white);
square.setOpaque(true);
square.setBorder(BorderFactory.createLineBorder(Color.black));
board.add(square);
}
}
return board;
}
private JPanel createTopPanel() {
JRadioButton optionS = new JRadioButton("S");
JRadioButton optionO = new JRadioButton("O");
JTextField boardSize = new JTextField(6);
JPanel topPanel = new JPanel();
ButtonGroup group = new ButtonGroup();
group.add(optionS);
group.add(optionO);
// Default state
optionS.setSelected(true);
JButton createButton = new JButton("Make it so");
createButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
State state = State.O;
if (optionS.isSelected()) {
state = State.S;
}
try {
int dimension = Integer.parseInt(boardSize.getText());
if (board != null) {
frame.remove(board);
}
board = createBoard(dimension, state);
frame.add(board, BorderLayout.CENTER);
frame.pack();
} catch (NumberFormatException exp) {
JOptionPane.showMessageDialog(topPanel, "Invalid board size", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
JLabel label = new JLabel("Board size:");
topPanel.add(label);
topPanel.add(boardSize);
topPanel.add(optionS);
topPanel.add(optionO);
topPanel.add(createButton);
return topPanel;
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new BoardPanel().createAndDisplayGui());
}
}
If you'd prefer something a little more dynamic, then you could add shared ActionListener to the buttons and text field, for example...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class BoardPanel {
private JFrame frame;
enum State {
S, O;
}
private JPanel board;
private void createAndDisplayGui() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createTopPanel(), BorderLayout.PAGE_START);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createBoard(int dimension, State state) {
JPanel board = new JPanel(new GridLayout(dimension, dimension));
for (int row = 0; row < dimension; row++) {
for (int col = 0; col < dimension; col++) {
JLabel square = new JLabel(" " + state.name() + " ");
square.setBackground(Color.white);
square.setOpaque(true);
square.setBorder(BorderFactory.createLineBorder(Color.black));
board.add(square);
}
}
return board;
}
private JPanel createTopPanel() {
JRadioButton optionS = new JRadioButton("S");
JRadioButton optionO = new JRadioButton("O");
JTextField boardSize = new JTextField(6);
JPanel topPanel = new JPanel();
ButtonGroup group = new ButtonGroup();
group.add(optionS);
group.add(optionO);
ActionListener actionListener = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
State state = State.O;
if (optionS.isSelected()) {
state = State.S;
}
try {
int dimension = Integer.parseInt(boardSize.getText());
if (board != null) {
frame.remove(board);
}
board = createBoard(dimension, state);
frame.add(board, BorderLayout.CENTER);
frame.pack();
} catch (NumberFormatException exp) {
// Not yet ready
}
}
};
optionS.addActionListener(actionListener);
optionO.addActionListener(actionListener);
boardSize.addActionListener(actionListener);
// Default state
optionS.setSelected(true);
JLabel label = new JLabel("Board size:");
topPanel.add(label);
topPanel.add(boardSize);
topPanel.add(optionS);
topPanel.add(optionO);
return topPanel;
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> new BoardPanel().createAndDisplayGui());
}
}
I have code right now that takes user input to customize the the amount of rows and columns of a JTextArea, I want to use the same variables to make a 2d array with random values filling it, ranging from 0 to 2, I tried creating a nested for loop but ultimately failed. I can't find anything online that answers how to do this. Any help or advice appreciated. Below is all I have right now without anything to do this.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Main {
int tf1r = 0;
int tf2c = 0;
String space = ", ";
JPanel panel;
JLabel label;
JTextField tf1, tf2;
JButton set, reset;
JTextArea ta;
public static void main(String[] args) {
// Frame
JFrame frame = new JFrame("Mapped Array");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
frame.setResizable(false);
// Menu Bar
JMenuBar mb = new JMenuBar();
JMenu m1 = new JMenu("EXPORT");
JMenu m2 = new JMenu("HELP");
mb.add(m1);
mb.add(m2);
JMenuItem m11 = new JMenuItem("TXT");
JMenuItem m22 = new JMenuItem("BAT");
m1.add(m11);
m1.add(m22);
// Panel Bottom and Components
JPanel panel = new JPanel();
JLabel label = new JLabel("Enter Rows and Columns");
JTextField tf1 = new JTextField(10);
JTextField tf2 = new JTextField(10);
JButton set = new JButton("SET");
JButton reset = new JButton("RESET");
panel.add(label);
panel.add(tf1);
panel.add(tf2);
panel.add(set);
panel.add(reset);
// Center TextArea
JTextArea ta = new JTextArea();
// Components to Frame
frame.getContentPane().add(BorderLayout.SOUTH, panel);
frame.getContentPane().add(BorderLayout.NORTH, mb);
frame.getContentPane().add(BorderLayout.CENTER, ta);
frame.setVisible(true);
//Sets Text
set.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String space = ", ";
String rows = tf1.getText();
String columns = tf2.getText();
int tf1r = Integer.parseInt(rows);
int tf2c = Integer.parseInt(columns);
ta.setRows(tf1r);
ta.setColumns(tf2c);
System.out.println("rows: "+tf1r+" columns: "+tf2c+" TaR: "+ta.getRows()+" TaC: "+ta.getColumns());
ta.setText(rows+space+columns+"\n100, 100\n");
}
});
//Resets Text
reset.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
ta.setText("");
}
});
}
}
This should work:
tf1r = Integer.parseInt(rows);
tf2c = Integer.parseInt(columns);
Random random = new Random();
int[][] values = new int[tf1r][tf2c];
for(int i = 0; i < values.length; i++)
for(int j = 0; j < values[i].length; j++)
values[i][j] = random.nextInt(3);
Check this.
public void actionPerformed(ActionEvent e) {
String space = ", ";
String rows = tf1.getText();
String columns = tf2.getText();
int tf1r = Integer.parseInt(rows);
int tf2c = Integer.parseInt(columns);
ta.setRows(tf1r);
ta.setColumns(tf2c);
Random random = new Random();
int[][] randomArray = new int[tf1r][tf2c];
for (int i = 0; i < tf1r; i++) {
for (int j = 0; j < tf2c; j++) {
randomArray[i][j] = random.nextInt(3);
}
}
System.out.println("rows: "+tf1r+" columns: "+tf2c+" TaR: "+ta.getRows()+" TaC: "+ta.getColumns());
ta.setText(rows+space+columns+"\n100, 100\n");
}
I created an utility that will be used within the firewall zone to get Websphere MQ contents using Java with Swing, since I'm not sure where the defect lies, I've posted almost the entire code apart from the redundant part:
package testbox;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
#SuppressWarnings("serial")
public class MainFrame extends JFrame implements ActionListener
{
JLabel lblqname = new JLabel("Please enter the queue name");
JTextField txtqname = new JTextField(25);
JLabel lblqcur = new JLabel("where curdeth greater than");
JTextField txtqcurdfil = new JTextField(5);
JLabel lblchlname = new JLabel("Please enter the Channel name");
JTextField txtchlname = new JTextField(30);
JLabel lblchs = new JLabel("where status is: ");
JTextField txtchs = new JTextField(8);
public String ID;
public String pwdValue;
public String qname;
public int cdepth;
public String chlname;
public String chlstatus;
public String cmdissue;
JTextArea out = new JTextArea();
JButton QMGR1 = new JButton("QMGR1");
JButton QMGR2 = new JButton("QMGR2");
public MainFrame()
{
JLabel jUserName = new JLabel("ID");
JTextField userName = new JTextField();
JLabel jPassword = new JLabel("Password");
JTextField password = new JPasswordField();
Object[] ob = {jUserName, userName, jPassword, password};
int result = JOptionPane.showConfirmDialog(null, ob, "Please input password for Login", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION)
{
ID = userName.getText();
pwdValue = password.getText();
final JFrame frame = new JFrame("Environment Choice");
frame.setSize(500, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout(1, 1));
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.addTab("QAQmgrList", makeQAPanel());
frame.getContentPane().add(tabbedPane);
}
}
public JPanel makeQAPanel()
{
JPanel p = new JPanel();
p.setLayout(new GridBagLayout());
GridBagConstraints gbc_QMGR1 = new GridBagConstraints();
gbc_QMGR1.insets = new Insets(0, 0, 5, 5);
gbc_QMGR1.gridx = 1;
gbc_QMGR1.gridy = 1;
p.add(QMGR1, gbc_QMGR1);
QMGR1.addActionListener(this);
GridBagConstraints gbc_QMGR2 = new GridBagConstraints();
gbc_QMGR2.insets = new Insets(0, 0, 5, 5);
gbc_QMGR2.gridx = 1;
gbc_QMGR2.gridy = 2;
p.add(QMGR2, gbc_QMGR2);
QMGR2.addActionListener(this);
return p;
}
public void createSubframe()
{
final JFrame subframe = new JFrame("Object Choice");
subframe.setSize(1000, 500);
subframe.getContentPane().setLayout(new GridLayout(1, 1));
out.setText(null);
out.setLineWrap(true);
out.setCaretPosition(out.getDocument().getLength());
out.setEditable (false);
JScrollPane jp = new JScrollPane(out);
jp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
JPanel queue = new JPanel();
queue.add(lblqname);
txtqname.setText(null);
queue.add(txtqname);
queue.add(lblqcur);
txtqcurdfil.setText(null);
queue.add(txtqcurdfil);
txtqname.addActionListener(this);
txtqcurdfil.addActionListener(this);
JPanel chl = new JPanel();
chl.add(lblchlname);
txtchlname.setText(null);
chl.add(txtchlname);
chl.add(lblchs);
txtchs.setText(null);
chl.add(txtchs);
txtchlname.addActionListener(this);
txtchs.addActionListener(this);
tabbedPane.addTab("Queues", queue);
tabbedPane.addTab("Channels", chl);
subframe.getContentPane().add(tabbedPane);
subframe.getContentPane().add(jp);
tabbedPane.setVisible(true);
subframe.setVisible(true);
}
public static void main(String[] args)
{SwingUtilities.invokeLater(new Runnable(){ public void run() { #SuppressWarnings("unused") MainFrame m = new MainFrame();}});}
#Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == QMGR1|| e.getSource() == QMGR2)
{createSubframe();}
if (e.getSource() == txtqname){qname = txtqname.getText();}
if (e.getSource() == txtqcurdfil)
{
cdepth = Integer.parseInt(txtqcurdfil.getText());
cmdissue = "qn has value messages";
cmdissue = cmdissue.replace("qn", ""+qname+"");
cmdissue = cmdissue.replace("value", ""+cdepth+"");
System.out.println(cmdissue);
cmdissue = null;
}
if (e.getSource() == txtchlname){chlname = txtchlname.getText(); chlname=null;}
if (e.getSource() == txtchs)
{
chlstatus = txtchs.getText();
cmdissue = "chln is chls";
cmdissue = cmdissue.replace("chln", ""+chlname+"");
cmdissue = cmdissue.replace("chls", ""+chlstatus+"");
System.out.println(cmdissue);
}
}
}
I'm getting the expected outcome for the code:
Running for first time
Say I close this object choice panel and open a new instance, irrespective of which choice I make the command runs twice:
Running for the second time
The iteration repeats. Say I make a choice for the fourth or fifth time, the command runs 4/5 times.
I understood the fact that the somehow it is initializing the objects for the number of times I run it, and it needs to be reset after I close the panel. But I'm not sure how/where to get this done.
Apologies for the lengthy code posted, since I wanted to be sure that people can point the mistake that has been committed.
GridBagConstraints gbc_QMGR1 = new GridBagConstraints();
gbc_QMGR1.insets = new Insets(0, 0, 5, 5);
gbc_QMGR1.gridx = 1;
gbc_QMGR1.gridy = 1;
p.add(QMGR1, gbc_QMGR1);
QMGR1.addActionListener(this);
GridBagConstraints gbc_QMGR1 = new GridBagConstraints();
gbc_QMGR1.insets = new Insets(0, 0, 5, 5);
gbc_QMGR1.gridx = 1;
gbc_QMGR1.gridy = 2;
p.add(QMGR1, gbc_QMGR1);
QMGR1.addActionListener(this);
Looks like you are trying to add the same component to the panel twice in two different grid locations. You can't do this.
You need to:
create two different component, or
get rid of one of the components.
Edit:
JButton QMGR1 = new JButton("QMGR1");
JButton QMGR2 = new JButton("QMGR2");
You create the buttons as instance variables.
But then in the makeQAPanel() method you add the actionListener to the button.
QMGR1.addActionListener(this);
...
QMGR2.addActionListener(this);
So every time you invoke that method the actionListener gets added again.
The actionListener should be added in the constructor of you class so it is only added once.
#jesper,
someone explained to me the constructor part of it:
package testbox;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
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.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
#SuppressWarnings("serial")
public class MainFrame extends JFrame implements ActionListener
{
JLabel lblqname = new JLabel("Please enter the queue name");
JTextField txtqname = new JTextField(25);
JLabel lblqcur = new JLabel("where curdeth greater than");
JTextField txtqcurdfil = new JTextField(5);
JLabel lblchlname = new JLabel("Please enter the Channel name");
JTextField txtchlname = new JTextField(30);
JLabel lblchs = new JLabel("where status is: ");
JTextField txtchs = new JTextField(8);
public String ID;
public String pwdValue;
public String qname;
public int cdepth;
public String chlname;
public String chlstatus;
public String cmdissue;
JTextArea out = new JTextArea();
JButton QMGR1 = new JButton("QMGR1");
JButton QMGR2 = new JButton("QMGR2");
public MainFrame()
{
QMGR1.addActionListener(this);
QMGR2.addActionListener(this);
txtqname.addActionListener(this);
txtqcurdfil.addActionListener(this);
txtchlname.addActionListener(this);
txtchs.addActionListener(this);
JLabel jUserName = new JLabel("ID");
JTextField userName = new JTextField();
JLabel jPassword = new JLabel("Password");
JTextField password = new JPasswordField();
Object[] ob = {jUserName, userName, jPassword, password};
int result = JOptionPane.showConfirmDialog(null, ob, "Please input password for Login", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION)
{
ID = userName.getText();
pwdValue = password.getText();
final JFrame frame = new JFrame("Environment Choice");
frame.setSize(500, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout(1, 1));
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.addTab("QAQmgrList", makeQAPanel());
frame.getContentPane().add(tabbedPane);
}
}
public JPanel makeQAPanel()
{
JPanel p = new JPanel();
p.setLayout(new GridBagLayout());
GridBagConstraints gbc_QMGR1 = new GridBagConstraints();
gbc_QMGR1.insets = new Insets(0, 0, 5, 5);
gbc_QMGR1.gridx = 1;
gbc_QMGR1.gridy = 1;
p.add(QMGR1, gbc_QMGR1);
GridBagConstraints gbc_QMGR2 = new GridBagConstraints();
gbc_QMGR2.insets = new Insets(0, 0, 5, 5);
gbc_QMGR2.gridx = 1;
gbc_QMGR2.gridy = 2;
p.add(QMGR2, gbc_QMGR2);
return p;
}
public void createSubframe()
{
final JFrame subframe = new JFrame("Object Choice");
subframe.setSize(1000, 500);
subframe.getContentPane().setLayout(new GridLayout(1, 1));
out.setText(null);
out.setLineWrap(true);
out.setCaretPosition(out.getDocument().getLength());
out.setEditable (false);
JScrollPane jp = new JScrollPane(out);
jp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
JPanel queue = new JPanel();
queue.add(lblqname);
txtqname.setText(null);
queue.add(txtqname);
queue.add(lblqcur);
txtqcurdfil.setText(null);
queue.add(txtqcurdfil);
JPanel chl = new JPanel();
chl.add(lblchlname);
txtchlname.setText(null);
chl.add(txtchlname);
chl.add(lblchs);
txtchs.setText(null);
chl.add(txtchs);
tabbedPane.addTab("Queues", queue);
tabbedPane.addTab("Channels", chl);
subframe.getContentPane().add(tabbedPane);
subframe.getContentPane().add(jp);
tabbedPane.setVisible(true);
subframe.setVisible(true);
}
public static void main(String[] args)
{SwingUtilities.invokeLater(new Runnable(){ public void run() { #SuppressWarnings("unused") MainFrame m = new MainFrame();}});}
#Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == QMGR1|| e.getSource() == QMGR2)
{createSubframe();}
if (e.getSource() == txtqname){qname = txtqname.getText();}
if (e.getSource() == txtqcurdfil)
{
cdepth = Integer.parseInt(txtqcurdfil.getText());
cmdissue = "qn has value messages";
cmdissue = cmdissue.replace("qn", ""+qname+"");
cmdissue = cmdissue.replace("value", ""+cdepth+"");
System.out.println(cmdissue);
cmdissue = null;
}
if (e.getSource() == txtchlname){chlname = txtchlname.getText(); chlname=null;}
if (e.getSource() == txtchs)
{
chlstatus = txtchs.getText();
cmdissue = "chln is chls";
cmdissue = cmdissue.replace("chln", ""+chlname+"");
cmdissue = cmdissue.replace("chls", ""+chlstatus+"");
System.out.println(cmdissue);
}
}
}
Worked like a charm.. THanks for pointing it out
Hello and thanks for your assistance in advance.
My goal is to load a directory called resource to create an array. After doing that the program should use a "Next" and "Previous" button to cycle both forwards and backwards through the array by using a counter. Cycling through the array should display the appropriate picture (without knowing what the picture is, just the order that it is in the array).
My code this far:
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import java.io.IOException;
import javax.swing.ImageIcon;
import java.io.*;
import java.util.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.awt.*;
import java.awt.event.*;
public class viewer extends JFrame implements ActionListener
{
int counter = 0; //Initial counter value
int maxItems = 0; // Set it equal to the length of the array
int minItems = 0; //Minimum items for previous button
JPanel botPanel = null;
JPanel midPanel = null;
JLabel midLabel = null;
ImageIcon image;
String[] picture = new String [1000];
public viewer()
{
setTitle ("Roberts Viewer");
setSize (1000, 1000);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Allow the X button to close the program and not just the frame
JPanel midPanel = new JPanel ();
JPanel botPanel = new JPanel ();
midLabel = new JLabel();
JButton prv = new JButton("Previous"); botPanel.add(prv); prv.addActionListener(this);
JButton nxt = new JButton("Next"); botPanel.add(nxt); nxt.addActionListener(this);
JButton quitButton = new JButton ("Quit Program"); botPanel.add(quitButton); quitButton.addActionListener(this);
add(midPanel, BorderLayout.CENTER);
add(botPanel, BorderLayout.SOUTH);
File dir = new File ("resource");
File[] picture = dir.listFiles();
for (int maxItems = 0;maxItems < picture.length; maxItems++);
midPanel.add(midLabel);
setVisible(true);
}
public static void main(String[] args)
{
viewer v = new viewer();
}
public void actionPerformed (ActionEvent e)
{
String action = e.getActionCommand();
if (action.equals("Quit Program"))
{
System.exit(0);
}
if(action.equals("Next"))
{
if (counter < 0){counter = 0;}
if (counter> maxItems) {counter = 0;}
String pictureString = String.format("resource/%s", picture[counter]);
System.out.printf("Retrieving[%s]\n",pictureString);
try
{
image = new ImageIcon(getClass().getClassLoader().getResource(pictureString));
}
catch (Exception xu)
{
System.out.println("Woops");
}
midLabel.setIcon(image);
counter++;
}
if(action.equals("Previous"))
{
if (counter < 0) {counter = maxItems;}
if (counter < minItems) {counter = maxItems;}
String pictureString = String.format("resource/%s",picture[counter]);
System.out.printf("Retrieving[%s]\n", pictureString);
try
{
image = new ImageIcon(getClass().getClassLoader().getResource(pictureString));
}
catch (Exception xu)
{
System.out.println("Woops");
}
midLabel.setIcon(image);
counter--;
}
}
}
The problem was solved. I had some bugs but here is the fixed code.
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import java.io.IOException;
import javax.swing.ImageIcon;
import java.io.*;
import java.util.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.awt.*;
import java.awt.event.*;
public class viewer extends JFrame implements ActionListener
{
int counter = 0; //Initial counter value
int maxItems = 0; // Set it equal to the length of the array
int minItems = 0; //Minimum items for previous button
JPanel botPanel = null;
JPanel midPanel = null;
JLabel midLabel = null;
ImageIcon image;
File[] picture;
public viewer()
{
setTitle ("Roberts Viewer");
setSize (500, 500);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel midPanel = new JPanel ();
JPanel botPanel = new JPanel ();
midLabel = new JLabel();
JButton prv = new JButton("Previous"); botPanel.add(prv); prv.addActionListener(this);
JButton nxt = new JButton("Next"); botPanel.add(nxt); nxt.addActionListener(this);
JButton quitButton = new JButton ("Quit Program"); botPanel.add(quitButton); quitButton.addActionListener(this);
add(midPanel, BorderLayout.CENTER);
add(botPanel, BorderLayout.SOUTH);
File dir = new File ("resource");
picture = dir.listFiles();
midPanel.add(midLabel);
image = new ImageIcon("resource/"+picture [0].getName());
midLabel.setIcon(image);
setVisible(true);
}
public static void main(String[] args)
{
viewer v = new viewer();
}
public void actionPerformed (ActionEvent e)
{
String action = e.getActionCommand();
if (action.equals("Quit Program"))
{
System.exit(0);
}
if(action.equals("Next"))
{
counter++;
if (counter>= picture.length) {counter = 0;}
try
{
image = new ImageIcon("resource/"+picture[counter].getName());
midLabel.setIcon(image);
}
catch (Exception xu)
{
System.out.println("Woops");
}
System.out.printf("Exit next: " + counter + "\n");
}
if(action.equals("Previous"))
{
counter--;
if (counter < 0) {counter = picture.length-1;}
String.format("resource/%s",picture[counter].getName());
try
{
image = new ImageIcon("resource/"+picture[counter].getName());
midLabel.setIcon(image);
}
catch (Exception xu)
{
System.out.println("Woops");
}
System.out.printf("Exit prev: " + counter + "\n");
}
}
}
I got 5 JTextareas that stand for output for the sorting I need help regarding displaying or appending iteration in each JTextArea to show the algorithm of sorting.
I have got values of { 5,3,9,7,1,8 }
JTextArea1 |3, 5, 7, 1, 8, 9|
JTextArea2 |3, 5, 1, 7, 8, 9|
JTextArea3 |3, 5, 1, 7, 8, 9|
JTextArea4 |1, 3, 5, 7, 8, 9|
JTextArea5 |1, 3, 5, 7, 8, 9|
My problem is how can I append those values in each textarea.
My code is too long, I'm sorry for that.
My code is not finished yet. I just ended on the button of ascbubble but it can run.
//importing needed packages
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Sorting extends JPanel
{
// String needed to contain the values then convert into int
int int1,int2,int3,int4,int5,temp;
String str1,str2,str3,str4,str5;
//Buttons needed
JButton ascbubble,descbubble,
ascballoon,descballoon,
clear;
//Output Area of the result sorting
JTextArea output1,output2,
output3,output4,
output5;
//Text Field for user to input numbers
JTextField input1,input2,
input3,input4,
input5;
Sorting()
{
//set color of the background
setBackground(Color.black);
//initialize JButton,JTextArea and JTextField
//JButton
ascbubble = new JButton("Ascending Bubble");
descbubble = new JButton("Descending Bubble");
ascballoon = new JButton("Ascending Balloon");
descballoon = new JButton("Descending Balloon");
clear = new JButton("Clear");
//JTextArea
output1 = new JTextArea(" ");
output2 = new JTextArea(" ");
output3 = new JTextArea(" ");
output4 = new JTextArea(" ");
output5 = new JTextArea(" ");
//JTextField
input1 = new JTextField("00");
input2 = new JTextField("00");
input3 = new JTextField("00");
input4 = new JTextField("00");
input5 = new JTextField("00");
//SetBounds
setLayout(null);
input1.setBounds(120, 50, 30, 20);
input2.setBounds(170, 50, 30, 20);
input3.setBounds(220, 50, 30, 20);
input4.setBounds(270, 50, 30, 20);
input5.setBounds(320, 50, 30, 20);
ascbubble.setBounds(50, 120, 150, 40);
descbubble.setBounds(50, 180, 150, 40);
clear.setBounds(210, 140, 100, 50);
ascballoon.setBounds(320, 120, 150, 40);
descballoon.setBounds(320, 180, 150, 40);
output1.setBounds(20, 300, 80, 100);
output2.setBounds(120, 300, 80, 100);
output3.setBounds(220, 300, 80, 100);
output4.setBounds(320, 300, 80, 100);
output5.setBounds(420, 300, 80, 100);
//add function to the buttons
thehandler handler = new thehandler();
ascbubble.addActionListener(handler);
descbubble.addActionListener(handler);
ascballoon.addActionListener(handler);
descballoon.addActionListener(handler);
//add to the frame
add(input1);
add(input2);
add(input3);
add(input4);
add(input5);
add(output1);
add(output2);
add(output3);
add(output4);
add(output5);
add(ascbubble);
add(descbubble);
add(ascballoon);
add(descballoon);
add(clear);
}
private class thehandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==ascbubble)
{
//input1
str1=input1.getText();
int1=Integer.parseInt(str1);
//input2
str2=input2.getText();
int2=Integer.parseInt(str2);
//input3
str3=input3.getText();
int3=Integer.parseInt(str3);
//input4
str4=input4.getText();
int4=Integer.parseInt(str4);
//input5
str5=input5.getText();
int5=Integer.parseInt(str5);
int contain[]={int1,int2,int3,int4,int5};
//formula for Buble Sort Ascending Order
for ( int pass = 1; pass < contain.length; pass++ )
{
for ( int i = 0; i < contain.length - pass; i++ )
{
if ( contain[ i ] > contain[ i + 1 ] )
{
temp = contain[ i ];
contain[ i ] = contain[ i + 1 ];
contain[ i + 1 ] = temp;
}
}
}
}
}
}
public static void main(String[]args)
{
JFrame frame = new JFrame("Sorting");
frame.add(new Sorting());
frame.setSize(550, 500);
frame.setVisible(true);
}
}
First of all, JTextArea has a simple append method, so you only need one. On each pass, you simply need to generate a new String which represents the current state of the array
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringJoiner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Sort {
public static void main(String[] args) {
new Sort();
}
public Sort() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTextField fieldValues;
private JTextArea fieldResults;
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.insets = new Insets(2, 2, 2, 2);
fieldValues = new JTextField(20);
fieldResults = new JTextArea(10, 20);
add(fieldValues, gbc);
JButton btn = new JButton("Sort");
add(btn, gbc);
add(new JScrollPane(fieldResults), gbc);
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
String[] values = fieldValues.getText().split(",");
int[] contain = new int[values.length];
for (int index = 0; index < values.length; index++) {
contain[index] = Integer.parseInt(values[index].trim());
}
for (int pass = 1; pass < contain.length; pass++) {
for (int i = 0; i < contain.length - pass; i++) {
if (contain[i] > contain[i + 1]) {
int temp = contain[i];
contain[i] = contain[i + 1];
contain[i + 1] = temp;
StringJoiner sj = new StringJoiner(", ", "", "\n");
for (int value : contain) {
sj.add(Integer.toString(value));
}
fieldResults.append(sj.toString());
}
}
}
}
});
}
}
}
Now, the problem with this is, the larger the dataset, the more time it will take to sort, this will mean the UI will pause until the sort is completed
One solution would be to use a SwingWorker, for example...
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;
import java.util.StringJoiner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingWorker;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Sort {
public static void main(String[] args) {
new Sort();
}
public Sort() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTextField fieldValues;
private JTextArea fieldResults;
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.insets = new Insets(2, 2, 2, 2);
fieldValues = new JTextField(20);
fieldResults = new JTextArea(10, 20);
add(fieldValues, gbc);
JButton btn = new JButton("Sort");
add(btn, gbc);
add(new JScrollPane(fieldResults), gbc);
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
btn.setEnabled(false);
String text = fieldValues.getText();
SortWorker worker = new SortWorker(text);
worker.addPropertyChangeListener(new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent evt) {
String name = evt.getPropertyName();
if ("state".equals(name)) {
if (worker.getState() == SwingWorker.StateValue.DONE) {
btn.setEnabled(true);
}
}
}
});
worker.execute();
}
});
}
public class SortWorker extends SwingWorker<int[], String> {
private String text;
public SortWorker(String text) {
this.text = text;
}
#Override
protected void process(List<String> chunks) {
for (String value : chunks) {
fieldResults.append(value);
}
}
#Override
protected int[] doInBackground() throws Exception {
String[] values = text.split(",");
int[] contain = new int[values.length];
for (int index = 0; index < values.length; index++) {
contain[index] = Integer.parseInt(values[index].trim());
}
for (int pass = 1; pass < contain.length; pass++) {
for (int i = 0; i < contain.length - pass; i++) {
if (contain[i] > contain[i + 1]) {
int temp = contain[i];
contain[i] = contain[i + 1];
contain[i + 1] = temp;
StringJoiner sj = new StringJoiner(", ", "", "\n");
for (int value : contain) {
sj.add(Integer.toString(value));
}
publish(sj.toString());
}
}
}
return contain;
}
}
}
}