a button that takes to a new panel - java

I created a button that is in the main page which when a user clicks on it، it changes the panal ( the main idea is that it changes the background and all whats on the panel and adds new stuff to it ) however i failed!, i also failed in adjusting the location of the button although i tried button.setBounse(..)
anyhow can someone help me in those two things?
public class mainClass {
private static JButton start;
static BackgroundPanel bp = null;
static JFrame mainf = null;
public static void main(String[] args) throws IOException {
mainf = new JFrame ("سين جيم");
// background
BufferedImage mFrame = ImageIO.read(new File("B1.png"));
bp = new BackgroundPanel(mFrame);
mainf.add(bp);
bp.setLayout(new GridBagLayout());
// Hi string
JLabel hi = new JLabel ("أهلا وسهلا");
Font fs = hi.getFont();
hi.setFont(fs.deriveFont(50f));
bp.add(hi);
// button
JPanel another = new JPanel();
start = new JButton ( " لنبدأ");
bp.add(start);
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bp.removeAll();
BufferedImage mFrame2= null;
try {
mFrame2 = ImageIO.read(new File("B2.png"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
bp = new BackgroundPanel(mFrame2);
bp.setLayout(new GridBagLayout());
JLabel hi1= new JLabel ("worked");
bp.add(hi1);
}
} );
// end of frame
mainf.pack();
mainf.setVisible(true);
}
}

Well, to change your main panel you don't have to create a new Frame object and set it's main panel. E.g., you could write a setter for bp and invoke this method in your action listener method. If your panel has a different size, you can easily change the frame's size...

Related

JFrame setContentPane hides other components

So I'm going into GUI's in Java, and am trying to create a simple main menu for a timer. All is well until I've attempted to add a background for the GUI. Adding the background works, however all other components are now gone, (the button). How could I fix this?
EDIT: Here is my new code.
public class MainMenu {
// JFrame = the actual menu / frame.
private JFrame frame;
// JLabel = provides text instructions or information on a GUI —
// display a single line of read-only text, an image or both text and an image.
private JLabel background;
// JButton = button.
private JButton alarmClockButton;
// Constructor to create menu
public MainMenu() {
frame = new JFrame("Alarm Clock");
alarmClockButton = new JButton("Timer");
// Add an event to clicking the button.
alarmClockButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO: CHANGE TO SOMETHING NICER
JOptionPane.showMessageDialog(null, "This feature hasn't been implemented yet.", "We're sorry!",
JOptionPane.ERROR_MESSAGE);
}
});
// Creating the background
try {
background = new JLabel(new ImageIcon(ImageIO.read(getClass()
.getResourceAsStream("/me/devy/alarm/clock/resources/Background.jpg"))));
} catch (IOException e) {
e.printStackTrace();
}
frame.setLayout(new FlowLayout());
frame.setContentPane(background);
frame.add(alarmClockButton);
frame.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
frame.setVisible(true);
frame.setSize(450, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
alarmClockButton.setForeground(Color.RED);
}
}
Thank you!
frame.setContentPane(background);
You use the label as the content pane. The problem is that the label doesn't use a layout manager by default.
You need to add:
background.setLayout( new BorderLayout() ); // or whatever layout you want
frame.setContentPane(background);
Now you can add the button directly to the frame. You don't need the panel.
Or if you want to get fancy you can use the Background Panel which gives you the option to scale or tile the background image.
Instead of making the ContentPane as JLabel, you can wrap the JLabel in a JPanel, then add this JPanel as the ContentPane :
public class MainMenu {
public static void main(String[] args) {
new MainMenu();
}
// JFrame = the actual menu / frame.
private JFrame frame;
private JPanel panel;
private JPanel bkgPanel;
// JLabel = provides text instructions or information on a GUI —
// display a single line of read-only text, an image or both text and an
// image.
private JLabel background;
// JButton = button.
private JButton alarmClockButton;
// Constructor to create menu
public MainMenu() {
frame = new JFrame("Alarm Clock");
panel = new JPanel();
bkgPanel = new JPanel();
alarmClockButton = new JButton("Timer");
// Add an event to clicking the button.
alarmClockButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// TODO: CHANGE TO SOMETHING NICER
JOptionPane.showMessageDialog(null, "This feature hasn't been implemented yet.", "We're sorry!",
JOptionPane.ERROR_MESSAGE);
}
});
// Creating the background
try {
background = new JLabel(new ImageIcon(
ImageIO.read(getClass().getResourceAsStream("/me/devy/alarm/clock/resources/Background.jpg"))));
bkgPanel.add(background);
} catch (IOException e) {
e.printStackTrace();
}
frame.setContentPane(bkgPanel);
frame.add(panel);
panel.add(alarmClockButton);
frame.setVisible(true);
frame.setSize(450, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
alarmClockButton.setForeground(Color.RED);
}
}

