command line indicator in JTextArea - java

I've made a JTextArea where I input different commands and separate them by newline "\n", and if there is an error in one of the lines then I write it in a console output. Here I made a very simple, and not the best solution to make this line indication, but it's a bit buggy.
How I made it
I've defined a textArea where I can type different information/commands, and if one of the commands/lines is invalid I write it in the console just to display something for now. I basically count the lines by splitting the textArea rows up by "\n" and then count which line the error occurs in, and the left consoleLineNum is using the amount of rows in textArea, to then make a string containing all the numbers of rows+"\n".
But here my question is, is this a good enough way? If so, why/how can I make it more robust? Or how can I make this indication with line numbers, in the left? It has to increase each time the user makes a new line in the textArea.
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setLayout(new BorderLayout(3,3));
/*----- Panels -----*/
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
//Add Components to this panel.
JScrollPane scrollPane = new JScrollPane(textArea);
JScrollPane scrollPaneOutput = new JScrollPane(consoleOutput);
textArea.setCaretPosition(textArea.getDocument().getLength());
consoleOutput.setEditable(false);
consoleLineNum.setEditable(false);
ButtonPanel_listener(buttonPanel);
textArea.addKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
consoleLineNum.setText("");
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= Integer.parseInt(String.valueOf(textArea.getText().split("\n").length)); i++) {
sb.append(i + " \n");
}
consoleLineNum.setText(sb.toString());
}
#Override
public void keyReleased(KeyEvent e) {
}
});
//Background
panel1.setBackground(Color.LIGHT_GRAY);
//Preferred size
panel1.setPreferredSize(new Dimension(100, 100));
scrollPane.setPreferredSize(new Dimension(200, 200));
panel2.setLayout(new BorderLayout());
panel2.add(scrollPane, BorderLayout.CENTER);
panel2.add(buttonPanel, BorderLayout.SOUTH);
panel2.add(consoleLineNum, BorderLayout.WEST);
consoleLineNum.setText(num);
panel3.setLayout(new BorderLayout());
panel3.add(drawCanvas, BorderLayout.CENTER);
panel1.setLayout(new BorderLayout());
panel1.add(scrollPaneOutput, BorderLayout.CENTER);
//textArea.addActionListener(e -> drawCanvas.drawCircle(250, 250, 200));
//Add contents to the window.
frame.add(panel2, BorderLayout.WEST);
frame.add(panel3, BorderLayout.CENTER);
frame.add(panel1, BorderLayout.SOUTH);
//Display the window.
frame.pack();
frame.setVisible(true);
}
Difference between your expected and actual results
When I hit newline/Enter, it doesn't show the number right away, only when I start typing.
Or
Here I want it to match where the user is, so if the user hit enter, and go to the next line, then the number matches and is shown right away.
If I delete all lines, except some, it still shows the numbers
Here I want it to wipe all the numbers and update it to match the amount of data in textArea.
Tried this, and it works almost as expected. The only problem is that when I delete lines, it's one behind.
#Override
public void keyPressed(KeyEvent e) {
numI = textArea.getLineCount();
if(e.getKeyCode() == KeyEvent.VK_BACK_SPACE && numI > 0) {
numI = numI - 1;
}
if(e.getKeyCode() == KeyEvent.VK_BACK_SPACE || e.getKeyCode() == KeyEvent.VK_ENTER){
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= numI; i++) {
sb.append(i+1 + " \n");
}
consoleLineNum.setText(sb.toString());
}
}

