JTable only showing after I resize the JPanel in Eclipse - java

I have a button called btnDisplay on a JPanel called TempPanel. When the button is clicked, it should display a JTable that is created manually.
However the table is only visible to me after I resize the panel manually with a mouse. Even if I make the panel smaller than it originally was, it shows the table, otherwise it doesn't.
What is the reason for this? And how can I fix it?

I'm writing the comment as an answer for clarity purpose:
Call revalidate and repaint on the container to which the JTable is been added

Related

How to reload a JPanel?

I'm a java newbie... I need to know how to reload a JPanel? I extended the JPanel class and created a panel that will run in a cardlayout in an Applet. I want this panel to reload/refresh after the user clicks a button in this panel. I tried including the revalidate() and repaint() methods (methods I don't understand well) in the ActionListener for the button but nothing happened. Can anyone throw some light on how to correctly refresh the whole panel?
This works fine for me. But after an other
_panel.revalidate();
_panel.repaint();

JButton twice setText to JLabel after JPanel revalidate()

When I click button I want to add one button to panel, which I did, but my program doesn't work like before. This is my program before:
South panel with pink buttons is cardlayout panel. When I click gray buttons on east, card panels changed. When I click pink button, this happened:
One pink button setText to one label. After I add one button to cardpanel "TOPLI NAPICI" this happened:
As you can see, one button "moka" add to panel,which I want, but when I click one button, it setText twice, only new button (in this case "MOKA") setText once. This happened also with other panels in cardpanel:
this is part of my code when I click button to add new button
if (enter == JOptionPane.OK_OPTION) {
try{
Double price1=Double.parseDouble(priceField.getText());
String name1= productField.getText();
Product name = new Product(name1, price1);
Application .manu.add(name);
if (field.getSelectedItem().equals("TOPLI NAPICI")){
for(Controller c:Controller) {
c.tnp.add(new JButton(name1));//c is panel , holds all panels
c.tn.revalidate();//c.tn is panel which is changed( adding one button), cardpanel
c.removeAll();
c.panels();//create all panels which is removed
c.revalidate();
System.out.print( c.tnp.size());//tnp is a list with buttons, shows that add one more button, that is correct
}
Like it duplicate panels, or when I click button it clicked twice. Is it possible? And although I changed only one panel in cardlayout ("TOPLI NAPICI"),it changed in all panels in cardlayout ("SOKOVI" and "ALKOHOL")
What is wrong?
Thanks for any help.
no idea whats LayoutManager is used, nothing talking about CardLayout, for better help sooner post an SSCCE, short, runnable, compilable, just about JPanel with CardLayout and one JButton for code posted here,
images posted here without code not helped
CardLayout by default never required call for revalidate() & repaint(), all those notifiers are implemented in API by default
this isn't proper way
.
c.tn.revalidate();//c.tn is panel which is changed( adding one button), cardpanel
c.removeAll();
c.panels();//create all panels which is removed
c.revalidate();
apply all changes to already visible GUI (remove, add, modify, relayout), then last two code lines should be revalidate() & repaint(), for 1st. container where are changes done, or if there are changed a few containers, then to call their 1.st joined container
up to Java7 is required to use validate() & repaint() for JFrame, JDialog, JWindow, in the Java7 is implemented revalidate() for Top Level Containers too,, the same as for JPanel (for example)

JPanel data getting changed on mouse click

I am using three JButtons in my swing application. When I click on each button, the corresponding data (formatted in JTable with JScrollPane) will display on JPanel.
Problem: when I resize the JFrame, the JPanel is replacing with default button (the button which i was clicked first) information instead of current JButton information.
My sample code:
jbutton1.addActionListener(this);
jbutton2.addActionListener(this);
public void actioPerformed(ActionEvent e){
if(e.getActionCommand.equals("button1"))
JPanel.add(table1);
}
if(e.getActionCommand.equals("button2"))
JPanel.add(table1);
}.......
Resizing the JPanel will not suddenly replace components or add other components to the panel.
My best guess (and it is a guess due to the limited information in the question) is that none of your buttons actually work and just show the wrong information.
The code you posted only contains an add without any revalidation of the layout. Consult the javadoc of the Container#add method. When you resize, the layout gets revalidated and you see what is actually contained in the JPanel.
Possible solutions:
Call invalidate and repaint on your panel as well in your ActionListener
Use a CardLayout to switch between the different components
I personally prefer the CardLayout option, but it might depend a bit on the situation.
Note that in the code you posted, you add table1 for both buttons. Might be a copy-paste problem, or a problem with your actual code.
I was unable express problem clearly.Sorry for your inconvenience.
JPanel.removeAll() method has fixed my problem.
I added this method before adding any new component to JPanel. That fixes JPanel unexpected behavior.

addMouseListener() won't work with JPanel under JScrollPane

I add my customized panel onto JScrollPane. I also have another set of "tool" that will attach itself to my custom panel. The problem is, in these tool, it invokes attached.addMouseListener(this). Normally, everything would function well, but when I have it inside JScrollPane, it won't function at all. My deduction is JScrollPane never gives window focus to its child component. Is there a way to get this around without having to change my attachment procedure? I want my 'tool' to specifically attach to my custom panel, not the scrollpane.
I hope I have understood the problem.
What about adding a MouseListener to the JScrollPane and then dispatch the event to the JPanel?
Somenthing like this:
//JScrollPane Listener
public void mousePressed(MouseEvent me) {
jpanel.dispatchEvent(me);
}
It's a little bit tricky, but so you don't have to change the JPanel Listener.

Java Applet Panel Refresh

I have a Java Applet that loads a record of a board game from a URL and displays the configuration using a series of panels laid out in a grid and nested in a JPanel. This is a small component of an overall applet with text fields and such for inputs and outputs. When a button is pressed, I want the board to show the next configuration from the record. I figured out a way to do this, but when the button is pressed, the same configuration is shown, but when I scroll up or down, or resize the window, then the configuration changes. Is there a way to "refresh" just the board JPanel when the button is pressed? Currently, when the button is pressed, the new configuration panel is created, the old configuration is removed, and the new configuration is added to the display.
Thank you very much!
If nothing else, you could try hiding and then immediately showing the panel again. That should force it to redraw.

Categories

Resources