Displaying an image, Java

I'm new to Java GUI, and am having issues displaying an image. My intention is to display a large image and allow the user to click on regions of the image to indicate where certain features are located. Anyway, I'm getting a rough start because I can't even get the image to appear, despite reading Oracle's explanation and other solutions.
I've created a JFrame and used its setContentPane() method to add a JPanel and JLabel. I use the setIcon() method of the JLabel to add an image to it, or at least that's my intention...
Any advice is appreciated, especially if there's a better way of doing this. I'll be using OpenCV to process images, and plan to convert them to Java image (or BufferedImage) before displaying them.
Here is the code. I left out the libraries to save space.
public class Pathology {
public static void main(String[] args) {
PrimaryFrame primaryFrame = new PrimaryFrame();
primaryFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
primaryFrame.setSize(1500, 900);
primaryFrame.setVisible( true );
primaryFrame.setContentPane(primaryFrame.getGui());
try {
primaryFrame.setImage(ImageIO.read(new File("C:\\Users\\Benjamin\\Pictures\\Pathology\\C\\001.png")));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
GUI Class:
public class PrimaryFrame extends JFrame{
//private JTextField textField1;
JPanel gui;
JLabel imageCanvas;
public PrimaryFrame() {
super( "Pathology-1" );
//setLayout(new FlowLayout());
//textField1 = new JTextField("Chup!", 50);
//add(textField1);
}
public void setImage(Image image) {
imageCanvas.setIcon(new ImageIcon(image));
}
public void initComponents() {
if (gui==null) {
gui = new JPanel(new BorderLayout());
gui.setBorder(new EmptyBorder(5,5,5,5));
imageCanvas = new JLabel();
JPanel imageCenter = new JPanel(new GridBagLayout());
imageCenter.add(imageCanvas);
JScrollPane imageScroll = new JScrollPane(imageCenter);
imageScroll.setPreferredSize(new Dimension(300,100));
gui.add(imageScroll, BorderLayout.CENTER);
}
}
public Container getGui() {
initComponents();
return gui;
}
}
Would you laugh at me if I'd tell you that you just have to put the primaryFrame.setVisible( true ); to the end of the main method? :)
For furture understanding, you don't have to call frame.setVisible(true) every time you want to add/update something in the frame (in an ActionListener, for example). Instead you can call frame.revalidate() and frame.repaint(). (Where frame can be replaced with the particular panel)
You need to setVisible(true) after the call to setImage():
primaryFrame.setImage(ImageIO.read(new
File("C:\\Users\\Benjamin\\Pictures\\Pathology\\C\\001.png")));
because any update to the GUI after setVisible() will not be shown.
That's it and the code should be like this:
public class Pathology {
public static void main(String[] args) {
PrimaryFrame primaryFrame = new PrimaryFrame();
primaryFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
primaryFrame.setSize(1500, 900);
primaryFrame.setContentPane(primaryFrame.getGui());
try {
primaryFrame.setImage(ImageIO.read(new File(
"C:\\Users\\Benjamin\\Pictures\\Pathology\\C\\001.png")));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
primaryFrame.setVisible( true );
}
}

How does this update a component without using an actionListener?

Code in Question
There isn't an actionListener for the image thumbnails, yet when clicked they update the image.
From this webpage.
Edit: I am currently importing images using JFileChooser and then creating a thumbnail and displaying the full image in a similar way to this, although not using ImageIcons. But would like to use this method so when I add an image it adds to the list and allows me to click the thumbnail to show that image.
However mine using actionListeners to change when something is pressed but this doesn't and can't understand the code where it does.
Thanks
Edit2:
Regarding the repaint option:
I have a class which extends component which then calls a repaint function.
public class Image extends Component {
private BufferedImage img;
//Print Image
public void paint(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}
I then have a class with all my Swing components which call methods from other classes.
Image importedImage = new Image(loadimageone.openFile());
Image scaledImage = new Image();
// Save image in Buffered Image array
images.add(importedImage.getImg());
// Display image
imagePanel.removeAll();
imagePanel.add(importedImage);
imagePanel.revalidate();
imagePanel.repaint();
previewPanel.add(scaledImage);
previewPanel.revalidate();
previewPanel.repaint();
If I remove the revalidate or repaint it wont' update the image on the screen.
Edit 3:
This is the code on how I implemented the dynamic buttons:
//Create thumbnail
private void createThumbnail(ImpImage image){
Algorithms a = new Algorithms();
ImpImage thumb = new ImpImage();
//Create Thumbnail
thumb.setImg(a.shrinkImage(image.getImg(), 75, 75));
//Create ImageIcon
ImageIcon icon = new ImageIcon(thumb.getImg());
//Create JButton
JButton iconButton = new JButton(icon);
//Create ActionListener
iconButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
bottomBarLabel.setText("Clicked");
imagePanel.removeAll();
imagePanel.add(images.get(position)); //Needs Fixing
imagePanel.revalidate();
}
});
//Add to previewPanel
previewPanel.add(iconButton);
previewPanel.revalidate();
previewPanel.repaint();
}
It looks like it uses ThumbnailAction instead which extends AbstractAction (at the very bottom of the code). Swing components can use Actions instead of ActionListeners. The advantage of Actions is that buttons can share an Action and they will automatically use the same key-bindings etc.
http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html
EDIT: I have added some code demonstrating that you do not need to explicitly repaint(). Give it a try.
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setSize(200, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(2, 1));
final JLabel iconLabel = new JLabel();
JButton button = new JButton("Put Image");
button.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent arg0) {
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
iconLabel.setIcon(new ImageIcon(ImageIO.read(fc.getSelectedFile())));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
panel.add(iconLabel);
panel.add(button);
frame.add(panel);
frame.setVisible(true);
}
EDIT 2 (There is no Edit 2)
EDIT 3: Try this
public class MyActionListener implements ActionListener {
private JPanel imagePanel;
private Image image;
public MyActionListener(JPanel imagePanel, Image image) {
this.imagePanel = imagePanel;
this.image = image;
}
#Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("Clicked");
imagePanel.removeAll();
imagePanel.add(image); //Needs Fixing
imagePanel.revalidate();
}
}

