0
import javax.swing.*; 
import javax.swing.border.*;
import javax.swing.table.TableColumn;

import java.awt.*;

import static java.awt.GraphicsDevice.WindowTranslucency.*;

import java.awt.Checkbox;
import java.awt.Paint;
import java.awt.Toolkit;
import java.awt.event.*;

public class Main extends JPanel {

    static Object [][] Services = {{"Google.exe","Chickeaen.exe","Crp.exe"}};
    static String [] ColNames = {"Processes:","Crolly:","Haler:"};

    static JFrame Fram = new JFrame(); 
    static JTextField CBox = new JTextField();
    static JTable Tabs = new JTable(Services,ColNames);
    JScrollPane ScrollArea = new JScrollPane();
    static JButton ExitB = new JButton();
    Dimension ScreenSize = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
    Border BlackLineB = BorderFactory.createLineBorder(new Color(50,50,50));

    public Main() {

    Fram.setTitle("Jared Console");
    Fram.setUndecorated(true);
    Fram.setVisible(true); 
    Fram.setDefaultCloseOperation(Fram.EXIT_ON_CLOSE);
    Fram.setResizable(false);
    Fram.setSize((int)Math.round(ScreenSize.getWidth()*0.45),(int)Math.round(ScreenSize.getHeight()*0.33));
    Fram.setBackground(new Color(0,0,0,150));
    Fram.add(this);

    CBox.setSize((int)Math.round(Fram.getWidth()*0.80),(int)Math.round(Fram.getHeight()*0.25));
    CBox.setBackground(new Color(255,255,255));
    CBox.setBorder(BorderFactory.createCompoundBorder(BlackLineB,BorderFactory.createEmptyBorder(0,20,0,0)));
    CBox.setLocation((int)Math.round(Fram.getWidth()*0.1),(int)Math.round(Fram.getHeight()*0.70));
    CBox.setFont(new Font("Arial",Font.BOLD,20));
    CBox.setVisible(true);

    ScrollArea.setSize((int)Math.round(Fram.getWidth()*0.80),(int)Math.round(Fram.getHeight()*0.50)); 
    ScrollArea.setLocation((int)Math.round(Fram.getWidth()*0.10),(int)Math.round(Fram.getHeight()*0.10));
    ScrollArea.setBorder(BlackLineB);
    ScrollArea.setLayout(null);
    ScrollArea.setVisible(true);

    Tabs.setSize((int)Math.round(Fram.getWidth()*0.995),(int)Math.round(Fram.getHeight()*0.995));
    Tabs.setLocation((int)Math.round(Fram.getWidth()*0.003),(int)Math.round(Fram.getHeight()*0.005));
    Tabs.setFillsViewportHeight(true);
    Tabs.setBackground(new Color(255,255,255));

    this.add(CBox);
    this.add(Tabs);
    this.add(ExitB);
    ScrollArea.add(Tabs);
    this.add(ScrollArea);
    this.setBorder(BorderFactory.createLineBorder(new Color(50,50,50),5));
    this.setLayout(null);
    this.setBackground(new Color(0,0,0));
    this.setVisible(true);

    }

    public void paintComponent(Graphics Gla) { 
    Paint Plat = new GradientPaint(0f, 0f, new Color(0, 40, 0, 0), 0.0f, Fram.getHeight(), new Color(0, 0, 0, 150), true); //Made 200 equal to Fram Background Alpha.
    Graphics2D Quo = (Graphics2D)Gla;
    Quo.setPaint(Plat);
    Quo.fillRect(0, 0, Fram.getWidth(), Fram.getHeight());
    }

    public static void main(String[] args) {
    Main CScreen = new Main(); 
    GraphicsEnvironment GE = GraphicsEnvironment.getLocalGraphicsEnvironment(); // Have to study lines 57,58 and 59
    GraphicsDevice GD = GE.getDefaultScreenDevice();
    boolean CheckTransL = GD.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);

    if (!CheckTransL) {
        System.out.println("PERPIXEL TRANSLUCENT NOT SUPPORTED - LOL UPDATESCRUB!");
        System.exit(0);
    };  
    }

}

Why does the JTable not show the Jtable heading even when added to JScrollPane? Also the Console shows a error message at first then quickly goes away and launches the program? So yea I'd like to know what's wrong with this and also can you can note me of some bad habits in this program such as the way it's being typed.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 1
    Because you don't, wrap the table in the `JScrollPane` – MadProgrammer Jul 07 '15 at 05:45
  • 1
    `ScrollArea.setLayout(null);` My, my.. If that is not part of the problem, I'll eat my hat. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jul 07 '15 at 05:47
  • 1
    BTW - Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Jul 07 '15 at 05:47
  • 1
    And `public void paintComponent(Graphics Gla) { Paint Plat..` should be `public void paintComponent(Graphics Gla) { super.paintComponent(Gla); Paint Plat..` to avoid painting artifacts & ensure borders etc. are painted. – Andrew Thompson Jul 07 '15 at 05:49
  • *"PERPIXEL TRANSLUCENT NOT SUPPORTED - LOL UPDATESCRUB!"* - Perpixel alphering support is also a OS issue, the user might have the hardware, but the OS might not supported ;) – MadProgrammer Jul 07 '15 at 06:01

2 Answers2

2

Problems

  • null layout. Avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify. See Laying Out Components Within a Container for more details
  • Over use of static. static is not your friend and you should avoid using it. It is not a cross object communication mechanism and over use like this will burn you
  • You don't actually wrap the JTable in JScrollPane
  • Breaking the paint chain by not calling super.paintComponent, this is going to produce a series of wonderful paint artifacts. See Painting in AWT and Swing and Performing Custom Painting for more details about how painting works in Swing
  • You might like to have a read through Code Conventions for the Java TM Programming Language, it will make it easier for people to read your code and for you to read others
  • All interactions, creations and modifications to the UI should be done from within the context of the Event Dispatching Event, to reduce the risk of potential race conditions, dead locks and rendering artifacts. See Initial Threads for more details
  • Toolkit.getDefaultToolkit().getScreenSize() is a bad indicator for the viewable space of a screen, it does not take into account OS specific elements, like the dock or task bar, which could have your application appearing under them (and I really, really, REALLY hate it when that happens). Use appropriate layout managers and pack to pack the window around the content. You can then use JFrame#setLocationRelativeTo and pass it null and it will center the frame in the screen

JScrollPane issue...

The simply solution would be to use JScrollPane's constructor to pass it the reference of the JTable...

static JTable Tabs = new JTable(Services,ColNames);
JScrollPane ScrollArea = new JScrollPane(Tabs);

But, then you do this later in your code...

this.add(Tabs);

This will remove the table from the scroll pane to add it to you panel, as a component can only have a single parent.

Another option would be to specifiy the scroll pane's viewport's view component...

this.add(CBox);
//this.add(Tabs);
this.add(ExitB);
//ScrollArea.add(Tabs);
ScrollArea.setViewportView(Tabs);
this.add(ScrollArea);

You should never add components directly to a scroll pane (or it's underlying viewport), they have their own internal layout management functionality going on. Instead, you need to supply the component as the "view" to the JViewport

Take a look at How to Use Scroll Panes for more details.

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

First of all you should add a JTable to the ViewPort of the a JScrollPane in order to JTableHeader could be visible.

After that you should not add your JTable to both the JScrollPane and also the underlying container. You should:

  1. add the JTable to the ViewPort of the JScrollPane.
  2. add the JScrollPane to the underlying container.

and remove the line that add the JTable to the container explicitly.

Good Luck.

STaefi
  • 4,297
  • 1
  • 25
  • 43