0

I wondered how do I put a JLabel or a JPanel at the center of a JTable that Sit in a JPanel. I saw one example with BufferedImage but I could not convert it for some resone, here is the example link: Put JLabel on Component in JPanel

https://i.stack.imgur.com/k8KUu.png

I put also an image to show what I mean. Press to see the result I need by the way this is how it looks like when I uses windows 8 now... any idea ?

camickr
  • 321,443
  • 19
  • 166
  • 288
amnon
  • 97
  • 1
  • 8
  • I think you might want to draw a picture or provide an example – MadProgrammer Oct 19 '18 at 00:34
  • 1) *"I saw one example.."* Where? Link please. The internet is a big place. 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. 4) As to putting anything at the 'center' of a `JTable`. .. How does that make any sense? – Andrew Thompson Oct 19 '18 at 00:36
  • this is the example i saw https://stackoverflow.com/questions/45701380/put-jlabel-on-component-in-jpanel – amnon Oct 19 '18 at 00:53
  • what I am asking is the same thing as the example I sent only insteed of image I want to use JComponent such as JTable and Insteed of JLable I want to use JPanel. – amnon Oct 19 '18 at 00:58
  • Andrew I found out that the library Jdesktop is not wotking anymore with windows 10 and since then I am tring to find a way to put my JPanel that contains the text 'please wait' on the center of the table till the data will be loaded. – amnon Oct 19 '18 at 01:05
  • Hope I cleared myself, if not tell me what more you want me to provide you. – amnon Oct 19 '18 at 01:07
  • `...that contains the text 'please wait' ` - Maybe something like the [Disabled Glass Pane](https://tips4java.wordpress.com/2008/11/07/disabled-glass-pane/). – camickr Oct 19 '18 at 01:19
  • no glass panel, I am in a tabbed panel and need individual msg for each tab. note: till windows 10 showed up java handled it fine with the library JDesktop, but not anymore – amnon Oct 19 '18 at 01:29
  • So only one tab is ever active at a time. Change the message depending on the tab – camickr Oct 19 '18 at 02:14

1 Answers1

0

You can add any component directly to the table (this is how an editor works).

You just need to set the size/location of the component:

JLabel label = new JLabel( "Please Wait" );
label.setSize( label.getPreferredSize() );
label.setLocation(20, 20);
table.add( label );
table.repaint();

Or you could use the JLayer class to decorate the JTable. Read the section from the Swing tutorial on How to Decorate Components With the JLayer Class for more information and working examples.

Edit:

A simple example of a proper MCVE"

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class PleaseWait extends JPanel
{
    PleaseWait()
    {
        JTable table = new JTable(5, 5);
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        add( new JScrollPane( table ) );

        JLabel label = new JLabel("Please Wait");
        label.setOpaque(true);
        label.setBackground(Color.LIGHT_GRAY);
        label.setBorder( new EmptyBorder(10, 10, 10, 10) );
        label.setSize( label.getPreferredSize() );
        label.setLocation(150, 20);
        table.add( label );
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame("PleaseWait");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new PleaseWait());
        frame.pack();
        frame.setLocationByPlatform( true );
        frame.setVisible( true );
    }

    public static void main(String[] args) throws Exception
    {
        EventQueue.invokeLater( () -> createAndShowGUI() );
/*
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowGUI();
            }
        });
*/
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Better to use a [`UILayer`](https://docs.oracle.com/javase/tutorial/uiswing/misc/jlayer.html) – MadProgrammer Oct 19 '18 at 03:25
  • I looked at the page How to Decorate Components With the JLayer page and found the class TapTapTap is the closest one to what i need, but Since taptaptap is not the result I wanted, I have been trying all this weekend to change this tap tap tap class to a jpanel. the problem is that i coulnd find a way to force layerUI contain a jpanel as an object. I thought it will be easy but somehow its not and I just couldn't do it, can you please show me an example – amnon Oct 20 '18 at 23:05
  • The tutorial has a working example. Instead of painting animation you paint text. How hard can that be? I have no idea what you find confusing about the example. You don't "force a layerUI to contain a JPanel". The LayerUI and JPanel work together to make a single object. The panel paints it's components on the bottom and the layerUI paints on top? If you want to use a layout then your code needs to be designed that way. Why do you think my example would be any different??? If the LayerUI is to complicated for you, then I showed you how to add a label to the table in 5 statements. – camickr Oct 21 '18 at 00:43
  • I already suggested the GlassPane which is easier than the LayerUI, since you can show/hide the glass pane at any time. It does not require the tight coupling of components like the LayerUI class does. You are the one that rejected the idea, In any case you have 3 solutions. We are not here to right the code for you. In any case I don't understand your full requirement and I can't guess what you find confusing. Just saying it doesn't work gives s no information. So I can't help further. – camickr Oct 21 '18 at 00:49
  • Its hard because i want to put JButton in the jpanel that should be there a so its not a drawing anymore,there are component there so its JPanel. In other words I need a way to make the paint metod to put a JPanel with its components not to draw some text with some lines. – amnon Oct 21 '18 at 06:49
  • by the way you asked why I can't put glass panel ? my program runs 4 queries at the same time from 4 tabs and when each finish, each one separatly should dissapear. with glass I can handle only one – amnon Oct 21 '18 at 07:47
  • Why do you keep ignoring my suggestions? Where is you code showing how you added a label to the table???? If you want help show me you have made an effort. So create a simple [mcve] showing what you tried. All you need is a JFrame with a JTable and you add a JLabel (or JButton) to the table. I can't guess why it doesn't work for you. It works for the JTable when you edit a cell. So what are your doing wrong? – camickr Oct 21 '18 at 20:19
  • camickr, solved this issue not thanks to you !!!! I will ignore what you told me in my last post because I don't want to lose you as an obsevoir. well sometimes its impossible to put code even the minimal, becase its too havy or not worth to put since i don't have a clue what I need to do. I hope next time you will watch my answers and answer only answers that concern what I posted. by the way the last issue I solved also no thanks to you. regards – amnon Oct 26 '18 at 00:49
  • I am planning to put another issue in the next few hours and hope you will halp me solve it. – amnon Oct 26 '18 at 00:51
  • `solved this issue not thanks to you` - excuse me, I gave you a solution that takes 5 lines of code. It is NOT my problem if you can't add those 5 lines of code to a simple program. It takes a couple of statements to create a JFrame. It takes a couple more statements to create a JTable and add it to the frame. Then the 5 statements to add the label to the table. In total 15-20 lines of code to show a label with the words "Please Wait" on a JTable. You do NOT need a 200 line program to demonstrate a simple concept. The sooner you learn that the faster you will solve problems. – camickr Oct 26 '18 at 02:45
  • And I am not the only person who asked you to post a [mcve]. In this forum if you want detailed help then you make an effort to ask a reasonable question that we can all understand. I am the only person who even made an effort to help! – camickr Oct 26 '18 at 02:48
  • @amnon, `its impossible to put code even the minimal,` - I gave you suggestions on how to create this basic example. But of course you actually need to read the comments and try it first before before saying it is impossible. If you can't post a simple example then that is because you don't understand your question. And if you don't understand your question/requirement then how do you expect us to? See my edit for an example of a "MCVE" that does exactly what your image indicates you want. – camickr Oct 26 '18 at 03:15
  • `hope you will halp me solve it.` - Seriously? You got a detailed answer in this question:https://stackoverflow.com/q/52869451/131872. You got a detailed answer with a runnable "MCVE" and didn't bother to say thank you or "accept" the answer. You still haven't clarified your question or posted an "MCVE" here: https://stackoverflow.com/q/52935629/131872 Again you ignored the comments. And here even though I gave you 3 solutions, you say I didn't help? Good luck! – camickr Oct 26 '18 at 03:22
  • `solved this issue` - then why don't you contribute to the forum and post your answer to that other can benefit from your experience. The forum is about helping others. I did my best, whether you appreciated it or not. – camickr Oct 26 '18 at 03:38
  • well thenks to you I saw for the first time MadProgrammer answer stackoverflow.com/q/52869451/131872 and he was absolutly right. – amnon Oct 27 '18 at 06:12
  • I dont know why I didnt see it before. It was solving a lot of problems for me, and saving a lot of time. I am really sorry – amnon Oct 27 '18 at 06:14
  • @amnon, You still haven't "accepted" my answer here. Your question was `how to put a component at the center of other component` and you posted an image showing what you want. Your example showed a label on a JTable. I gave you an exact answer to that question by giving you the 5 lines of code necessary to do that. I then gave you a [mcve] showing the complete answer. Now it may not be the solution your used because you changed your requirement. But I answered the question that was asked. I can't be a mind reader guessing what you are really asking. – camickr Oct 27 '18 at 14:29
  • I marked it as solved although I am not sure its useful – amnon Oct 29 '18 at 00:44
  • @amnon, What am I missing??? It does exactly what you asked. It does exactly what your picture shows. I don't know how to provide a clearer answer based on the question asked. Please tell me how this does not answer the question that was asked??? If the answer isn't useful then that means the question isn't useful so why did you ask the question??? We can't read your mind to guess what you are really thinking. – camickr Oct 29 '18 at 01:06