Use a DocumentListener to listen for changes in the text of the JTextArea. The API of JTextArea already provides a method that tells you how many lines it contains, namely getLineCount(). After placing the JTextArea in a JScrollPane, set a JList as the row header for the JScrollPane.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JViewport;
import javax.swing.WindowConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class CountLns implements DocumentListener, Runnable {
private JFrame frame;
private JList<Integer> lineNumbersList;
private JTextArea textArea;
public void changedUpdate(DocumentEvent docEvent) {
// Never called.
}
public void insertUpdate(DocumentEvent docEvent) {
handleDocEvent();
}
public void removeUpdate(DocumentEvent docEvent) {
handleDocEvent();
}
#Override
public void run() {
showGui();
}
private JScrollPane createTextArea() {
textArea = new JTextArea(20, 50);
textArea.getDocument().addDocumentListener(this);
JScrollPane scrollPane = new JScrollPane(textArea);
lineNumbersList = new JList<>(new Integer[]{1});
lineNumbersList.setBackground(Color.cyan);
lineNumbersList.setFont(textArea.getFont());
lineNumbersList.setFixedCellHeight(16);
JViewport rowHeader = new JViewport();
rowHeader.setView(lineNumbersList);
scrollPane.setRowHeader(rowHeader);
return scrollPane;
}
private void handleDocEvent() {
DefaultListModel<Integer> model = new DefaultListModel<>();
List<Integer> lineNumbers = IntStream.range(0, textArea.getLineCount())
.boxed()
.map(i -> i + 1)
.collect(Collectors.toList());
model.addAll(lineNumbers);
lineNumbersList.setModel(model);
}
private void showGui() {
frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(createTextArea(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
textArea.requestFocusInWindow();
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new CountLns());
}
}
Whenever the contents of the JTextArea change, the DocumentListener counts the lines in the JTextArea and sets the JList model to contain exactly that number of elements. So if the JTextArea has 12 lines, the JList will contain all the numbers from 1 to 12.

Try doing it like this:
yourTextArea.addKeyListener(new KeyAdapter() {
#Override
public void keyTyped(KeyEvent e) {
// Returns true every time the user presses either backspace or enter.
if (e.getKeyChar() == '\n' || e.getKeyChar() == '\b') {
int lines;
// Checks if the text ends with a newline, if so,
// it adds 1 to the line count, so that would make your line
// appear even if the user just started a new line.
if (yourTextArea.getText().endsWith("\n")) {
lines = yourTextArea.getText().split("\n").length + 1;
} else {
lines = yourTextArea.getText().split("\n").length;
}
// Removes previous count.
linePanel.setText("");
// Appends a new line to the area for every line.
for (int i = 0; i < lines; i++) {
linePanel.append((i + 1) + "\n");
}
}
}
});

I did not write in Java for 1 year so sorry, if I messed up syntax
First of all, why won't you implement line wrap and line count increment by pressing Enter? It looks much more simple to me
#Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
textArea.append("\n");
Integer lineCount = Integer.parseInt(consoleLineNum.getText());
Integer newLineNumber = new Integer(lineCount.intValue() + 1)
consoleLineNum.append("\n" + newLineNumber.toString())
}
}
However you still will not get actual number of lines, if some of them will be deleted. So you can also add trickier logic to your keyPressed() method
if(e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
// Well I actually googled this line for counting occurences
Integer linesCountActual = textArea.getText().split("\n", -1).length - 1);
Integer linesCountLeft = consoleLineNum.getText().split("\n", -1).length - 1);
if (linesCountActual > linesCountLeft) {
String oldText = consoleLineNum.getText();
String newText = oldText.substring(0, oldText.length() - 2);
consoleLineNum.setText(newText);
}
}

Related

I'm trying to make a button to count characters in a text field

I'm trying to use the JButton count to count the number of characters entered into the JTextField t. I'm new to Java and GUIs but here's my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI1 extends Frame implements ActionListener{
TextField t;
Button count;
int a;
Choice choice;
public GUI1(){
this.t = new TextField("", 30);
this.count = new Button("count");
this.count.addActionListener(this);
JTextField x = new JTextField();
x.setEditable(false);
this.setTitle("Character count");
this.setLayout(new FlowLayout());
this.add(x);
this.add(t);
this.add(count);
this.pack();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()== this.count)
t.setText(choice.getSelectedItem()+ " " +a);
}
I'm also trying to enter the value in another uneditable JTextField x. Any help is appreciated.
Add this to your code
count.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
a = t.getText().length();
}
});
OR
You can use lambda expression like this
count.addActionListener(e -> a = t.getText().length());
For More
http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionListener.html
You need to add a listener
TextField t = new TextField();
Button b = new Button("Count");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int count = t.getText().length();
}
});
You can read more about here
http://www.tutorialspoint.com/awt/awt_button.htm
First of all, I recommend you not to use AWT elements, since it brings tons of problems and it has little to no support, instead you can try using Swing components which are a replacement/fix for AWT. You can read more about here. You might also want to read AWT vs Swing (Pros and Cons).
Now going into your problem:
You should avoid extending from JFrame, I might recommend you to create a new JFrame object instead. Here's the reason to why. That being said, you can also remove all your this.t and other calls with this.
I'm glad you're using a Layout Manager!
And now to count the number of characters on your JTextField and set text to your other JTextField you should use this code:
count.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int count = t.getText().length();
System.out.println(count);
x.setText(t.getText());
}
});
Also I fixed your code, I changed AWT elements to Swing ones and added number of cols to your second JTextField so it would appear.
So, here's a running example that I made from your code (And removed Choice choice line since you didn't posted that code):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI1 {
JTextField t;
JButton count;
int a;
JFrame frame;
public GUI1(){
frame = new JFrame();
t = new JTextField("", 15);
count = new JButton("count");
JTextField x = new JTextField("", 15);
x.setEditable(false);
count.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int count = t.getText().length();
System.out.println(count);
x.setText(t.getText());
}
});
frame.setTitle("Character count");
frame.setLayout(new FlowLayout());
frame.add(x);
frame.add(t);
frame.add(count);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main (String args[]) {
new GUI1();
}
}
When you click on a button you should
String str = t.getText(); // get string from jtextfield
To save the text from the texfield. Then you can use something like:
a = str..length(); // get length of string
x.setText(str + " " + a); //set it to the field
To set it to the JTextField.

