eclipse ide java not loading jpanels - java

so my problem is quite bizarre. So I am trying to write a java program in the eclipse ide. I have one JFrame and multiple JPanels added to it. You can navigate through the JPanels via buttons setting one panel to not be visible and another to be visible, you get the idea. However, after some time this started to occur: when I press a button the correct JPanel is being set to visible and I can see it, but it is completely empty. Sometimes its content is displayed correctly, but this is rather inconsistent.
As I said: sometimes this happens, sometimes it doesnt happen. I would start the program and close it 5 times and the 6th time it finally works. I don't think the code is necessary (as it is all correct because, after all, it does work sometimes), but if you want to see it feel free to ask. My eclipse workspace is on my C drive on an SSD and it is not too full. it has like 100 out of 250 gigs left. Thank you for your time and help in advance!
Edit:
So heres the code example.
Two of the JPanels Im trying to switch between:
The "main" panel
Var.menuPanel = new JPanel();
Var.jf1.add(Var.menuPanel);
Var.menuPanel.setBounds(0, 0, Var.screenwidth, Var.screenheight);
Var.menuPanel.setLayout(null);
Var.menuPanel.setVisible(true);
Var.menuPanel.revalidate();
Var.menuPanel.repaint();
This loads all fine.
The button to switch to the other panel:
Var.editCourse = new JButton("Edit");
Var.menuPanel.add(Var.editCourse);
Var.editCourse.setBounds(550, 120, 200, 50);
Var.editCourse.setVisible(true);
Var.editCourse.addActionListener(handler);
Var.editCourse.setBackground(Color.YELLOW);
The other JPanel + UI elements
Var.editCoursePanel = new JPanel();
Var.jf1.add(Var.editCoursePanel);
Var.editCoursePanel.setBounds(0, 0, Var.screenwidth, Var.screenheight);
Var.editCoursePanel.setLayout(null);
Var.editCoursePanel.setVisible(false);
Var.editCoursePanel.revalidate();
Var.editCoursePanel.repaint();
Var.editCourseToMenu = new JButton("Back");
Var.editCoursePanel.add(Var.editCourseToMenu);
Var.editCourseToMenu.setVisible(true);
Var.editCourseToMenu.setBounds(550, 400, 200, 50);
Var.editCourseToMenu.addActionListener(handler);
Var.editCourseSelection = new JComboBox<String>(JSONHandler.courses);
Var.editCoursePanel.add(Var.editCourseSelection);
Var.editCourseSelection.setBounds(550, 200, 200, 50);
Var.editCourseSelection.setVisible(true);
Methods.setArrowBounds(Var.editCourseSelection);
Var.editCourseSelection.setSelectedItem("Edit.");
Var.submitCourse = new JButton("Continue");
Var.editCoursePanel.add(Var.submitCourse);
Var.submitCourse.setBounds(550, 300, 200, 50);
Var.submitCourse.setVisible(true);
Var.submitCourse.addActionListener(handler);
The line in the AcionHandler:
else if(e.getSource() == Var.editCourse) {
MenuHandler.showEditCoursePanel();
}
The showEditCoursePanel methtod:
public static void showEditCoursePanel() {
Var.menuPanel.setVisible(false);
Var.editCoursePanel.setVisible(true);
}
Oh and also, sometimes when it doesnt load, there is the scrollbar of a JScrollPane on the side, which, as you can see, is never even mentioned.
Edit 2:
Okay, I found the source of the problem. When I add the raw JPanel to the JFrame and workt with that it all works fine, I cant scroll of course. But when I add the JScrollPane the newCoursePanel doesnt show up at all. Everything else is now working fluently, except for the panel with the scrollbar. Heres the code form the nwCoursePanel im talking about:
Var.newCoursePanel = new JPanel();
Var.newCoursePanel.setBounds(0, 0, Var.screenwidth, Var.screenheight);
Var.newCoursePanel.setLayout(null);
Var.newCoursePanel.revalidate();
Var.newCoursePanel.repaint();
Var.newCoursePane = new JScrollPane(Var.newCoursePanel);
Var.newCoursePane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
Var.newCoursePane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
Var.newCoursePane.setVisible(false);
Var.jf1.add(Var.newCoursePane);
I cant figure out how to get it working though. in to show the panel I just Var.menuPanel.setVisible(false) and Var.newCoursePane.setVisible(true).

