Hello Everyone I am new to Java so I am sure I am doing something obviously wrong here but I just keep going in circles.
I am trying to add a scroll to a JTextarea. Here is what I have tried but it doesn't show up.
textarea1 = new JTextArea();
textarea1.setBounds(251,26,795,345);
textarea1.setBackground(new Color(255,255,255));
textarea1.setForeground(new Color(0,0,0));
textarea1.setEnabled(true);
textarea1.setFont(new Font("sansserif",0,12));
textarea1.setText("");
textarea1.setBorder(BorderFactory.createBevelBorder(1));
textarea1.setVisible(true);
JScrollPane ScrollPane1 = new JScrollPane(textarea1);
ScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//adding components to contentPane panel
contentPane.add(browseFileOne);
contentPane.add(browseOutput);
contentPane.add(button1);
contentPane.add(button2);
contentPane.add(button4);
contentPane.add(fileOneText);
contentPane.add(fileTwoText);
contentPane.add(label1);
contentPane.add(label2);
contentPane.add(label3);
contentPane.add(outputTextFile);
contentPane.add(textarea1);
see working example:
https://repl.it/repls/DimpledDefensiveSourcecode
Code:
import javax.swing.*;
import java.awt.*;
public class Main extends JFrame {
public static void main(String[] args) {
new Main();
}
public Main() {
this.setSize(400, 400);
this.setLocation(0, 0);
this.setResizable(false);
this.setTitle("Application");
JPanel painel = new JPanel(null);
// Creating the Input
JTextField tf1 = new JTextField("Some random text", 15);
tf1.setBounds(5, 5, this.getWidth() - 120, 20);
tf1.setColumns(10);
tf1.setText("Omg");
// resultsTA,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
painel.add(tf1);
// Creating the button
JButton button1 = new JButton("Send");
button1.setBounds(290, 5, 100, 19);
painel.add(button1);
// Creating the TextArea
JTextArea ta1 = new JTextArea(15, 20);
JScrollPane scr = new JScrollPane(ta1,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);// Add your text area to scroll pane
ta1.setBounds(5, 35, 385, 330);
ta1.setLineWrap(true);
ta1.setWrapStyleWord(true);
scr.setBounds(20, 30, 100, 40);// You have to set bounds for all the controls and containers incas eof null layout
painel.add(scr);// Add you scroll pane to container
this.add(painel);
this.setVisible(true);
}
}
Related
When the JComboBox, cmbox was not added to a JPanel, two panels, p1 & p2 could be rendered. You may comment out the combo box portion to see the result. But after I added the combo box into one of the panels, all panels were not rendered.
My code is like the following:
import java.awt.*;
import javax.swing.*;
public class TestCombo {
public static void main(String[] args) {
JFrame frame = new JFrame("康樂彩歌");
frame.setVisible(true);
frame.setBounds(0, 0, 1368, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p1 = new JPanel();
p1.setBackground(Color.CYAN);
JRadioButton rb1 = new JRadioButton("加簡譜", false);
rb1.setFont(new Font("新細明體", Font.PLAIN, 20));
JRadioButton rb2 = new JRadioButton("加人聲", false);
rb2.setFont(new Font("新細明體", Font.PLAIN, 20));
rb1.setBounds(450, 180, 50, 50);
rb2.setBounds(500, 180, 50, 50);
JButton btPlay = new JButton("PLAY");
btPlay.setFont(new Font(Font.DIALOG_INPUT, Font.BOLD, 25));
btPlay.setBounds(100, 20, 100, 20);//x axis, y axis, width, height
JButton btStop = new JButton("STOP");
btStop.setFont(new Font(Font.DIALOG_INPUT, Font.BOLD, 25));
btStop.setBounds(140, 20, 100, 20);//x axis, y axis, width, height
//p1.add(cmbox);
p1.add(rb1);
p1.add(rb2);
p1.add(btPlay);
p1.add(btStop);
p1.setBorder(BorderFactory.createLineBorder(Color.black));
JPanel p2 = new JPanel();
p2.setBackground(Color.PINK);
p2.setBorder(BorderFactory.createLineBorder(Color.red));
JComboBox cmbox = new JComboBox(); //The JComboBox to be added to a JPanel
cmbox.setFont(new Font("新細明體", Font.PLAIN, 20));
cmbox.addItem("紫竹調");
cmbox.addItem("走一同去郊遊");
cmbox.addItem("大野狼");
cmbox.addItem("歸來吧蘇連多");
cmbox.addItem("追尋");
cmbox.addItem("三輪車");
cmbox.addItem("我家門前有小河");
cmbox.addItem("漁家樂");
cmbox.addItem("嚕啦啦");
cmbox.addItem("踏雪尋梅");
p2.add(cmbox);
frame.add(p1, BorderLayout.PAGE_START);
frame.add(p2, BorderLayout.CENTER);
}
}
Implementing the changes as detailed below the code, solves the problem.
import java.awt.*;
import javax.swing.*;
public class TestCombo {
public static void main(String[] args) {
JFrame frame = new JFrame("康樂彩歌");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p1 = new JPanel();
p1.setBackground(Color.CYAN);
JRadioButton rb1 = new JRadioButton("加簡譜", false);
rb1.setFont(new Font("新細明體", Font.PLAIN, 20));
JRadioButton rb2 = new JRadioButton("加人聲", false);
rb2.setFont(new Font("新細明體", Font.PLAIN, 20));
JButton btPlay = new JButton("PLAY");
btPlay.setFont(new Font(Font.DIALOG_INPUT, Font.BOLD, 25));
JButton btStop = new JButton("STOP");
btStop.setFont(new Font(Font.DIALOG_INPUT, Font.BOLD, 25));
p1.add(rb1);
p1.add(rb2);
p1.add(btPlay);
p1.add(btStop);
p1.setBorder(BorderFactory.createLineBorder(Color.black));
JPanel p2 = new JPanel();
p2.setBackground(Color.PINK);
p2.setBorder(BorderFactory.createLineBorder(Color.red));
JComboBox cmbox = new JComboBox(); //The JComboBox to be added to a JPanel
cmbox.setFont(new Font("新細明體", Font.PLAIN, 20));
cmbox.addItem("紫竹調");
cmbox.addItem("走一同去郊遊");
cmbox.addItem("大野狼");
cmbox.addItem("歸來吧蘇連多");
cmbox.addItem("追尋");
cmbox.addItem("三輪車");
cmbox.addItem("我家門前有小河");
cmbox.addItem("漁家樂");
cmbox.addItem("嚕啦啦");
cmbox.addItem("踏雪尋梅");
p2.add(cmbox);
frame.add(p1, BorderLayout.PAGE_START);
frame.add(p2, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
btPlay.setBounds(100, 20, 100, 20); Do not set the bounds of components. Let the layouts (padding and borders) do their job.
frame.setBounds(0, 0, 1368, 500); That's just a guess, and if it's the right guess on one OS, it will be the wrong guess on others. Instead pack() the window after components are added.
Sidebar: GUIs should be started on the EDT. (Not implemented above: 'batteries not included'.)
I have added some code for scrollbar which I got from questions asked by some other people on stackoverflow but I am not getting any scrollbar added to my JTextArea. I want to add scrollbar to JTextArea area2 in f2 frame.
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
public class TextAreaExample implements ActionListener {
JFrame f1 = new JFrame("INPUT WINDOW");
JFrame f2 = new JFrame("FILE DATA OUTPUT");
JTextArea area1;
JTextArea area2;
JButton b;
TextAreaExample() {
area1 = new JTextArea();
area2 = new JTextArea();
JScrollPane scroll = new JScrollPane (area2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
b = new JButton("click Me");
b.setBounds(100, 95, 80, 30);
f1.add(b);
area1.setBounds(10, 30, 200, 60);
area2.setBounds(5, 5, 480, 480);
f1.add(area1);
f2.add(area2);
f2.add(scroll);
f1.setSize(300,140);
f2.setSize(510, 510);
f1.setLayout(null);
f2.setLayout(null);
f1.setVisible(true);
f2.setVisible(true);
b.addActionListener(this);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == b) {
String s1 = area1.getText();
String s2 = "";
try {
FileInputStream fin = new FileInputStream(s1);
BufferedInputStream bin = new BufferedInputStream(fin);
int i;
while((i = bin.read()) != -1) {
s2 = s2 + (char)i;
}
bin.close();
fin.close();
}catch(Exception a) {
System.out.println(a);
}
area2.setText(s2);
}
}
public static void main(String args[]) {
new TextAreaExample();
}
}
JScrollPane scroll = new JScrollPane (area2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
b = new JButton("click Me");
b.setBounds(100, 95, 80, 30);
f1.add(b);
area1.setBounds(10, 30, 200, 60);
area2.setBounds(5, 5, 480, 480);
f1.add(area1);
f2.add(area2);
First you create a JScrollPane using the JTextArea as a parameter, which is correct.
But then you add the text area to the frame, which is incorrect. Swing components can only have a single parent so the text area is removed from the scroll pane.
The scroll pane must be added to the frame.
f1.add(scroll);
Also, get rid of all the null layouts and setBounds() statements. Swing was designed to be used with layout managers. Read the section from the Swing tutorial on Layout Manager for more information and examples to get your started.
Now when you create the text area you can use:
JTextArea textArea = new JTextArea(5, 20);
to suggest the original size for the text area. Scrollbars will then appear when more than 5 lines of data is added.
I'm trying to add a JScrollPane to JTextArea, but when I add it, JPane is not showing my textarea anymore. Without JScrollPane it shows it, but then I can't display all the information retrieved from a file.
This is my code for one of textAreas which I want to be wrapped with JScrollPane.
public GUI_CWK()
{
//frame details
setSize(450,400);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("H.o. | Location Database");
setExtendedState(JFrame.MAXIMIZED_BOTH);
getContentPane().setBackground(Color.YELLOW);
// Useing Lable to set a title for multiple buttons
lbl2 = new JLabel();
lbl2.setSize(420,85);
lbl2.setLocation(15,0);
lbl2.setEnabled(false);
lbl2.setFont(new Font ("arial",5,17));
lbl2.setText("Options to Manipulate with Data");
add(lbl2);
// Adding buttons to JFrame
btn1 = new JButton("Add new entry");
btn1.setSize(120,20);
btn1.setLocation(7,80);
btn1.addActionListener(this);
add(btn1);
btn2 = new JButton("Search");
btn2.setSize(120,20);
btn2.setLocation(7,110);
btn2.addActionListener(this);
add(btn2);
btn3 = new JButton("Update Entry");
btn3.setSize(120,20);
btn3.setLocation(150,110);
btn3.addActionListener(this);
add(btn3);
btn4 = new JButton("Print All");
btn4.setSize(120,20);
btn4.setLocation(150,80);
btn4.addActionListener(this);
add(btn4);
btn5 = new JButton("Print Arrays");
btn5.setSize(120,20);
btn5.setLocation(7,140);
btn5.addActionListener(this);
add(btn5);
btn6 = new JButton("Delete Entry");
btn6.setSize(120,20);
btn6.setLocation(300,80);
btn6.addActionListener(this);
add(btn6);
btn7 = new JButton("Delete ALL");
btn7.setSize(120,20);
btn7.setLocation(300,110);
btn7.addActionListener(this);
add(btn7);
lbl1 = new JLabel();
lbl1.setSize(420,85);
lbl1.setLocation(15,190);
lbl1.setEnabled(false);
lbl1.setFont(new Font ("arial",5,17));
lbl1.setText("Extra Options");
add(lbl1);
btn8 = new JButton("Sort Data");
btn8.setSize(160,20);
btn8.setLocation(7,265);
btn8.addActionListener(this);
add(btn8);
btn9 = new JButton("Open Text file");
btn9.setSize(160,20);
btn9.setLocation(7,300);
btn9.addActionListener(this);
add(btn9);
btn11 = new JButton("Retrieve from text file");
btn11.setSize(160,20);
btn11.setLocation(240,265);
btn11.addActionListener(this);
add(btn11);
btn12 = new JButton("Exit Program");
btn12.setSize(160,20);
btn12.setLocation(240,300);
btn12.addActionListener(this);
add(btn12);
lbl3 = new JLabel();
lbl3.setSize(420,85);
lbl3.setLocation(15,340);
lbl3.setEnabled(false);
lbl3.setFont(new Font ("arial",5,17));
lbl3.setText("Terminal Window");
add(lbl3);
// Adding textArea to display each user entered value
textArea2 = new JTextArea(5, 20);
textArea2.setEditable(false);
textArea2.setSize(440,300);
textArea2.setLocation(7,400);
textArea2.setDisabledTextColor(Color.black);
textArea2.setFont(new Font("arial",5,14));
sp2 = new JScrollPane(textArea2);
sp2.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
add(sp2);
lbl4 = new JLabel();
lbl4.setSize(420,40);
lbl4.setLocation(570,10);
lbl4.setEnabled(false);
lbl4.setFont(new Font ("arial",5,17));
lbl4.setText("Data Display");
add(lbl4);
textArea = new JTextArea(55, 50);
textArea.setEditable(false);
textArea.setSize(800,640);
textArea.setLocation(560,60);
textArea.setFont(new Font("arial",5,19));
textArea.setDisabledTextColor(Color.black);
sp = new JScrollPane(textArea);
sp.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
add(sp);
setVisible(true);
}
I think that this should solve your problem.
You'll notice that I erased the thiss between the addActionListener() methods because you have to specify what do you want the ActionListeners to do.
StartingPoint (main) class:
public class StartingPoint{
public static void main(String[] args){
GUI_CWK gui_cwk = new GUI_CWK(); //Creates a new instance of "GUI_CWK".
gui_cwk.setVisible(true); //You set the gui_cwk to visible.
}
}
GUI_CWK class:
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
public class GUI_CWK extends JFrame{
private static final long serialVersionUID = 1L;
public GUI_CWK() {
// frame details
this.setSize(450, 400);
JPanel panel = new JPanel();
this.setLayout(null); /* Each time that you want to refer to the classyou are in, you say "this" */
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("H.o. | Location Database");
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.getContentPane().setBackground(Color.YELLOW);
// Using labels to set a title for multiple buttons
JLabel lbl2 = new JLabel(); /*Remember that you have to create the instances of an object by naming the class before the name of the instance */
lbl2.setSize(420, 85);
lbl2.setLocation(15, 0);
lbl2.setEnabled(false);
lbl2.setFont(new Font("arial", 5, 17));
lbl2.setText("Options to Manipulate with Data");
panel.add(lbl2);
// Adding buttons to JFrame
JButton btn1 = new JButton("Add new entry");
btn1.setSize(120, 20);
btn1.setLocation(7, 80);
btn1.addActionListener(/* Insert ActionListener */);
panel.add(btn1); //The button is added to the panel, so then the last one can be added to the JFrame at the end.
JButton btn2 = new JButton("Search");
btn2.setSize(120, 20);
btn2.setLocation(7, 110);
btn2.addActionListener(/* Insert ActionListener */);
panel.add(btn2);
JButton btn3 = new JButton("Update Entry");
btn3.setSize(120, 20);
btn3.setLocation(150, 110);
btn3.addActionListener(/* Insert ActionListener */);
panel.add(btn3);
JButton btn4 = new JButton("Print All");
btn4.setSize(120, 20);
btn4.setLocation(150, 80);
btn4.addActionListener(/* Insert ActionListener */);
panel.add(btn4);
JButton btn5 = new JButton("Print Arrays");
btn5.setSize(120, 20);
btn5.setLocation(7, 140);
btn5.addActionListener(/* Insert ActionListener */);
panel.add(btn5);
JButton btn6 = new JButton("Delete Entry");
btn6.setSize(120, 20);
btn6.setLocation(300, 80);
btn6.addActionListener(/* Insert ActionListener */);
panel.add(btn6);
JButton btn7 = new JButton("Delete ALL");
btn7.setSize(120, 20);
btn7.setLocation(300, 110);
btn7.addActionListener(/* Insert ActionListener */);
panel.add(btn7);
JLabel lbl1 = new JLabel();
lbl1.setSize(420, 85);
lbl1.setLocation(15, 190);
lbl1.setEnabled(false);
lbl1.setFont(new Font("arial", 5, 17));
lbl1.setText("Extra Options");
panel.add(lbl1);
JButton btn8 = new JButton("Sort Data");
btn8.setSize(160, 20);
btn8.setLocation(7, 265);
btn8.addActionListener(/* Insert ActionListener */);
panel.add(btn8);
JButton btn9 = new JButton("Open Text file");
btn9.setSize(160, 20);
btn9.setLocation(7, 300);
btn9.addActionListener(/* Insert ActionListener */);
panel.add(btn9);
JButton btn11 = new JButton("Retrieve from text file");
btn11.setSize(160, 20);
btn11.setLocation(240, 265);
btn11.addActionListener(/* Insert ActionListener */);
panel.add(btn11);
JButton btn12 = new JButton("Exit Program");
btn12.setSize(160, 20);
btn12.setLocation(240, 300);
btn12.addActionListener(/* Insert ActionListener */);
panel.add(btn12);
JLabel lbl3 = new JLabel();
lbl3.setSize(420, 85);
lbl3.setLocation(15, 340);
lbl3.setEnabled(false);
lbl3.setFont(new Font("arial", 5, 17));
lbl3.setText("Terminal Window");
panel.add(lbl3);
// Adding textArea to display each user entered value
JTextArea textArea2 = new JTextArea(5, 20);
textArea2.setEditable(false);
textArea2.setSize(440, 300);
textArea2.setLocation(7, 400);
textArea2.setDisabledTextColor(Color.black);
textArea2.setFont(new Font("arial", 5, 14));
JScrollPane sp2 = new JScrollPane(textArea2);
sp2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
textArea2.add(sp2); //Here you add the scrollPanel to the textArea,
//in the original code you were adding it to the frame
panel.add(textArea2);
JLabel lbl4 = new JLabel();
lbl4.setSize(420, 40);
lbl4.setLocation(570, 10);
lbl4.setEnabled(false);
lbl4.setFont(new Font("arial", 5, 17));
lbl4.setText("Data Display");
panel.add(lbl4);
JTextArea textArea = new JTextArea(55, 50);
textArea.setEditable(false);
textArea.setSize(800, 640);
textArea.setLocation(560, 60);
textArea.setFont(new Font("arial", 5, 19));
textArea.setDisabledTextColor(Color.black);
JScrollPane sp1 = new JScrollPane(textArea);
sp1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
textArea.setVisible(true);
textArea.add(sp1);
panel.add(textArea);
this.add(panel); //Everything which was added to the panel now is added to the frame.
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Here is an example of using a scroll pane.
import javax.swing.*;
import java.awt.*;
public class ScrollPaneExample{
public void buildGui(){
JFrame frame = new JFrame("button and scrollpane");
JButton button = new JButton("example");
JTextArea area = new JTextArea(20, 20);
//this is where we will place our components.
//It has a flow layout by default.
JPanel content = new JPanel();
JScrollPane sp = new JScrollPane(area);
content.add(button);
content.add(sp);
//this line limits the size of the scroll pane.
sp.setPreferredSize(new Dimension(400, 60));
//The frame will use our panel instead of the default one.
frame.setContentPane(content);
//sizes the frame to the content area.
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args){
EventQueue.invokeLater(()->{new ScrollPaneExample().buildGui();});
}
}
The main difference is that I set my content pane so I know its layout manager. You appear to be manually setting all of the sizes, which I am surprised it works. You should choose a good layout manager. pick one you think does what you want.
Also note that this is a minimal example that will compile.
Thank you for help guys, I found out the answer. As #matt told me, I should add size and location to JScrollPane instead of textArea. So I did it and it works for me now.
code is below.
textArea2 = new JTextArea(5,20);
textArea2.setEditable(false);
textArea2.setDisabledTextColor(Color.black);
textArea2.setFont(new Font("arial",5,14));
sp2 = new JScrollPane(textArea2);
sp2.setSize(440,300);
sp2.setLocation(7,400);
sp2.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
add(sp2);
For some reason my scrollbar is appearing but it is not working. What is suppose to happen is using the scrollbar to scroll through the text of the textarea. Can somebody please explain why this isnt working?
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class App extends JFrame{
private JPanel paneel;
public App(){
paneel = new AppPaneel();
setContentPane(paneel);
}
public static void main(String args[]){
JFrame frame = new App();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setTitle("Auto Clicker");
frame.setVisible(true);
}
}
class AppPaneel extends JPanel{
private JTextField delayField, xLocation, yLocation;
private JTextArea listArea;
private JButton addButton, saveButton, runButton;
private JScrollPane scroll;
public AppPaneel(){
setLayout(null);
delayField = new JTextField();
delayField.setBounds(10, 10, 85, 25);
delayField.setText("delay in ms");
xLocation = new JTextField();
xLocation.setBounds(105, 10, 85, 25);
xLocation.setText("X position");
yLocation = new JTextField();
yLocation.setBounds(200, 10, 85, 25);
yLocation.setText("Y position");
addButton = new JButton("Add");
addButton.setBounds(295, 10, 75, 24);
addButton.addActionListener(new AddHandler());
listArea = new JTextArea();
listArea.setBounds(10, 45, 360, 180);
scroll = new JScrollPane(listArea);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setBounds(370, 45, 20, 180);
saveButton = new JButton("Save");
saveButton.setBounds(10, 230, 85, 24);
runButton = new JButton("Run (F1)");
runButton.setBounds(105, 230, 85, 24);
runButton.addActionListener(new RunHandler());
add(delayField);
add(xLocation);
add(yLocation);
add(addButton);
add(listArea);
add(saveButton);
add(runButton);
add(scroll);
}
class AddHandler implements ActionListener{
public void actionPerformed(ActionEvent a){
listArea.setText(listArea.getText() + delayField.getText() + ", " + xLocation.getText() + ", " + yLocation.getText() + ", " + "click;" + "\n");
}
}
class RunHandler implements ActionListener{
private Robot bot;
private String text;
int foo = Integer.parseInt("1234");
public void actionPerformed(ActionEvent b) {
try{
text = listArea.getText();
bot = new Robot();
for(String line : text.split("\\n")){
int delay = Integer.parseInt((line.substring(0, 4)));
int xpos = Integer.parseInt((line.substring(6, 10)));
int ypos = Integer.parseInt((line.substring(12, 16)));
bot.mouseMove(xpos, ypos);
Thread.sleep(delay);
bot.mousePress(InputEvent.BUTTON1_MASK);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
}
}
catch (AWTException | InterruptedException e){
e.printStackTrace();
}
}
}
}
Don't use null layouts and setBounds as it is messing up your program. We tell folks time and time again not to do this for a reason -- by setting the JTextArea's bound you constrain its size so it won't grow when it needs to. The solution, as always -- learn and use the layout managers. Set the JTextArea's column and row properties but not its bounds, its size, or its preferred size.
Next don't do this: Thread.sleep(delay); in your Swing application as it will put the entire application to sleep. Use a Swing Timer instead for any delays. The tutorials can help you use this.
For a non-functional layout example:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.*;
#SuppressWarnings("serial")
public class App2 extends JPanel {
private static final int GAP = 5;
private JTextField textField1 = new JTextField(GAP);
private JTextField textField2 = new JTextField(GAP);
private JTextField textField3 = new JTextField(GAP);
private JTextArea textArea = new JTextArea(15, 30);
public App2() {
JPanel topPanel = new JPanel(new GridLayout(1, 0, GAP, GAP));
topPanel.add(textField1);
topPanel.add(textField2);
topPanel.add(textField3);
topPanel.add(new JButton("Add"));
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
JPanel bottomPanel = new JPanel(new GridLayout(1, 0, GAP, GAP));
bottomPanel.add(new JButton("Save"));
bottomPanel.add(new JButton("Run (F1)"));
bottomPanel.add(new JLabel(""));
bottomPanel.add(new JLabel(""));
setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
setLayout(new BorderLayout(GAP, GAP));
add(topPanel, BorderLayout.PAGE_START);
add(scrollPane, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.PAGE_END);
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Auto Clicker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new App2());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
Your whole approach is wrong. You should not be using a null layout. For example:
scroll.setBounds(370, 45, 20, 180);
you are setting the width to 20. Well how to you expect the text area to display in 20 pixels? This is the problem with using setBounds(...), the random numbers you use have no meaning. Let a layout manager do its job. Also:
add(listArea);
The problem is that a component can only have a single parent. You originally created the JScrollPane with the text area which is correct.
But then you add the text area directly to the frame which removes it from the scroll pane so the scroll panel will no longer function.
Get rid of that statement and just add the scroll pane to the frame.
However, I don't recommend you use this as your final solution. I just wanted to mention the current problems so you hopefully understand the benefits of using layout managers and so that you don't try to share components again.
The proper solution is to use layout managers as has been suggested by the "Community Wiki". Then the layout manager will determine the size and location of each component so you don't need to worry about calculating your pixel values correctly.
I'm pretty new to java. And i can't get this to work... I'm trying to add components using this code:
public class Board
{
public static void main(String[] args) {
JFrame window = new JFrame("Tellraw Generator");
window.setVisible(true);
window.setSize(400, 600);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel label = new JLabel();
panel.setLayout(null);
window.add(panel);
//"Generate" Button
JButton button1 = new JButton("Generate");
button1.setBounds(262, 485, 100, 37);
panel.add(button1);
//"Add Text" Button
JButton button2 = new JButton("Add Text");
button2.setBounds(51, 337, 88, 33);
panel.add(button2);
//Title
JLabel txt1 = new JLabel("Tellraw Generator");
txt1.setFont(new Font("Minecrafter Alt Regular", Font.BOLD, 29));
txt1.setBounds(61, 18, 278, 30);
panel.add(txt1);
}
}
But when i'm trying to do it, the components aren't showing up on the screen.
So are there someone who can tell me why it isn't working/Showing up and how i can add it in ?
Thanks
You mean that JLabel and JButton don't appear? right ? not JTextField because there is no JTextField in the code
Anyway just add this line of code to the end:
window.add(panel);
Here you are adding the JPanel that contains all the JComponents to the JFrame
and always: have the setVisible(true) call as last call. Everything that you add to the window/frame must be done before the setVisible call
This is how to declare a JTextField
final JTextField variableName = new JTextField(size);
This is how you get the text from JTextField
variableName.getText()
Hope that helps.
Try this:
public class Board
{
public static void main(String[] args) {
JFrame window = new JFrame("Tellraw Generator");
window.setVisible(true);
window.setSize(400, 600);
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel label = new JLabel();
panel.setLayout(null);
window.add(panel);
//"Generate" Button
JButton button1 = new JButton("Generate");
button1.setBounds(262, 485, 100, 37);
panel.add(button1);
//"Add Text" Button
JButton button2 = new JButton("Add Text");
button2.setBounds(51, 337, 88, 33);
panel.add(button2);
//Title
JTextField txt1 = new JTextField ();
txt1.setFont(new Font("Minecrafter Alt Regular", Font.BOLD, 29));
txt1.setBounds(61, 18, 278, 30);
panel.add(txt1);
}
}