Scrollbar not scrolling

I have a program that is designed simply to make and test a user interface. The current setup is that it displays a large black screen with text, and a box for user input at the bottom, and a scrollbar on the right side. I've got everything working the way I want it except that the scrollbar absolutely will not scroll. It is there, but doesn't seem connected at all to the textarea. You can press the buttons on the bar, but they don't do anything. Any help would be appreciated!
Here is the code I have so far:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class RandomTest extends JFrame implements KeyListener{
JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
JFrame frame = new JFrame();
static JTextArea txt;
static JTextField inputField;
static String text;
static String choice;
static boolean enter = false;
Container positioner = frame.getContentPane();
RandomTest(){
text = "";
txt = new JTextArea(text);
txt.setEditable(false);
inputField = new JTextField("");
txt.setBackground(Color.black);
txt.setFont(new Font("Courier New", Font.PLAIN, 18));
txt.setForeground(Color.lightGray);
inputField.setBackground(Color.black);
inputField.setFont(new Font("Courier New", Font.PLAIN, 18));
inputField.setForeground(Color.lightGray);
panel.add(txt);
panel.add(inputField);
//Dimension d = new Dimension(500,500);
//scrollPane.setPreferredSize(d);
panel.add(scrollPane);
frame.add(panel);
frame.setSize(500,500);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
positioner.setLayout(new BorderLayout());
positioner.add(inputField, BorderLayout.PAGE_END);
positioner.add(scrollPane, BorderLayout.EAST);
positioner.add(txt, BorderLayout.CENTER);
positioner.setBackground(Color.black);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
inputField.addKeyListener(this);
}
public static void main(String[] arg){
new RandomTest();
println("Please enter the letter 'm'");
for(;;){
println("/\n/\n/\n/\n/\n/\n/\n/\n");
if(input().equals("m")){
println("Thank you.");
}else{
println("Try again.");
}
}
}
public static void println(String line){
text += line + "\n";
txt.setText(text);
}
public static String input(){
for(;;){
if(enter == true){
enter = false;
return choice;
}else{
try {
Thread.sleep(10);
} catch (InterruptedException e) {
if(enter == true){
enter = false;
return choice;
}
}
}
}
}
#Override
public void keyPressed(KeyEvent e) {
}
#Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER){
choice = inputField.getText();
inputField.setText("");
enter = true;
try{
Thread.currentThread().interrupt();
}catch(Exception E){
}
}
}
#Override
public void keyTyped(KeyEvent e) {
}
}
There's nothing attached to the scroll pane's view, so there is nothing for it to scroll...
You need to supply a component view for the scroll, using something like scrollPane.setViewportView(...);
You may want to take a look at How to use scroll panes for some more details
There's a lot of things in you code that worry me...
Thread.sleep in a GUI environment is always of concern, given that this could actually cause your application to become unresponsive. The use of infinite loops, for the same reason.
The use of KeyListener which is simply performing the same function of an ActionListener
The fact that you are adding txt and inputField and scrollPane to two different containers and txt is actually supplementing panel...
You may like to spend some time reading through
Creating a GUI With JFC/Swing
Using Text Components
How to Write an Action Listener
Concurrency in Swing

JList click one and put it into first JTextfield then click another into next JTextfield