Related

Java's JOptionsPane dialog window too small?

I created a Panel to allow some user input. This panel needs to come over the application's main window which is usually marked as 'alwaysOnTop'. So the input panel is shown using JOptionPane like this:
ConfigurationPanel cp = new ConfigurationPanel();
cp.setDuration(total);
JWindow window = (JWindow) SwingUtilities.getWindowAncestor(TimerPanel.this);
window.setAlwaysOnTop(false);
if (JOptionPane.showConfirmDialog(TimerPanel.this, cp, "Settings", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
// if successfull, take data
try {
total = cp.getDuration();
savePreferences();
reset();
} catch (DateTimeParseException dtpe) {
// do nothing
}
}
window.setAlwaysOnTop(true);
The ConfigurationPanel has been created with the NetBeans GUI builder which has not failed me in 20 years, so I never actually checked. But since it was mentioned I checked and the cnnstructor calls initComponents(), which in turn contains these lines:
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
This is what the panel itself shall look like (screenshot from Netbeans IDE):
And this is what the combination looks like at runtime:
Why are the main buttons Ok and Cancel not properly visible? This has worked in the past, and it seems the problem comes from outside my code...
This seems to happen both on Windows 10 based Oracle Java 13.0.1+9 and 15.0.1+9-18 and OpenJDK 16.0.1+9-24.
Switching from GroupLayout (Netbeans' default layout) to GridBagLayout resolved the issue.

Why is my JButton background white when I've set it to black? [duplicate]

This question already has answers here:
Why does setBackground to JButton does not work?
(6 answers)
Closed 5 years ago.
I've been following this tutorial on youtube on how to create a text-based adventure game. I've been following the tutorial step-by-step but unfortunately, I cannot seem to find a solution to my issue.
Here is my block of code (I've narrowed it down to the JPanel/JButton portion):
public Game() {
window = new JFrame();
window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(Color.black);
window.setLayout(null);
con = window.getContentPane();
// button panel
buttonPanel = new JPanel();
buttonPanel.setBounds(300, 400, 200, 100);
buttonPanel.setBackground(Color.black);
//start button
pushButton = new JButton("START");
pushButton.setBackground(Color.black);
pushButton.setForeground(Color.white);
pushButton.setFont(normalFont);
pushButton.setFocusPainted(false);
buttonPanel.add(pushButton);
con.add(buttonPanel);
window.setVisible(true);
}
Here's a photo of what my button looks like:
Unclicked
Clicked
Some of the solutions I've tried include adding the following, both in combination and different orders:
pushButton.setContentAreaFilled(false);
pushButton.setOpaque(true);
pushButton.setOpaque(false);
pushButton.setFocusable(true);
And together:
pushButton.getBackground();
pushButton.setBackground(Color.BLACK);
But so far, none of them work or get the results I want -- which is to have a button with a BLACK background and WHITE text.
*I have been searching through Stackoverflow and other websites for an answer but so far, there seems to be no one else having similar issues as the one I am having.
**I am not sure if the OS that I am using Eclipse on makes a difference or not.
Please let me know if more information needs to be provided! Thank you all in advance.
Use setOpaque(true) first.
btn = new JButton();
btn.setOpaque(true);
btn.setBackground(backgroundColor);

jOptionPane dont show on top windows. Java 1.4

jOptionPane dont show on top windows.
I've read that using
JFrame frmOpt = new JFrame();
frmOpt.setVisible(true);
frmOpt.setLocation(100, 100);
frmOpt.setAlwaysOnTop(true);
should be enough, but the problems is that I'm using Java 1.4 and 'setAlwaysOnTop' does not exists.
so....is there a way to solve this situation?
thanks in advance.
EDIT: Here is what I'm doing:
JFrame frmOpt = new JFrame();
frmOpt.setVisible(false);
response = JOptionPane.showOptionDialog(frmOpt,message,mens, 0,JOptionPane.OK_CANCEL_OPTION,null,options,null);
First I create a JFrame, then I create a new JOptionFrame setting the JFrame.
And it still shows at the back. Notice I do not use setAlwaysOnTop because of Java 1.4

Chart Wont Show When I Run It?

I'm trying to make a simple pie chart appear on a jpanel in netbeans with JFreeChart and got this:
public void createPieChart()
{
DefaultPieDataset myPie = new DefaultPieDataset();
myPie.setValue("Apples",new Integer(12));
myPie.setValue("Oranges",new Integer(23));
myPie.setValue("Mangos",new Integer(7));
myPie.setValue("Pears",new Integer(22));
JFreeChart myChart = ChartFactory.createPieChart3D("Damo's Fruit Sales", myPie,true,true,true);
PiePlot3D pie3D = (PiePlot3D)myChart.getPlot();
ChartPanel myPanel = new ChartPanel(myChart);
lowerMain_PNL.removeAll();
lowerMain_PNL.add(myPanel,BorderLayout.CENTER);
lowerMain_PNL.revalidate();
}
I get no compiler errors and when it runs the window appears with the button, but when I press the button my pie chart doesn't appear. Anyone know what I could be missing?
Check the layout manager of lowerMain_PNL. Netbeans form designer uses GroupLayout by default, so unless you changed it, that's what you got. Adding to a container using GroupLayout at run time is tricky, especially if the component contains more than one subcomponent (And requires adding components to the layout, instead of using the usual add() methods).
Change it to BorderLayout instead, since you are using BorderLayout constraints.

Frame Showing Problem

I have made one project which is showing the inventory of the stock of one store.
In that inventory the software should store data of the products with their images.
There is one problem...
Bcz of the lots of stock, the screen on which is image is loading taking a lot of time.
So, i thought i should give the frame in which there will be on label which will show the "Loading Software".
But now when i am setting visible = true for that frame, but bcz of that images screen class loading problem my frame is not showing correctly. I have put screen shot, now my code.
JFrame f;
try{
f = new JFrame("This is a test");
f.setSize(300, 300);
Container content = f.getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
JLabel jl = new JLabel();
jl.setText("Loading Please Wait....");
content.add(jl);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}catch(Exception e){
e.printStackTrace();
}
initComponents();
try {
addInverntory = new AddInventoryScreen();
showstock = new showStock(); // this class will take big time.
mf = new mainForm();
f.setVisible(false);
}catch (Exception ex) {
ex.printStackTrace();
}
How Can show some message that, other class is loading or "Loading Software" kind of thing in this situation.
Just For the know....this class is not screen on which the image will load.
It's hard to answer this because it's not clear what the effects (Swing-wise) are of the calls to new AddInventoryScreen(); and new showStock();. You should only touch the UI that the user sees right at the end (when all the processing is done).
You should really spin off methods that will take a long time into their own Thread (see SwingWorker. There are alternatives for Java 5.0). That way, the UI won't be blocked while it's processing.
Maybe what you want is a Splash Screen?
Try calling validate(); and pack(); methods before calling f.setVisible(true);
Your code can be
validate();
pack();
f.setVisible(false);
I think one big problem in your code (maybe not the only one however) is the fact that you should use a different thread for long operations.
GUI operations (creating swing components, adding them to panels, changing labels...) are to be performed exclusively in the "EDT" and must be short (typically, less than 100ms or even 50ms).
Long operations can be easily done by another thread if you use the SwingWorker API (part of JDK 1.6).

Categories

Resources