JFrame not staying displayed - java

I have a program that does several thing and displays different JFrames after several actions. When I launch the first JFrame from the main, it all goes ok, but When I launch it from another class different from the main class, it doesn't shows up.
What is the point? What am I doing wrong?
Here's some code:
This is called from the main:
SwingUtilities.invokeLater(new Runnable() {
PdfFileUtils pfu = new PdfFileUtils(path);
public void run() {
try {
PdfToImg.setup(pfu, null);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
And it works.
And this is called from another class that is used after some operations:
pfu.setPath(SIGNED);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
PdfToImg.setup(pfu, data);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
Sometimes (every 4 or 5 executions), it launches interrupted exception.
I also tried launching the second frame in this way:
pfu.setPath(SIGNED);
try {
PdfToImg.setup(pfu, data);
} catch (IOException ex) {
ex.printStackTrace();
}
But it shows up for a second and than disappears.
EDIT :
This is the setup() method:
public static void setup(PdfFileUtils pfu, BiometricData data) throws IOException {
// load a pdf from a byte buffer
File file = new File(pfu.getPath());
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0,
channel.size());
PDFFile pdffile = new PDFFile(buf);
int numPgs = pdffile.getNumPages();
ImageIcon[] images = new ImageIcon[numPgs];
for (int i = 0; i < numPgs; i++) {
// draw the first page to an image
PDFPage page = pdffile.getPage(i + 1);
// get the width and height for the doc at the default zoom
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox()
.getWidth(), (int) page.getBBox().getHeight());
// generate the image
Image img = page.getImage(rect.width, rect.height, rect, null,
true, true);
pfu.setWidth(rect.width);
pfu.setHeight(rect.height);
// save it on an array
images[i] = new ImageIcon(img);
}
if(data != null){
SignedFileDisplay fileDisplay = new SignedFileDisplay(pfu, data);
fileDisplay.DisplayAndSelect(images);
} else{
SignPosition signPos = new SignPosition(pfu);
signPos.DisplayAndSelect(images);
}
raf.close();
}
The JFrames are launched by SignedFileDisplay(pfu, data) and by SignPosition(pfu). They work both if launched by the main, and no one the second time.
The constructors are:
public SignPosition(PdfFileUtils pfutils) {
pfu = pfutils;
// scale dimensions
width = (int) (scale * pfu.getWidth());
height = (int) (scale * pfu.getHeight());
// sets the frame appearance
sp.setSize(width + 8, height + 68);
sp.setVisible(true);
sp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sp.setLocationRelativeTo(null);
// Add the image gallery panel
add(imageGallery, BorderLayout.PAGE_START);
// creates the JButtons objects for each button
JButton FIRST = new JButton("|<<<");
JButton PREVIOUS = new JButton("< Prev");
JButton OK = new JButton("Ok");
JButton NEXT = new JButton("Next >");
JButton LAST = new JButton(">>>|");
// adds the buttons to the button panel
JPanel buttons = new JPanel();
buttons.setLayout(new GridLayout(1, 4));
buttons.add(FIRST);
buttons.add(PREVIOUS);
buttons.add(OK);
buttons.add(NEXT);
buttons.add(LAST);
// add buttons on the bottom of the frame
add(buttons, BorderLayout.SOUTH);
// register listener
FirstButtonListener FirstButton = new FirstButtonListener();
PreviousButtonListener PreviousButton = new PreviousButtonListener();
OkButtonListener OkButton = new OkButtonListener();
NextButtonListener NextButton = new NextButtonListener();
LastButtonListener LastButton = new LastButtonListener();
// add listeners to corresponding componenets
FIRST.addActionListener(FirstButton);
PREVIOUS.addActionListener(PreviousButton);
OK.addActionListener(OkButton);
NEXT.addActionListener(NextButton);
LAST.addActionListener(LastButton);
}
and
public SignedFileDisplay(PdfFileUtils pfutils, BiometricData bd) {
data = bd;
pfu = pfutils;
// scale dimensions
width = (int) (scale * pfu.getWidth());
height = (int) (scale * pfu.getHeight());
// sets the frame appearance
sfd.setSize(width + 8, height + 68);
sfd.setVisible(true);
sfd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sfd.setLocationRelativeTo(null);
// Add the image gallery panel
add(imageGallery, BorderLayout.PAGE_START);
// creates the JButtons objects for each button
JButton FIRST = new JButton("|<<<");
JButton PREVIOUS = new JButton("< Prev");
JButton GRAPH = new JButton("Gaph display");
JButton NEXT = new JButton("Next >");
JButton LAST = new JButton(">>>|");
// adds the buttons to the button panel
JPanel buttons = new JPanel();
buttons.setLayout(new GridLayout(1, 4));
buttons.add(FIRST);
buttons.add(PREVIOUS);
buttons.add(GRAPH);
buttons.add(NEXT);
buttons.add(LAST);
// add buttons on the bottom of the frame
add(buttons, BorderLayout.SOUTH);
// register listener
FirstButtonListener FirstButton = new FirstButtonListener();
PreviousButtonListener PreviousButton = new PreviousButtonListener();
GraphButtonListener GraphButton = new GraphButtonListener();
NextButtonListener NextButton = new NextButtonListener();
LastButtonListener LastButton = new LastButtonListener();
// add listeners to corresponding componenets
FIRST.addActionListener(FirstButton);
PREVIOUS.addActionListener(PreviousButton);
GRAPH.addActionListener(GraphButton);
NEXT.addActionListener(NextButton);
LAST.addActionListener(LastButton);
}
Obviously the both extends JFRAME
Are you typing something into that frame? Do you have any shortcuts defined per application?
I had the same problem with my application frame dissapearing from time to time.
In my case, i had some key shortcuts defined per application and one of them was Shift + C (closing the application - bad choice, i know).. so whenever i wanted to type upper case for "c" into a field, i was actually calling the shortcut to close window.
Have many frames is not a good practice.
You should try to use the JDialog instead of JFrame.
This way you can pass the Main Frame to the others dialogs and let them be modal
Like this:
Opening from main
public class Test extends JDialog {
public Test(Frame frame, String dialogName) {
super(frame, dialogName, true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setBounds(x, y, w, h);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
new Test(null, "Test Dialog");
}
}
Opening from another frame
public void yourMethod() {
new Test(yourMainFrame, dialogName);
}

Image change when button is pressed Java

Okay I want my background image to change when helpButton is pressed. I can make the button image change when the mouse hovers over it with a Mouse Listener. I did the same steps except with the Action Listener but with no success. Any help would be great!
public class test extends JFrame{
private JLabel label;
private JButton button;
private ImageIcon bgi;
private JLabel bgl;
public static Rectangle gameSquare;
private JButton startButton;
private JButton helpButton;
private final Action action = new SwingAction();
public static void main(String[] args) throws MalformedURLException, IOException {
test gui = new test ();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // when click x close program
gui.setSize(902, 305);
gui.setVisible(true);
gui.setTitle("Solid Cloud Inc - Twitter Unfolower");
}
public test() throws MalformedURLException, IOException{
bgi = new ImageIcon(getClass().getResource("tu.png"));
getContentPane().setLayout(null);
BufferedImage img = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/start_zpsf3781681.png"));
//ImageIcon start = new ImageIcon(getClass().getResource("start.png"));
startButton = new JButton("");
startButton.setIcon(new ImageIcon(img));
startButton.setBounds(22, 186, 114, 50);
getContentPane().add(startButton);
BufferedImage img2 = ImageIO.read(new URL("http://i1344.photobucket.com/albums/p656/SolidCloudInc/help_zpsc4fad867.png"));
final JButton helpButton = new JButton("");
helpButton.setIcon(new ImageIcon(img2));
helpButton.setBounds(192, 186, 114, 50);
getContentPane().add(helpButton);
bgl = new JLabel (bgi);
bgl.setBounds(0, 0, 886, 272);
getContentPane().add(bgl);
Events e = new Events();
startButton.addActionListener(e);
helpButton.addActionListener(e);
}
public class Events implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == startButton) {
label.setText("Searching");
try {
Unfollow();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else if (e.getSource() == helpButton){
System.out.println("gottem");
bgi = new ImageIcon(getClass().getResource("tu2.png"));
bgl = new JLabel (bgi);
}
}
}
bgl = new JLabel (bgi);
Here you're creating a new JLabel, and putting it into the bgl variable but are doing anything with it and make no change to the JLabel object that continues to be shown in the GUI. It's a common newbie trap to think that by changing the reference of a variable you change the state of the original object that the variable previously referred to. That's not how it works. In other words, the original JLabel that was held by the bgl variable still exists and still displays its original content in the GUI despite this code above. What you should do instead is to change the icon shown by the original JLabel, or in other words, change the state of the current JLabel object, not change references held by the bgl variable. i.e.,
bgl.setIcon(bgi);
Also, you'll want to get rid of any and all use of null layout and calls to setBounds(...) as this will lead to buggy hard to maintain and upgrade code. Let the layout managers do the heavy lifting with regards to laying out the GUI.

Categories