I'm currently creating a soundboard like application where the person is going to click one of the items in my JList and its going to set the name of the next available text field as what was just clicked.
For example, my JList contains hello, testing1, testing2. If testing2 is clicked first I would like to put it into the first textfield, and if hello is clicked next I'd like to put it into the 2nd textfield and so on.
The program will have around 100 items in the JList by the time the app is done. I currently can not get this to work and have tried countless times.
Also there is a problem that when the top one and a different JList item is clicked the top one will display first. Not necessarily a problem if I can get the problem fully functional but it makes it feel a little wonky.
My code so far:
package com.leagueoflegends.soundboard;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
public class Soundboard implements ListSelectionListener {
static JList<Object> list;
String[] text = { "hello", "testing1", "testing2" };
Icon icon;
JLabel pictureLabel;
JPanel insidePanel;
JTextField inlineText;
JTextField field[] = new JTextField[6];
public Soundboard() {
JFrame f = new JFrame("soundboard!");
JPanel masterPanel = new JPanel(new BorderLayout());
//icon = new ImageIcon(getClass().getResource("Tray.png"));
//pictureLabel = new JLabel(icon);
list = new JList<Object>(text); // data has type Object[]
list.setSelectionModel(new DefaultListSelectionModel(){
public void setSelectionInterval(int index0, int index1){
if(super.isSelectedIndex(index0)){
super.removeSelectionInterval(index0,index1);
}else{
super.addSelectionInterval(index0,index1);
}
}
});
list.setLayoutOrientation(JList.VERTICAL_WRAP);
list.setVisibleRowCount(-1);
list.addListSelectionListener(this);
JScrollPane listScroller = new JScrollPane(list);
listScroller.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
listScroller.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
listScroller.setPreferredSize(new Dimension(100, 60));
// listScroller.setSize(new Dimension(250, 60));
JPanel smallPanel = new JPanel(new GridLayout(2, 3));
// smallPanel.setBorder(BorderFactory.createLineBorder(Color.red));
for (int i = 0; i <= 5; i++) {
insidePanel = new JPanel(new BorderLayout());
insidePanel.setBorder(BorderFactory.createLineBorder(Color.black));
field[i] = new JTextField();
field[i].setEditable(false);
field[i].setHorizontalAlignment(JTextField.CENTER);
insidePanel.add(field[i], BorderLayout.PAGE_START);
smallPanel.add(insidePanel);
}
masterPanel.add(smallPanel);
// masterPanel.add(pictureLabel, BorderLayout.PAGE_START);
masterPanel.add(listScroller, BorderLayout.WEST);
f.add(masterPanel);
f.pack();
f.setSize(1000, 800);
f.setMinimumSize(new Dimension(400, 350));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
#Override
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting() == false) {
for (int i = 0; i < text.length; i++) {
if (list.getSelectedIndex() == i) {
field[0].setText(text[i]);
}
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new Soundboard();
}
});
}
}
The general idea would be to;
Create an array or List of your JTextFields. Add these your UI.
Create a counter, indicating the current "empty" field
Use either a MouseListener or ListSelectionListener to identify when the user has selected something, extract the value from the list. Using the "current" counter, extract the JTextField from the List and set it's value accordingly, increment the counter

How to make a specific row visible of a textArea in Java

My JTextArea contains thousands of lines but not all of them are visible at a time. I want to programmatically scroll to a specific row of the textArea so that the line is visible. I found that scrollPane has a method scrollRectToVisible but I am not successful with that. Can anyone suggest me how to accomplish the goal. A workable code snippet will be really helpful for me. Thanks.
scrollRectToVisible(...) should work. Make sure you invoke scrollRectToVisible(...) on the text area and not the scrollpane. If that doesn't work then I would guess you are not getting the proper Rectangle to scroll to. Post your SSCCE that demonstrates the problem.
Another approach is to use the gotoStartOfLine(...) method of the Text Utilities. You can also use the centerLineInScrollPane(...) method if you wish.
I guess you've answered this already. I was creating my SSCCE during this time, so I'll post it for others' benefit if not yours.
import java.awt.BorderLayout;
import java.awt.Rectangle;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.text.BadLocationException;
public class TestScrollRectToVisible extends JPanel {
private static final int MAX_LOOP = 10000;
private DefaultListModel listModel = new DefaultListModel();
private JTextArea textarea = new JTextArea(20, 30);
private JList jList = new JList(listModel);
JScrollPane textareaScrollPane = new JScrollPane(textarea);
public TestScrollRectToVisible() {
jList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
String text = jList.getSelectedValue().toString();
text += ": ";
String docText = textarea.getText();
int index = docText.indexOf(text);
if (index < 0) {
return;
}
try {
Rectangle rect = textarea.modelToView(index);
textarea.scrollRectToVisible(rect);
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
}
});
jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
StringBuilder strBuilder = new StringBuilder();
for (int i = 0; i < MAX_LOOP; i++) {
String text = String.valueOf(i);
listModel.addElement(text);
strBuilder.append(text + ": abcdefghijklmnopqrstuvwxyz" + "\n");
}
textarea.setText(strBuilder.toString());
setLayout(new BorderLayout());
add(textareaScrollPane, BorderLayout.CENTER);
add(new JScrollPane(jList), BorderLayout.EAST);
}
private static void createAndShowUI() {
JFrame frame = new JFrame("TestScrollRectToVisible");
frame.getContentPane().add(new TestScrollRectToVisible());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}

