So I want to change the image showing every time i press the Roll button but whenever I try to do it I double instantiate the JFrame. I would like to use the ActionListener this way if it is posible.
Here is my code:
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class DiceFrame extends JFrame{
ImageIcon[] dice_images = new ImageIcon[7];
String score = "This is a test";
JPanel mainPanel;
JPanel scorePanel;
JPanel buttonPanel;
JLabel picLabel;
JTextArea scoreField;
JButton roll;
JButton save;
ActionListener action;
ActionListener output;
public DiceFrame(){
super();
mainPanel = new JPanel();
scorePanel = new JPanel();
buttonPanel = new JPanel();
roll = new JButton("Roll");
save = new JButton("Save");
picLabel = new JLabel();
scoreField = new JTextArea();
setSize(400,300);
setTitle("Dice Program");
loadImage();
getContentPane().add(mainPanel, BorderLayout.CENTER);
getContentPane().add(scorePanel, BorderLayout.EAST);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
mainPanel.add(picLabel);
picLabel.setIcon(dice_images[0]);
buttonPanel.add(roll);
buttonPanel.add(save);
scorePanel.add(scoreField);
scoreField.setText(score);
roll.addActionListener(action);
save.addActionListener(output);
}
private void loadImage()
{
dice_images [0] = new ImageIcon("res/dice_img/die_01_sm.gif");
dice_images [1] = new ImageIcon("res/dice_img/die_02_sm.gif");
dice_images [2] = new ImageIcon("res/dice_img/die_03_sm.gif");
dice_images [3] = new ImageIcon("res/dice_img/die_04_sm.gif");
dice_images [4] = new ImageIcon("res/dice_img/die_05_sm.gif");
dice_images [5] = new ImageIcon("res/dice_img/die_06_sm.gif");
dice_images [6] = new ImageIcon("res/dice_img/die_01_sm.gif");
}
public static void main(String [] args){
DiceFrame frame = new DiceFrame();
frame.setDefaultLookAndFeelDecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class DiceActionListener implements ActionListener{
#Override
public void actionPerformed(ActionEvent e){}
}
class SaveActionListener implements ActionListener{
#Override
public void actionPerformed(ActionEvent e){}
}
I dont know what to put inside the actionPerformed method in order to not create another instance of the JFrame.
Do something like below code. Create a method that can change your image icon. Then call it from as part of your button click. Have a look at the comments.
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class DiceFrame extends JFrame {
ImageIcon[] dice_images = new ImageIcon[7];
String score = "This is a test";
JPanel mainPanel, scorePanel, buttonPanel;
JLabel picLabel;
JTextArea scoreField;
JButton roll, save;
public DiceFrame(){
super();
mainPanel = new JPanel();
scorePanel = new JPanel();
buttonPanel = new JPanel();
roll = new JButton("Roll");
save = new JButton("Save");
picLabel = new JLabel();
scoreField = new JTextArea();
setSize(400,300);
setTitle("Dice Program");
loadImage();
getContentPane().add(mainPanel, BorderLayout.CENTER);
getContentPane().add(scorePanel, BorderLayout.EAST);
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
mainPanel.add(picLabel);
picLabel.setIcon(dice_images[0]);
buttonPanel.add(roll);
buttonPanel.add(save);
scorePanel.add(scoreField);
scoreField.setText(score);
save.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
System.out.println("You clicked save");
}
});
roll.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
//call your icon change method with the index you want to change it to
changeIcon(3);
}
});
}
private void loadImage() {
//use a loop insted of repeting the add, but becareful if i < 10 because your file names need to match :)
for(int i = 0; i < 7; i++) {
dice_images [i] = new ImageIcon("res/dice_img/die_0" + i + "_sm.gif");
}
}
public static void main(String [] args) {
DiceFrame frame = new DiceFrame();
frame.setDefaultLookAndFeelDecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
//create a method to change your icon
public void changeIcon(int imageIndex) {
picLabel.setIcon(this.dice_images[imageIndex]);
}
}
Related
Everything looks ok to me but for some reason, nothing is showing up properly, maybe I missed something but I'm not sure why it's not working, can someone help me out?
** task **
Improve your program by adding two
combo boxes in the frame. Through the combo boxes, the user should be able to
select their preferred fonts and font sizes. The displayed text will then be updated
accordingly (see the figure below).
Here is what its suppose to look like
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.*;
public class ComboGUI extends JFrame implements ActionListener {
public JButton update;
public JTextField textField;
public JLabel textLabel;
public JComboBox<String> fontBox;
public JComboBox sizeBox;
public String font;
public String size;
public ComboGUI()
{
components();
panels();
actionListener();
}
public void components()
{
this.update = new JButton("update");
this.textField=new JTextField(20);
this.textField.setText("hello");
this.textLabel= new JLabel("GUI");
this.font="Arial";
this.size="20";
this.textLabel.setFont(new Font(this.font, Font.PLAIN, Integer.parseInt(this.size)));
this.fontBox=new JComboBox();
this.fontBox.addItem("Times New Roman");
this.fontBox.addItem("Calibri");
this.sizeBox= new JComboBox();
this.sizeBox.addItem("20");
this.sizeBox.addItem("30");
this.sizeBox.addItem("40");
this.setSize(400, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
}
public void panels(){
JPanel northPanel =new JPanel();
JLabel fontLabel =new JLabel("Font: ");
JLabel sizeLabel =new JLabel("size: ");
northPanel.add(fontLabel);
//center
BGPanel centerPanel =new BGPanel();
centerPanel.add(this.textLabel);
this.add(centerPanel,BorderLayout.CENTER);
//south
BGPanel southPanel =new BGPanel();
southPanel.add(this.textLabel);
this.add(southPanel,BorderLayout.CENTER);
}
public void actionListener(){
this.update.addActionListener(this);
this.fontBox.addActionListener(this);
this.sizeBox.addActionListener(this);
}
#Override
public void actionPerformed(ActionEvent e){
if(e.getSource()==this.update){
this.textLabel.setText(this.textField.getText());
}
if(e.getSource()==this.fontBox || e.getSource()==this.sizeBox){
this.font=this.fontBox.getSelectedItem().toString();
this.size=this.sizeBox.getSelectedItem().toString();
this.textLabel.setFont(new Font(this.font,Font.PLAIN,Integer.parseInt(this.size)));
}
this.repaint();
}
public static void main(String[] args) {
ComboGUI comb =new ComboGUI();
combo.setVisible(true);
}
}
this is what im getting instead
It looks like you are missing this.setVisible(true); at the end of your constructor.
Your code should look like this:
public ComboGUI()
{
components();
panels();
actionListener();
this.setVisible(true);
}
incase anyone is trying to do something similar, this is the complete solution
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
public class ComboGUI extends JFrame implements ActionListener{
public void updateLabelText(){
size = fSize.getItemAt(fSize.getSelectedIndex());
font = fStyles.getItemAt(fStyles.getSelectedIndex());
updateLabel.setFont(new Font(font,Font.PLAIN,size));
}
public static BGPanel centrePanel;
public JComboBox<String> fStyles;
public JComboBox<Integer> fSize;
public JButton updateButton;
public JLabel updateLabel;
public JLabel fontLabel;
public JLabel sizeLabel;
public JTextField textField;
public JPanel BottomPanel;
public JPanel topPanel;
private String font;
private int size;
public ComboGUI() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(400,400);
this.setLocation(0, 0);
//Top
topPanel = new JPanel();
fontLabel = new JLabel("Font:");
sizeLabel = new JLabel("Font Size:");
fStyles = new JComboBox<String>();
fStyles.addItem("Arial");
fStyles.addItem("TimesRoman");
fStyles.addItem("Serif");
fStyles.addItem("Monospaced");
fStyles.addActionListener(this);
fSize = new JComboBox<Integer>();
fSize.addItem(10);
fSize.addItem(20);
fSize.addItem(30);
fSize.addItem(40);
fSize.addActionListener(this);
topPanel.add(fontLabel);
topPanel.add(fStyles);
topPanel.add(sizeLabel);
topPanel.add(fSize);
//CentrePanel setup
centrePanel = new BGPanel();
updateLabel = new JLabel("I love PDC :)");
centrePanel.add(updateLabel);
//Bottom
BottomPanel = new JPanel();
updateButton = new JButton("Update");
textField = new JTextField(20);
textField.setText("I love PDC :)");
updateButton.addActionListener(this);
BottomPanel.add(textField);
BottomPanel.add(updateButton);
this.add(centrePanel,BorderLayout.CENTER);
this.add(BottomPanel,BorderLayout.SOUTH);
this.add(topPanel,BorderLayout.NORTH);
updateLabelText();
}
public static void main(String[] args) {
ComboGUI combo = new ComboGUI();
combo.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == updateButton){
updateLabel.setText(textField.getText().trim());
}
if(e.getSource() == fStyles){
updateLabelText();
}
if (e.getSource() == fSize){
updateLabelText();
}
}
}
So I have created two buttons and I want to do a specific task when the buttons are clicked. If button 1 (b1) is clicked using the ActionListener, I want to create an object of Van and display the instance variables in a JTextarea or JTable. For example if Van button is clicked then the action would be to create an object of Van and get the instance variable values and print them in a JTextArea/JTable. Below is my code so far:
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
public class TestButton extends JFrame{
JTable table;
public TestButton (){
setLayout(new FlowLayout());
}
static class ActionTwo implements ActionListener{
#Override
public void actionPerformed (ActionEvent evt){
Vehicle sport = new Sportcar (200, 1500, 220);
}
}
static class Action implements ActionListener{
#Override
public void actionPerformed (ActionEvent evt){
Vehicle aVan = new Van(100,0.9,3500,160.4);
}
}
public static void main (String [] args){
JFrame frame = new JFrame ("Type of Vehicle");
frame.setVisible(true);
frame.setSize(400,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setBackground(Color.black);
JButton b1 = new JButton("Van");
JButton b2 = new JButton("Sports Car");
panel.add(b1);
panel.add(b2);
frame.add(panel);
b1.addActionListener(new Action());
b2.addActionListener(new ActionTwo());
}
}
Have a look at the Java tutorial on Action Listeners
This will do what you would like it to do, but you should read through that tutorial to get a full grasp of what's happening.
public static void main (String [] args){
JTextField text = new JTextField();
ActionListener textSetter = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JButton clicked = (JButton) e.getSource();
text.setText(clicked.getText());
}
};
JButton btnVan = new JButton("Van");
btnVan.addActionListener(textSetter);
JButton btnCar = new JButton("Sports Car");
btnCar.addActionListener(textSetter);
JPanel btnPanel = new JPanel();
btnPanel.add(btnVan);
btnPanel.add(btnCar);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(text, BorderLayout.NORTH);
mainPanel.add(btnPanel, BorderLayout.SOUTH);
JFrame frame = new JFrame ("Type of Vehicle");
frame.add(mainPanel);
frame.setSize(400,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
I have a very simple code that creates a frame object from the class MyJFrame accepts the first string which is used as a title. Place the second string is the text to be displayed in a JScrollPane. You can see the code below. What I need is to use copy and paste of text highlighted. I need help implementing it. So that if copy selected from a menubar it copies the highlighted portion and if paste is pastes it.
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.Container;
import javax.swing.JOptionPane;
public class DisplayText
{
private static JTextArea text;
public DisplayText(String title, String info)
{
MyJFrame f = new MyJFrame(title);
Container c = f.getContentPane();
//default text
text = new JTextArea(info);
//Scrollpane
JScrollPane sp = new JScrollPane(text);
c.add( sp );
f.setBounds(100,200, 500, 400 );
f.setVisible(true);
}
Use the Actions that are available in the DefaultEditorKit including DefaultEditorKit.CopyAction, DefaultEditorKit.CutAction, and DefaultEditorKit.PasteAction.
For example:
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.text.*;
public class TestActions {
private String[] texts = {
"Hello", "Goodbye", "What the f***?", "Heck if I know", "Peace out man!"
};
private JTextArea textArea = new JTextArea(10, 30);
private Action[] textActions = { new DefaultEditorKit.CutAction(),
new DefaultEditorKit.CopyAction(), new DefaultEditorKit.PasteAction(), };
private JPanel mainPanel = new JPanel();
private JMenuBar menubar = new JMenuBar();
private JPopupMenu popup = new JPopupMenu();
private PopupListener popupListener = new PopupListener();
public TestActions() {
JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 5));
JMenu menu = new JMenu("Edit");
for (Action textAction : textActions) {
btnPanel.add(new JButton(textAction));
menu.add(new JMenuItem(textAction));
popup.add(new JMenuItem(textAction));
}
menubar.add(menu);
JPanel textFieldPanel = new JPanel(new GridLayout(0, 1, 5, 5));
for (String text: texts) {
JTextField textField = new JTextField(text, 15);
textField.addMouseListener(popupListener);
textFieldPanel.add(textField);
textField.addFocusListener(new FocusAdapter() {
#Override
public void focusGained(FocusEvent e) {
((JTextComponent)e.getSource()).selectAll();
}
});
}
textArea.addMouseListener(popupListener);
JScrollPane scrollPane = new JScrollPane(textArea);
JPanel textFieldPanelWrapper = new JPanel(new BorderLayout());
textFieldPanelWrapper.add(textFieldPanel, BorderLayout.NORTH);
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
mainPanel.setLayout(new BorderLayout(5, 5));
mainPanel.add(btnPanel, BorderLayout.NORTH);
mainPanel.add(scrollPane, BorderLayout.CENTER);
mainPanel.add(textFieldPanelWrapper, BorderLayout.EAST);
}
public JComponent getMainPanel() {
return mainPanel;
}
private JMenuBar getMenuBar() {
return menubar;
}
private class PopupListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
maybeShowPopup(e);
}
public void mouseReleased(MouseEvent e) {
maybeShowPopup(e);
}
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
popup.show(e.getComponent(),
e.getX(), e.getY());
}
}
}
private static void createAndShowGui() {
TestActions testActions = new TestActions();
JFrame frame = new JFrame("Test Actions");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(testActions.getMainPanel());
frame.setJMenuBar(testActions.getMenuBar());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
code borrowed from my answer here.
Edit
You ask in comment:
I appreciate the answer. However, could you make it a bit simpler to understand, I am fairly new to Java.
Sure, here is a simple JMenuBar that holds an edit JMenu that holds JMenuItems for copy, cut, and paste with just that code borrowed from my example. Note that as an aside, you should not setBounds on anything, you should instead set the rows and columns of your JTextArea, and that you should not use a static JTextArea, and in fact no Swing components should ever be static.
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import java.awt.Container;
import javax.swing.JOptionPane;
import javax.swing.text.DefaultEditorKit;
public class DisplayText {
private JTextArea text;
private Action[] textActions = { new DefaultEditorKit.CutAction(),
new DefaultEditorKit.CopyAction(), new DefaultEditorKit.PasteAction(), };
public DisplayText(String title, String info) {
JMenu menu = new JMenu("Edit");
for (Action textAction : textActions) {
menu.add(new JMenuItem(textAction));
}
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
JFrame f = new JFrame(title);
f.setJMenuBar(menuBar);
Container c = f.getContentPane();
text = new JTextArea(info, 20, 50);
JScrollPane sp = new JScrollPane(text);
c.add(sp);
// f.setBounds(100,200, 500, 400 );
f.pack();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
new DisplayText("Title", "This is info text");
}
}
everytime i click on a JList item, i need to clear + refresh my current panel & load another panel, returned via method 'populateWithButtons()'. temp is an int variable that stores what was clicked at the JList. How do i rectify the following?
list_1.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent evt) {
//refresh + populate JPanel
Food food = new Food();
JPanel panel2 = new JPanel();
JPanel pane11 = new JPanel();
panel2.add(panel1);
panel1.validate();
panel1.repaint();
panel1.setBounds(153, 74, 281, 269);
panel1.add(food.populateWithButtons(temp));
contentPane.add(panel2);
}
don't to use NullLayout
add ListSelectionListener to JList instead of MouseListener, otherwise you would need to convert point from mouse to Item in JList
use CardLayout instead of add, remove JPanels on runtime, then selection from ListSelectionListener (ListSelectionModel to SINGLE...) to switch prepared card (JPanel with some contents)
EDIT
.
.
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
public class CardlayoutTest {
private Color[] colors = new Color[]{Color.BLACK, Color.RED, Color.GREEN, Color.BLUE};
private JFrame frame = new JFrame();
private JList list = new JList();
private JPanel panel = new JPanel();
private CardLayout card = new CardLayout();
public CardlayoutTest() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setLayout(card);
Vector<String> items = new Vector<String>();
for (int x = 0; x < colors.length; x++) {
JPanel pnl = new JPanel(new BorderLayout());
pnl.setBackground(colors[x]);
panel.add(pnl, colors[x].toString());
items.add(colors[x].toString());
}
list = new JList(items);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
String card = list.getSelectedValue().toString();
CardLayout cL = (CardLayout) (panel.getLayout());
cL.show(panel, card);
}
}
});
frame.add(new JScrollPane(list), BorderLayout.WEST);
frame.add(panel);
frame.setPreferredSize(new Dimension(400, 150));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new CardlayoutTest();
}
});
}
}
move validate() and repaint() after adding to contentPane as in that point it will be redrawed.
I wnat add to JFrame JSrollPane. ScrollPane contains a JPanels. But I have problem when I add first JPanel to ScrollPane i see nothing when I add JPanel to JFrame I see JPanels. So where I make mistake? Here code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class AddingJPanels {
public static void main(String... args) {
JFrame jF = new JFrame();
PanelMain pM = new PanelMain();
Panel p = new Panel("sas");
JPanel jp = makeJPanel(10);
p.setPreferredSize(new Dimension(600,600));
JScrollPane scroll = new JScrollPane();
scroll.add(jp);
JScrollBar verticalPane = scroll.getVerticalScrollBar();
verticalPane.setValue(verticalPane.getMinimum());
verticalPane.setValue(20);
//scroll.setPreferredSize(new Dimension(570, 300));
scroll.setPreferredSize(new Dimension(400,500));
pM.add(scroll);
//JTabbedPane tB = new JTabbedPane();
//tB.addTab(":]", null, pM, "Tab 3");
jF.add(jp);
jF.setSize(new Dimension(500,500));
jF.setVisible(true);
}
static JPanel makeJPanel(int i){
JPanel jPl = new JPanel();
jPl.setLayout(new GridLayout(i,0));
JLabel lebel;
for(int j=0;j<i;++j){
JPanel p = new JPanel();
p.setLayout(new GridLayout(2,2));
JButton b = new JButton("asa");
p.add(b);
p.setBorder(BorderFactory.createLineBorder(Color.black));
p.setPreferredSize(new Dimension(400,400));
lebel = new JLabel("Napis: "+j);
p.add(lebel);
JTextField jTF = new JTextField("Nic",20);
p.add(jTF);
jPl.add(p);
}
return jPl;
}
}
class Frame extends JFrame {
public Frame() {
super("Frame");
this.setPreferredSize(new Dimension(200, 200));
}
public void see() {
this.setVisible(true);
}
}
class PanelMain extends JPanel {
JButton b = new JButton("press me");
public PanelMain() {
this.add(b);
b.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println("Pressed");
}
});
}
}
class Panel extends JPanel {
JLabel l;
public Panel(String s) {
l = new JLabel(s);
this.add(l);
}
}
When I make jF.add(scroll) is no effect.
add() doesn't work on a JScrollPane. You need to use setViewport() or else pass a component in the contstructor.
JScrollPane scroll = new JScrollPane(jp);
or
JScrollPane scroll = new JScrollPane();
scroll.setViewport(jp);
JScrollPane scroll = new JScrollPane(jp);
use:
scroll.setViewportView(jp);