How do I dispose my second frame while retaining my MainFrame?
I have a JFrame( CCurrencyConverterFrame) that I call from the Main Frame.
JMenuItem mntmCurrencyConverter = new JMenuItem("Currency Converter");
mntmCurrencyConverter.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CCurrencyConverterFrame frame = new CCurrencyConverterFrame();
myFrame = frame;
JPanel contentPane = new CCurrencyConverter(myFrame);
frame.setContentPane(contentPane);
frame.setVisible(true);
}
});
In CCurrencyConverterFrame.java,
public CCurrencyConverterFrame() {
setFont(new Font("Dialog", Font.BOLD, 12));
getContentPane().setFont(new Font("Tahoma", Font.BOLD, 16));
setTitle("Currency Converter ");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 469, 470);
//Center
setLocationRelativeTo(null) ;
}
I have tried using JFrame.HIDE_ON_CLOSE but it still terminates my application. What can I do to resolve this?
Related
I'm creating a Loading Page for our group project, but every time I call the loading page class, it is invisible.
Here is my Main class:
public static void main(String[] args) {
new caller();
}
Here is the button that when you click you will go into the loading page class.
JFrame frame = new JFrame();
JButton btn = new JButton("Press me!");
caller(){
btn.setBounds(20, 30, 120, 50);
btn.setFocusable(false);
btn.addActionListener(this);
frame.setSize(420, 420);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLayout(null);
frame.add(btn);
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn) {
frame.dispose();
new loading_page();
}
}
And here is the loading page class
JLabel lablab = new JLabel();
JProgressBar bar = new JProgressBar();
JFrame frame = new JFrame();
loading_page(){
lablab.setBackground(Color.WHITE);
lablab.setHorizontalAlignment(SwingConstants.CENTER);
lablab.setIcon(new ImageIcon("C:\\Users\\KHAdmin\\Documents\\Pictures\\giphy.gif"));
lablab.setBounds(65, 40, 367, 215);
bar.setForeground(Color.cyan);
bar.setBounds(10, 300, 480, 17);
bar.setValue(0);
bar.setStringPainted(true);
bar.setFont(new Font("MV Boli", Font.BOLD, 15));
bar.setForeground(Color.CYAN);
bar.setBackground(Color.BLACK);
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(400, 200, 500, 335);
frame.setVisible(true);
frame.setLayout(null);
frame.getContentPane().setBackground(Color.BLACK);
frame.getContentPane().add(lablab);
frame.getContentPane().add(bar);
int counter =0;
while(counter<=100) {
bar.setValue(counter);
try {
Thread.sleep(50);
if(counter==100) {
frame.dispose();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
counter +=1;
}
{
new caller();
}
}
I think it's in the while(counter<==100) statement.
I used the frame dispose to dispose the frame from the previous class, but when the system call the 3rd class which is the loading page class, it's invisible.
I'm trying to change the theme using FlatLaf (2.0.1) on a JDateChooser (JCalendar 1.4) object. I don't know why the day buttons don't refresh.
Here is my code:
public class Test extends JFrame {
private JPanel contentPane;
private JDateChooser dateChooser;
private JToggleButton bChangeTheme;
public static void main(String[] args) {
FlatArcDarkOrangeIJTheme.setup();
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Test() {
setFont(new Font("Verdana", Font.PLAIN, 12));
setTitle("Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 95);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new MigLayout("", "[][grow]", "[grow]"));
bChangeTheme = new JToggleButton("Change theme");
bChangeTheme.setFont(new Font("Verdana", Font.PLAIN, 12));
bChangeTheme.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if(bChangeTheme.isSelected()) {
FlatArcOrangeIJTheme.setup();
FlatLaf.updateUI();
dateChooser.setFont(FlatUIUtils.nonUIResource(new Font("Verdana", Font.PLAIN, 12)));
} else {
FlatArcDarkOrangeIJTheme.setup();
FlatLaf.updateUI();
dateChooser.setFont(FlatUIUtils.nonUIResource(new Font("Verdana", Font.PLAIN, 12)));
}
}
});
contentPane.add(bChangeTheme, "cell 0 0,growy");
dateChooser = new JDateChooser();
dateChooser.setDateFormatString("yyyy-MM-dd");
dateChooser.setFont(new Font("Verdana", Font.PLAIN, 12));
dateChooser.getCalendarButton().setFont(new Font("Verdana", Font.PLAIN, 12));
dateChooser.getJCalendar().getDayChooser().setDecorationBackgroundVisible(false);
dateChooser.getJCalendar().getDayChooser().setSundayForeground(Color.decode("#f57900"));
dateChooser.getJCalendar().getDayChooser().setWeekdayForeground(Color.decode("#f57900"));
dateChooser.getJCalendar().getYearChooser().setFont(dateChooser.getFont());
dateChooser.getJCalendar().getDayChooser().setForeground(dateChooser.getJCalendar().getMonthChooser().getForeground());
dateChooser.getJCalendar().setFont(dateChooser.getFont());
contentPane.add(dateChooser, "cell 1 0,grow");
}
}
I've been trying to get to the buttons like this:
dcDo.getJCalendar().getDayChooser().getDayPanel().getComponents();
Failed to change the background of the buttons in a loop.
I also tried to find a similar trick to the one used to set the font.
dateChooser.setFont(FlatUIUtils.nonUIResource(new Font("Verdana", Font.PLAIN, 12)));
But no success...
I will be grateful for all suggestions.
Hi so I am trying to creat this menu for the game, and when I run the code it is very hit or miss. In the same computer with same screen same OS and everything the same, I might run the code ones and the menu is fine. and then I run it again and the pictures are missalighed or the just disappear and so on. I have tried changing the order of the contentPane.add but and I am out of ideas. What else could it be? Thanks
public static int type(){
// Create a "clickable" image icon
int i =0;
Color c = new Color (0,0,0);
ImageIcon icon = new ImageIcon("images/mike_main.png");
JLabel label = new JLabel(icon);
ImageIcon icon1 = new ImageIcon("images/igal_main.png");
JLabel label1 = new JLabel(icon1);
JPanel contentPane = new JPanel();
JLabel label3 = new JLabel("Choose your character!");
JLabel mike_l = new JLabel("Mike");
JLabel mike_info = new JLabel("<html>Speed: 10<br>Range: 7</html>");
JLabel igal_l = new JLabel("Igal");
JLabel igal_info = new JLabel("<html>Speed: 7<br>Range: 10</html>");
label3.setBounds(600,100,2000,100);
label3.setFont(new Font("Serif", Font.BOLD, 56));
label3.setForeground(Color.WHITE);
contentPane.setOpaque(true);
contentPane.setBackground(c);
contentPane.setLayout(null);
final JFrame frame = new JFrame("My Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label);
frame.setTitle("The Ultimate Game");
frame.setExtendedState(frame.MAXIMIZED_BOTH);
frame.pack();
frame.setVisible(true);
label.setBounds(400,400,300,600);
mike_l.setBounds(500,330,1000,100);
mike_info.setBounds(750,500,1000,100);
//label.setLocation(50, 50);
label1.setBounds(1200,400,300,600);
igal_l.setBounds(1300,330,1000,100);
igal_info.setBounds(1550,500,1000,100);
//label1.setLocation(250, 250);
mike_l.setFont(new Font("Serif", Font.BOLD, 26));
mike_l.setForeground(Color.WHITE);
mike_info.setFont(new Font("Serif", Font.BOLD, 26));
mike_info.setForeground(Color.WHITE);
igal_l.setFont(new Font("Serif", Font.BOLD, 26));
igal_l.setForeground(Color.WHITE);
igal_info.setFont(new Font("Serif", Font.BOLD, 26));
igal_info.setForeground(Color.WHITE);
contentPane.add(label);
contentPane.add(label1);
contentPane.add(label3);
contentPane.add(mike_l);
contentPane.add(mike_info);
contentPane.add(igal_l);
contentPane.add(igal_info);
frame.setContentPane(contentPane);
label.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
System.out.println("CLICKED");
frame.setTitle("Mike");
}
});
label1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
System.out.println("CLICKED");
frame.setTitle("Igal");
}
});
while (i==0){
//System.out.println("I am here");
String s = frame.getTitle();
System.out.println(s);
if (s.equals("Mike")){
i =1;
}
else if(s.equals("Igal")){
i = 2;
}
// Add it to a frame.
}
System.out.println("im out");
frame.setVisible(false);
return i;
}
I think the issue is that your are calling frame.setVisible(true) before you are done making changes to frame. If you move this (and maybe the pack() call) pass all your add()s and after you setContentFrame(), then your window will open more reliably.
As for the alignment, that is going to come down to all your setBounds() calls. If you don't need to use setBounds() you should probably look into Layout Managers and nest JPanels for modular sections of you're design (e.g. character info).
I have created a set of buttons that I want to be at the top of every page of my application.
Rather than having to recreate the setup in every class, is it possible to create it once and include it in every class, similar to a reusable view in Android.
My code for the button setup is below:
public Buttons() {
setLayout(null);
JButton btnNewButton = new JButton("New button");
btnNewButton.setBounds(10, 11, 89, 23);
add(btnNewButton);
JButton btnNewButton_1 = new JButton("New button");
btnNewButton_1.setBounds(101, 11, 89, 23);
add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("New button");
btnNewButton_2.setBounds(192, 11, 89, 23);
add(btnNewButton_2);
textField_2 = new JTextField();
textField_2.setBounds(104, 42, 86, 20);
add(textField_2);
textField_2.setColumns(10);
System.out.println("HELLO");
}
My code to create the class is below:
public static void gui(){
frame = new JFrame("name");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setLayout(new BorderLayout());
frame.add(new Buttons(), BorderLayout.WEST);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setBounds(200, 0, 500, 500);
}
I have tried adding the following line to this method to add a second set of buttons:
frame.add(new Extras(), BorderLayout.CENTER);
but it only adds the second object to the display.
Create a class (or method) that creates a JPanel which holds all those buttons you want. Then, just add a new instance of that JPanel to your GUI. Pseudo-code:
public JPanel createButtons() {
JPanel panel = new JPanel();
// add buttons to panel
return panel;
}
public void gui() {
frame = new JFrame("name");
frame.setLayout(new BorderLayout());
frame.add(createButtons(), BorderLayout.WEST);
// ...
}
I know this is ill advised but I am using absolute layout and trying to go from panel to panel and was curious how to do this.
public class Mainscreen extends JFrame {
private JPanel Home;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Mainscreen frame = new Mainscreen();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Mainscreen() {
final Dataentrylog DEL = new Dataentrylog();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setBounds(100, 100, 618, 373);
Home = new JPanel();
Home.setBackground(new Color(255, 250, 250));
Home.setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
setContentPane(Home);
Home.setLayout(null);
JButton btnNewButton = new JButton("Data Entry login");
btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnNewButton.setForeground(new Color(0, 0, 0));
btnNewButton.setBackground(UIManager.getColor("Menu.selectionBackground"));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Home.setVisible(false);
setContentPane(DEL);
setLayout(null);
}
});
btnNewButton.setBounds(44, 214, 204, 61);
Home.add(btnNewButton);
}
}
Calling this JPanel which works
import java.awt.EventQueue;
public class Dataentrylog extends JPanel {
public Dataentrylog() {
setBounds(100, 100, 618, 373);
setBackground(new Color(255, 250, 250));
setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
setLayout(null);
DEadmin DEA = new Deadmin();
final JButton btnSignIn = new JButton("Sign in");
btnSignIn.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnSignIn.setBackground(UIManager.getColor("EditorPane.selectionBackground"));
btnSignIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnSignIn.setBounds(226, 282, 153, 52);
add(btnSignIn);
}
}
While this works if I try an call another JPanel from Dataentry log the JFrame is blank. What can I do to call another JPanel? Any help would be great. Also, I know that layoutmanagers are the norm but for what I had to do I wasn't able to find anything that worked so I choose to use null despite my best judgement. THANKS!
Intialization of Dataentrylog should be like this
final Dataentrylog DEL = new Dataentrylog(this);
we have to pass the parent jframe.
public class Dataentrylog extends JPanel {
public Dataentrylog(final JFrame parent ) {
setBounds(100, 100, 618, 373);
setBackground(new Color(255, 250, 250));
setBorder(new LineBorder(Color.DARK_GRAY, 1, true));
final JButton btnSignIn = new JButton("Sign in");
btnSignIn.setFont(new Font("Tahoma", Font.PLAIN, 14));
btnSignIn.setBackground(UIManager.getColor("EditorPane.selectionBackground"));
btnSignIn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
JPanel panel = new JPanel();
panel.add(new JButton("Solved"));
parent.setContentPane(panel);
}
});
btnSignIn.setBounds(226, 282, 153, 52);
add(btnSignIn);
}
}