Replacing a imageIcon

I have a value called 'AmountWrongGuessed' that gives the amount of wrong guesses the users puts while guessing a word.
Each times the word is not found in the arraylist the AmountWrongGuessed goes ++. (tested this wih a println and it works properly)
Now the problem is each time the AmountWrongGuessed goes 1 up it should display a ImageIcon.
But insteed it displays the last image icon all the time, and skips the other icons.
I use no layout mananger (its set to null, if this makes any difference in the total picture setLayout = null)
Also while initialising this game the amountwrongguessed is default 0, yet it does not display the first imageicon either. (i used different labels before to add each icon on the same position but then i had the problem only the first image displayed and nothing changed).
public HrView(Hrgame hg) {
this.hg = hg;
CreateComponents();
SetLayoutComponents();
UpdateComponents();
AddListeners();
}
Creation of the images:
private void CreateComponents() {
hang0 = new ImageIcon("hang0.gif");
lblHang = new JLabel(hang0);
lblHang.setLocation(60, -10);
lblHang.setSize(200, 200);
hang1 = new ImageIcon("hang1.gif");
lblHang = new JLabel(hang1);
lblHang.setLocation(60, -10);
lblHang.setSize(200, 200);
hang2 = new ImageIcon("hang2.gif");
lblHang = new JLabel(hang2);
lblHang.setLocation(60, -10);
lblHang.setSize(200, 200);
}
private void AddListeners()
{
btnCheck.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
hg.Input(tfToGuessInput.getText().toLowerCase());
Pictures();
lblHang.updateUI();
}
});
}
private void Pictures()
{
//works, does increment
System.out.println(hg.getAmountWrongGuessed());
if (hg.getAmountWrongGuessed() == 0) {
add(lblHang);
}
if (hg.getAmountWrongGuessed() == 1) {
add(lblHang);
}
if (hg.getAmountWrongGuessed() == 2) {
add(lblHang);
}
}
After CreateComponents() your attribute lblHang references the label you created last (the one containing image hang2.) In order to use the 3 labels later on, you need to have 3 label attibutes which you can then use in Pictures().
Btw, in Java the naming convention is that method names start with a lowercase character.
import javax.swing.*;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import java.awt.BorderLayout;
import java.awt.*;
import java.awt.event.*;
public class testgui{
private static int flag = 1;
public static void main(String[] args){
final JLabel label = new JLabel("",new ImageIcon("0.jpg"),JLabel.CENTER);
final JLabel label1 = new JLabel("",new ImageIcon("1.jpg"),JLabel.CENTER);
final JLabel label2 = new JLabel("",new ImageIcon("2.jpg"),JLabel.CENTER);
final JFrame frame = new JFrame();
final JPanel panel = new JPanel();
frame.add(panel,BorderLayout.CENTER);
frame.setVisible(true);
final JButton button = new JButton("Next");
frame.add(button,BorderLayout.SOUTH);
panel.add(label);
frame.pack();
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(flag==0){
System.out.println("0.jpg");
//label image, flag increment
flag = flag+1;
panel.removeAll();
panel.add(label);
frame.pack();
} else if(flag==1){
System.out.println("1.jpg");
//label1 image, flag increment
flag = flag+1;
panel.removeAll();
panel.add(label1);
frame.pack();
} else if (flag==2){
System.out.println("2.jpg");
//label2 image, reset flag to 0
flag = 0;
panel.removeAll();
panel.add(label2);
frame.pack();
}
else{
System.out.println("Wrong flag number !");
}
panel.validate();
panel.updateUI();
}
});
}
}
I think if you want to switch images, using jlabels, the above codes would help. It would help to rotate jlabels containing images but this codes are not optimized.

Categories

Resources