My Java code below is trying to import a photo to place on new JLabel(new ImageIcon("")); using File Chooser. I have no experience with file Chooser but I want to select a image and then have the image place on the jLabel. I don't know what to put in action event e that will help me achieve this goal.
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class back extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
public int act = 0;
public back(){
setTitle("Question");
JPanel Panel1 = new JPanel();
Panel1.setLayout(new BorderLayout());
JPanel Panel2 = new JPanel();
Panel2.setLayout(new GridLayout(3,1));
JLabel myButton1 = new JLabel(new ImageIcon(""));
JButton myButton2 = new JButton("2:Select Image");
myButton2.addActionListener(this);
Panel2.add(myButton1);
Panel2.add(myButton2);
Panel1.add(Panel2,BorderLayout.CENTER);
add(Panel1);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
SomeImageType image = findImage();
displayImage(image, Panel1);
}
public static void main(String[] args) {
new back();
}
}
Related
I am creating a JFrame holding a JPanel(p1) and a JButton(b1)....p1 contains another JPanel(sub1) holding a JLabel(l1)...after i Click on b1,i want a String to be printed in the JLabel...but it is not printing anything.
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class panAccDel implements ActionListener {
JFrame frame = new JFrame();
JButton b1;
JLabel l1;
String intro = "Hello there";
JPanel sub1,p1;
public static void main(String[] args) {
panAccDel panAccDel = new panAccDel();
}
public panAccDel(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p1 = new JPanel();
b1 = new JButton("Print Data");
sub1 = new JPanel();
p1.add(sub1);
p1.add(b1);
p1.setLayout(new GridLayout(2,1));
sub1.setBackground(Color.RED);
l1 = new JLabel();
sub1.add(l1);
frame.add(p1);
frame.setVisible(true);
frame.setSize(700,700);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == b1){
l1.setText(intro);
}
}
}
This is a trial Code.
Any Suggestions???
The listener needs added to the button. For example:
b1.addActionListener(this);
I am attempting to add components to a JFrame but the only thing being displayed is my ImageIcon when I run GameMenu.java. I have instantiated setVisible(); specifically after I have set my frame or added components to the panels or menubars. So I'm unsure as to why no components are showing up. I think it may have something to do with my formatting or main method.
Here are my two classes:
GameMenu.java:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
public class GameMenu{
public static void main(String[] args) {
FrameCaller obj = new FrameCaller();
}
}
class FrameCaller extends JFrame {
public FrameCaller(){
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new JLabel(new ImageIcon("logo.png")));
pack();
setLocationRelativeTo(null);
JMenuBar mb = new JMenuBar();
JMenu m1 = new JMenu("Game List");
JMenu m2 = new JMenu("Help");
JMenu m3 = new JMenu("Stats");
mb.add(m1);
mb.add(m2);
mb.add(m3);
JMenuItem showRulesButton = new JMenuItem("View game rules");
m2.add(showRulesButton);
JMenuItem m77 = new JMenuItem("View past game stats");
m3.add(m77);
mb.setVisible(true);
JPanel panel = new JPanel();
JButton newGameButton = new JButton("New Game");
newGameButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new inGameFrame();
dispose();
}
});
panel.add(newGameButton);
panel.setVisible(true);
setVisible(true);
}
}
EightOff.java:
import javax.swing.*;
public class EightOff {
public static void main(String[] args)
{
inGameFrame obj = new inGameFrame();
}
}
class inGameFrame extends JFrame
{
public inGameFrame() {
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
}
Any tips would be wonderful. Thanks.
So i have three panels that i have three different buttons for to change them each to their respective colors. I need to add a fourth button that will return all three panels to their original default light gray color. I add this "reset" button and it only changes the first panel back. What am i doing wrong?
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class PanelDemo extends JFrame implements ActionListener
{
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
private JPanel redPanel;
private JPanel whitePanel;
private JPanel bluePanel;
public static void main(String[] args)
{
PanelDemo gui = new PanelDemo();
gui.setVisible(true);
}
public PanelDemo()
{
super("Panel Demonstration");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel biggerPanel = new JPanel();
biggerPanel.setLayout(new GridLayout(1, 3));
redPanel = new JPanel();
redPanel.setBackground(Color.LIGHT_GRAY);
biggerPanel.add(redPanel);
whitePanel = new JPanel();
whitePanel.setBackground(Color.LIGHT_GRAY);
biggerPanel.add(whitePanel);
bluePanel = new JPanel();
bluePanel.setBackground(Color.LIGHT_GRAY);
biggerPanel.add(bluePanel);
add(biggerPanel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(Color.LIGHT_GRAY);
buttonPanel.setLayout(new FlowLayout());
JButton redButton = new JButton("Red");
redButton.setBackground(Color.RED);
redButton.addActionListener(this);
buttonPanel.add(redButton);
JButton whiteButton = new JButton("White");
whiteButton.setBackground(Color.WHITE);
whiteButton.addActionListener(this);
buttonPanel.add(whiteButton);
JButton blueButton = new JButton("Blue");
blueButton.setBackground(Color.BLUE);
blueButton.addActionListener(this);
buttonPanel.add(blueButton);
JButton resetButton = new JButton("Reset");
resetButton.setBackground(Color.LIGHT_GRAY);
resetButton.addActionListener(this);
buttonPanel.add(resetButton);
add(buttonPanel, BorderLayout.SOUTH);
}
#Override
public void actionPerformed(ActionEvent e)
{
String buttonString = e.getActionCommand();
if (buttonString.equals("Red"))
redPanel.setBackground(Color.RED);
else if (buttonString.equals("White"))
whitePanel.setBackground(Color.WHITE);
else if (buttonString.equals("Blue"))
bluePanel.setBackground(Color.BLUE);
else if (buttonString.equals("Reset"))
redPanel.setBackground(Color.LIGHT_GRAY);
else if (buttonString.equals("Reset"))
bluePanel.setBackground(Color.LIGHT_GRAY);
else if (buttonString.equals("Reset"))
whitePanel.setBackground(Color.LIGHT_GRAY);
else
System.out.println("Unexpected error.");
}
}
Here was your problem. You had if else's on each panel for the reset. Compare the code below to what you have. It was just a simple logic issue.
public void actionPerformed(ActionEvent e) {
String buttonString = e.getActionCommand();
if (buttonString.equals("Red"))
redPanel.setBackground(Color.RED);
else if (buttonString.equals("White"))
whitePanel.setBackground(Color.WHITE);
else if (buttonString.equals("Blue"))
bluePanel.setBackground(Color.BLUE);
else if (buttonString.equals("Reset")) {
redPanel.setBackground(Color.LIGHT_GRAY);
bluePanel.setBackground(Color.LIGHT_GRAY);
whitePanel.setBackground(Color.LIGHT_GRAY);
}
else
System.out.println("Unexpected error.");
And a couple of suggestions.
Don't extend JFrame. Just use an instance of it. It's better technique.
Put the following as the last statement in your constructor. It will center the panel on your screen.
setLocationRelativeTo(null);
// or when using a frame instance.
frame.setLocationRelativeTo(null);
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);
As i am new to java swing, i am finding a little difficulty in integrating the JFileChooser with JList. My goal is to select a file from the dialog-box(JFileChooser) and click 'add' so that it gets added to the JList automatically and the same mechanism with 'remove'. I tried going through a few tutorials and a few hints but it dint work. It would be really great if any of you could help me with this step.
Thanks in advance..!!
package examples;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JSplitPane;
//import javax.swing.SwingConstants;
class SplitPane extends JFrame
{
private static final long serialVersionUID = 1L;
private JSplitPane splitPaneV;
private JSplitPane splitPaneH;
private JLayeredPane panel1;
private JPanel panel2;
private JPanel panel3;
private JButton add;
private JButton remove;
private JScrollBar scrollBar;
private JList list;
public SplitPane()
{
setTitle("AdditionalLoaderInformation");
setBackground(Color.blue);
JPanel topPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
topPanel.setPreferredSize(new Dimension(700, 500));
getContentPane().add(topPanel);
// Create the panels
createPanel1();
createPanel2();
createPanel3();
// Create a splitter pane
splitPaneV = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
topPanel.add(splitPaneV, BorderLayout.CENTER);
splitPaneH = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPaneH.setLeftComponent(panel1);
splitPaneH.setRightComponent(panel2);
splitPaneV.setLeftComponent(splitPaneH);
splitPaneV.setRightComponent(panel3);
scrollBar = new JScrollBar();
scrollBar.setOrientation(JScrollBar.HORIZONTAL);
panel3.add(scrollBar, BorderLayout.SOUTH);
list = new JList();
panel3.add(list, BorderLayout.CENTER);
}
public void createPanel1()
{
panel1 = new JLayeredPane();
panel1.setLayout(new BorderLayout());
}
public void createPanel2()
{
panel2 = new JPanel();
add = new JButton("ADD");
final JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
add.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
************************************
}
});
panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
panel2.add(add);
remove = new JButton("REMOVE");
remove.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
removeActionPerformed(e);
}
private void removeActionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
panel2.add(remove);
}
public void createPanel3()
{
panel3 = new JPanel();
panel3.setLayout(new BorderLayout());
panel3.setPreferredSize(new Dimension(400, 100));
panel3.setMinimumSize(new Dimension(100, 50));
final JFileChooser fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(true);
//fileChooser.showOpenDialog(fileChooser);
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser .setDialogTitle("OPEN");
panel3.add(fileChooser, BorderLayout.NORTH);
//fileChooser.addActionListener(new ActionListener()
// {
// public void actionPerformed(ActionEvent e)
//{
// }
//});
}
public static void main(String args[]) {
// Create an instance of the test application
SplitPane mainFrame = new SplitPane();
mainFrame.pack();
mainFrame.setVisible(true);
}
}
When you get a new file name in your chooser's action listener, shown here, add it to (or remove it from) the list's models, as shown in this example.
Addendum: To display the file's content in the JList, you'll need to create a suitable renderer using one of the text components.
Read the JFileChooser tutorial
If the above step is not sufficient, take a look at the class javadoc of JFileChooser and note the APPROVE_OPTION and the getSelectedFile method. This should allow you to obtain the file
Read the JList tutorial
If the above step is not sufficient, take a look at the available API of JList and ListModel, and more in particular the default implementation DefaultListModel which contains add and remove methods