java awt or Swing component missing or not shown - java

i've been charged with a new software tool, and i m trying to test it.
PROBLEM:
I noticed a difference between mine GUI and the GUI of the tool run on the server:
a component is missing!
It is like if it's not shown, because i went in debug mode, and everything seems to be fine.
this image shows the different:
as you can see, the "Shaft end Dia" component is missing between the others 2 components.
CODE:
here is a code part. There are lots of items that get instantiate and added in the same way, but it doesn't apper on my pc (but they works well in others!!)
//PanelContainerClass
public class myInputPanel extends JPanel {
//myInputPanel fields
private JPanel myPanelMissing = null;
private JLabel jLabel_ofPanelMissing = null;
//myInputPanel is added to the mainFrame.
public myInputPanel() {
super();
initialize();
}
private void initialize() {
//a GridBagConstraints is created for each panel i m going to add to myInputPanel
GridBagConstraints gbc6 = new GridBagConstraints();
gbc6.gridx = 6;
gbc6.ipadx = 46;
gbc6.fill = GridBagConstraints.BOTH;
gbc6.insets = new Insets(0, 0, 0, 0);
gbc6.ipady = 0;
gbc6.gridy = 1;
//in a similiar way others gbc are instantiate
//a layout is given to myInputPanel
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 176, 0, 63, 0, 0, 0, 0, 0, 0, 0};
this.setLayout(gridBagLayout);
this.setSize(1185, 120);
this.setPreferredSize(new Dimension(1164, 143));
this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
// add others panels to myInputPanel using this.add(getPanel_X(), gridBagConstraintsN);
this.add(getmyPanelMissing(), gridBagConstraints6);
// add others panels to myInputPanel using this.add(getPanel_Y(), gridBagConstraintsM);
}
private JPanel getmyPanelMissing() {
if (myPanelMissing == null) {
//in debug it get inside the if
jLabel_ofPanelMissing = new JLabel();
jLabel_ofPanelMissing.setHorizontalAlignment(SwingConstants.CENTER);
jLabel_ofPanelMissing.setText("<html><body>" +
"<table cellspacing='0';cellpadding='0';> <tr> <td align='center'>Shaft end Dia</td> </tr> <tr> <td align='center'>[mm]</td> </tr></table>" +
"</body></html>");
GridLayout lay = new GridLayout();
lay.setRows(1);
myPanelMissing = new JPanel();
myPanelMissing.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
myPanelMissing.setLayout(lay);
myPanelMissing.add(jLabel_ofPanelMissing, null);
}
return myPanelMissing;
}
}
What i tried to do is:
running it via my Eclipse: FAIL
running the jar via .bat file: FAIL
(FAIL means it doesn't appear)
running the jar on the server via .bat file: WORKS
running the jar via eclipse on a colleague's computer by downloading the code via cvs: WORKS
running the jar via .bat on colleague's computer giving him my files: WORKS
running the jar via .bat on colleague's computer with files compiled by himself: WORKS
commenting the previous panelColumn on my code: WORKS!!!!
NOTE:
1) java version: 1.7.0.75 (either on mine pc either on my collegue pc)
2) OS: window 7 on Vbox (either me and collegue)
3) Eclipse Luna 4.x (same version again for both)
QUESTION:
Has anyone had this kind of problems?
Any idea about how to solve it?
Thank you all in advance!

You must increase the area of the your panel myInputPanel with the method setBounds()

Related

Scrollbar for JTable

so I have actually couple of issues where I could need some help.
1.) Actually I can display the SQL Result into the jList, that works so far.
What I am actually missing are the names of the Columns. It just shows me the MySQL Entries.
2.) The second issue is, that my Database actually includes more Information. I want to show that in the jList also but to prevent having a huge window I want to add a horizontal and vertical scrollbar.
I tried adding one in but whenever I did that the list suddenly disappeared. how would adding a scrollbar to the list actually look like ?
The next steps of my Java program would be MySQL Operations. I want to change Values of the received dataset and send it back. I think I will do that with some kind of getSelectedRow right ?
Thank you very much for your help
public class aPanel extends JFrame{
private JTable jt;
public aPanel() {
getContentPane().setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(0, 0, 780, 450);
JPanel contentPane = new JPanel();
contentPane.setBackground(new Color (51,51,51));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel leftTitle = new JPanel();
leftTitle.setBackground(new Color (51,153,255));
leftTitle.setBounds(0, 0, 230, 450);
contentPane.add(leftTitle);
leftTitle.setLayout(null);
String[] columnNames = {"ID","First_Name","Last_Name"};
DefaultTableModel tableModel = new DefaultTableModel(columnNames, 0);
try {
// Establishment of JDBC Connection
String url = "jdbc:mysql://localhost:3306/eHealthDB?serverTimezone=UTC";
Connection con = DriverManager.getConnection(url,"root","root");
String sql = "SELECT ID,First_Name,Last_Name FROM patient;";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next() )
{
String pID = rs.getString("ID");
String vName = rs.getString("First_Name");
String nName = rs.getString("Last_Name");
String[] data = {pID,vName,nName};
tableModel.addRow(data);
}
jt = new JTable();
jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jt.setCellSelectionEnabled(false);
jt.setRowSelectionAllowed(true);
jt.setBorder(new LineBorder(Color.WHITE));
jt.setForeground(Color.WHITE);
jt.setBackground(new Color (59,59,59));
jt.setBounds(370, 52, 251,167);
contentPane.add(jt);
jt.setModel(tableModel);
} catch (Exception e1) {
System.out.println(e1);
}
}
}
Don't use a null layout (get rid of that code)
Don't use setBounds(...) (get rid of that code)
Swing was designed to be used with layout managers. The layout manager will give the component a size/location based on the rules of the layout manager.
What I am actually missing are the names of the Columns.
In order for the columns names to appear the table must be added to the viewport of a JScrollPane and the scroll pane added to the frame.
So the basic change would be:
//contentPane.setLayout(null);
contentPane.setLayout(new BorderLayout());
and
//contentPane.add(jt);
contentPane.add(new JScrollPane(jt), BorderLayout.CENTER);
This will allow the table to fill the available space in frame. Scrollbars will appear as required. Read the section from the Swing tutorial on Layout Managers for more information and working examples.
Your question states, "JList", but your code shows a JTable -- this is a bit confusing
Having said that, you've a major problem here:
jt.setBounds(370, 52, 251,167);
Never restrict the size of an expanding component such as a JTable or a JTextArea since this prevents it from displaying within the JScrollPane as it should since it artificially restricts its size. If you are going to restrict the size of anything, it should be the JScrollPane's viewport, and only do this very carefully.
Next, you're adding the JTable itself to the GUI and should be adding JTable to a JScrollPane, actually to the JScrollPane's viewport, and then add the containing JScrollPane to the GUI.
e.g.,
jt = new JTable();
jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jt.setCellSelectionEnabled(false);
jt.setRowSelectionAllowed(true);
jt.setBorder(new LineBorder(Color.WHITE));
jt.setForeground(Color.WHITE);
jt.setBackground(new Color (59,59,59));
// jt.setBounds(370, 52, 251,167);
JScrollPane scrollPane = new JScrollPane(jt);
// contentPane.add(jt);
jt.setModel(tableModel);
contentPane.add(scrollPane);

JTabbedpane in JSplitPane

Respected Developers,
I am developing a NASA World Wind application for desktop.
I have tried to put a JSplitPane where in the lower Panel displays the result and the upper one displays the canvas is in two sections. The left component contains the layer list and the right component contains the canvas. I have been trying to insert a JTabbedPane in the left component so as to have different options like the layer list, SQL query list and many more. I am unable to add and view the frame of the JTabbedPane on the left Panel.
I am attaching my code for reference.
All help is appreciated.
CODE BLOCK
public AppFrame()
{
/*------------------------------------INITIALIZATION OF THE MAIN FRAME------------------------------------*/
this.initialize(false, true, false);
/*---------------------------------------SPLIT PANE AND PANEL CODE---------------------------------------*/
// Create a horizontal split pane containing the layer panel and the WorldWindow panel.
JSplitPane horizontalSplitPane = new JSplitPane();
horizontalSplitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
JTabbedPane tp=new JTabbedPane();
horizontalSplitPane.setLeftComponent(tp);
horizontalSplitPane.setRightComponent(wwjPanel);
horizontalSplitPane.setOneTouchExpandable(true);
horizontalSplitPane.setContinuousLayout(true);// prevents the pane's being obscured when expanding right
// Create a panel for the bottom component of a vertical split-pane.
JPanel bottomPanel = new JPanel(new BorderLayout());
JLabel label = new JLabel("Bottom Panel");
label.setBorder(new EmptyBorder(10, 10, 10, 10));
label.setHorizontalAlignment(SwingConstants.CENTER);
bottomPanel.add(label, BorderLayout.CENTER);
// Create a vertical split-pane containing the horizontal split plane and the button panel.
JSplitPane verticalSplitPane = new JSplitPane();
verticalSplitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
verticalSplitPane.setTopComponent(horizontalSplitPane);
verticalSplitPane.setBottomComponent(bottomPanel);
verticalSplitPane.setOneTouchExpandable(true);
verticalSplitPane.setContinuousLayout(true);
verticalSplitPane.setResizeWeight(1);
// Add the vertical split-pane to the frame.
this.getContentPane().add(verticalSplitPane, BorderLayout.CENTER);
this.pack();
// Center the application on the screen.
Dimension prefSize = this.getPreferredSize();
Dimension parentSize;
java.awt.Point parentLocation = new java.awt.Point(0, 0);
parentSize = Toolkit.getDefaultToolkit().getScreenSize();
int x = parentLocation.x + (parentSize.width - prefSize.width) / 2;
int y = parentLocation.y + (parentSize.height - prefSize.height) / 2;
this.setLocation(x, y);
this.setResizable(true);
/*-------------------------------------------PATH CREATION CODE-------------------------------------------*/
// Add a dragger to enable shape dragging
this.getWwd().addSelectListener(new BasicDragger(this.getWwd()));
// Create and set an attribute bundle.
ShapeAttributes attrs = new BasicShapeAttributes();
attrs.setOutlineMaterial(new Material(WWUtil.makeRandomColor(null)));
attrs.setOutlineWidth(2d);
ArrayList<Position> pathPositions = new ArrayList<Position>();
pathPositions.add(Position.fromDegrees(26, 75, 1e4));
pathPositions.add(Position.fromDegrees(20, 80, 1e4));
Path path = new Path(pathPositions);
path.setAttributes(attrs);
path.setVisible(true);
path.setAltitudeMode(WorldWind.RELATIVE_TO_GROUND);
path.setPathType(AVKey.GREAT_CIRCLE);
RenderableLayer propertiesLayer = new RenderableLayer();
propertiesLayer.addRenderable(path);
/*--------------------------------------------TABBED PANE CODE--------------------------------------------*/
JTextArea ta = new JTextArea();
horizontalSplitPane.add(ta);
// JPanel p2=new JPanel();
// JPanel p3=new JPanel();
tp.setBounds(50,50,200,200);
tp.add("Layers",layerPanel);
// tp.add("SQL",p2);
// tp.add("help",p3);
horizontalSplitPane.add(tp);
tp.setSize(400,400);
tp.setLayout(null);
tp.setVisible(true);
// Add the layer to the model.
insertBeforeCompass(getWwd(), propertiesLayer);
List<String> markers = new ArrayList<String>(1);
markers.add(String.valueOf(new BasicMarker(Position.fromDegrees(90, 0), new BasicMarkerAttributes())));
MarkerLayer markerLayer = new MarkerLayer();
// markerLayer.setMarkers(markers);
insertBeforeCompass(getWwd(), markerLayer);
}
ERROR BLOCK
"C:\Program Files (x86)\Java\jdk1.7.0_03\bin\java" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.3.4\lib\idea_rt.jar=55088:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.3.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\ext\sunec.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\ext\sunpkcs11.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.7.0_03\jre\lib\rt.jar;D:\NasaWorldWind\classes;D:\NasaWorldWind\jogl-all.jar;D:\NasaWorldWind\gluegen-rt.jar;D:\NasaWorldWind\gdal.jar;D:\DOWNLOADS\postgresql-42.2.1.jre7.jar" avr_dbpostgisapp.ApplicationTemplateTest
java.lang.InstantiationException: avr_dbpostgisapp.ApplicationTemplateTest$AppFrame
at java.lang.Class.newInstance0(Class.java:357)
at java.lang.Class.newInstance(Class.java:325)
at avr_dbpostgisapp.ApplicationTemplateTest.start(ApplicationTemplateTest.java:410)
at avr_dbpostgisapp.ApplicationTemplateTest.main(ApplicationTemplateTest.java:586)
Exception in thread "main" java.lang.NullPointerException
at avr_dbpostgisapp.ApplicationTemplateTest.connstart(ApplicationTemplateTest.java:502)
at avr_dbpostgisapp.ApplicationTemplateTest.main(ApplicationTemplateTest.java:588)
Process finished with exit code 1
So, I was able to manage the addition of the JTabbedPane to the left component of the JSplitPane.
The code is as follows,
CODE BLOCK
JSplitPane horizontalSplitPane = new JSplitPane();
horizontalSplitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
JTextArea ta = new JTextArea(200, 200);
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
p1.add(ta);
p2.add(ta);
JTabbedPane tp = new JTabbedPane();
tp.add("Layers",layerPanel);
tp.add("SQL",p2);
horizontalSplitPane.setLeftComponent(tp);
horizontalSplitPane.setRightComponent(wwjPanel);
horizontalSplitPane.setOneTouchExpandable(true);
horizontalSplitPane.setContinuousLayout(true);

BoxLayout adding left margin

I've got a JPanel that has a BoxLayout (Page axis), and I want to lay out two components, one on top of the other.
My problem is the margin to the left of the large lipsum box, how can I get rid of this? If I don't add the top components, there is no margin.
Here's my code, the second image is created by not adding headerPanel:
JLabel commandLabel = new JLabel(command);
JLabel paramLabel = new JLabel(params);
JLabel descLabel = new JLabel("<html><body style='width: 200px;'>" + description + "</body></html>");
Font baseFont = commandLabel.getFont(), commandFont, paramFont, descFont;
commandFont = baseFont.deriveFont(Font.BOLD);
paramFont = baseFont.deriveFont(Font.ITALIC);
descFont = baseFont.deriveFont(Font.PLAIN);
commandLabel.setFont(commandFont);
paramLabel.setFont(paramFont);
descLabel.setFont(descFont);
descLabel.setAlignmentX(LEFT_ALIGNMENT);
descLabel.setBorder(BorderFactory.createStrokeBorder(new BasicStroke()));
JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
headerPanel.add(commandLabel);
headerPanel.add(paramLabel);
this.add(headerPanel);
this.add(descLabel);
This class extends JPanel, and is added to a JFrame, which is simply pack()'d
Though I couldn't tell where the observed behaviour comes from, the expected display could be achieved by using an intermediate JPanel to contain your label, rather than adding the JLabel directly :
JLabel commandLabel = new JLabel(command);
JLabel paramLabel = new JLabel(params);
JLabel descLabel = new JLabel("<html><body style='width: 200px;'>" + description + "</body></html>");
Font baseFont = commandLabel.getFont(), commandFont, paramFont, descFont;
commandFont = baseFont.deriveFont(Font.BOLD);
paramFont = baseFont.deriveFont(Font.ITALIC);
descFont = baseFont.deriveFont(Font.PLAIN);
commandLabel.setFont(commandFont);
paramLabel.setFont(paramFont);
descLabel.setFont(descFont);
descLabel.setAlignmentX(LEFT_ALIGNMENT);
descLabel.setBorder(BorderFactory.createStrokeBorder(new BasicStroke()));
JPanel headerPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
JPanel descPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));// added
headerPanel.add(commandLabel);
headerPanel.add(paramLabel);
descPanel.add(descLabel);// added
this.add(headerPanel);
this.add(descPanel);// modified
My problem is the margin to the left of the large lipsum box, how can I get rid of this?
You need to make the alignments of your components consistent. That is the alignment "X" property of all the components should be left aligned.
I'm guessing the JLabel is center aligned so you need to use:
descLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
See Fixing Alignment Problems section from the Swing tutorial on How to Use BoxLayout for more information and examples.

List not updating correctly?

Ive been following TheCherno's network chat programming tutorials, and I am almost done, just fixing bugs when I ran into a problem. I have a button that opens a window that is supposed to have a list of all the connected users listed. However, everytime I test it, the only item on the list is the first person who connected to that instance of the server. It is the same for all of the other users connected as well. I have no errors in my code, and it all seems correct. I can't seem to find the error.
This function is called from a while running loop, and everything else in it is working constantly, so I don't think there is something wrong in this.
private void sendStatus() {
if (clients.size() <= 0) return;
String users = "/u/";
for (int i = 0; i < clients.size() - 1; i++) {
users += clients.get(i).name + "/n/";
}
users += clients.get(clients.size() - 1).name + "/e/";
sendToAll(users);
}
This is from another class, where the client decodes the messages sent from the server.
else if (message.startsWith("/u/")) {
String[] u = message.split("/u/|/n/|/e/");
users.update(Arrays.copyOfRange(u, 1, u.length - 1));
}
And then this is the peice that handles the graphics of it, I don't see anything wrong in it, but I will include it anyway jsut in case. Thanks all!
private JPanel contentPane;
private JList list;
public OnlineUsers() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(200, 320);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0};
gbl_contentPane.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{1.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
list = new JList();
GridBagConstraints gbc_list = new GridBagConstraints();
gbc_list.fill = GridBagConstraints.BOTH;
gbc_list.gridx = 0;
gbc_list.gridy = 0;
JScrollPane p = new JScrollPane();
p.setViewportView(list);
contentPane.add(p, gbc_list);
list.setFont(new Font("Verdana", 0, 14));
}
public void update(String[] users) {
list.setListData(users);
}
Still no ideas at all. I even checked the source code that the tutorial person made and its the same thing. I would really appreciate the help, as I am 14 and just learning java. Bump.
https://github.com/TheCherno/ChernoChat

Simple way to add window to system tray [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How can I put my Java program in the system tray?
I am making a notification system in java, I want the program to show up in the system tray, instead of on the task bar, I have tried:
notification.setExtendedState(JFrame.ICONIFIED);
Not only does this not work, but it lags the heck otta my computer
Current Code:
public static void notify(String line1, String line2, String imagepath, int style){
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int width = gd.getDisplayMode().getWidth();
int swidth = width - 320;
JFrame notification = new JFrame();
JPanel main = new JPanel(new FlowLayout(FlowLayout.RIGHT));
main.setLayout( new GridLayout( 2 , 1 ) );
JLabel notifyline1 = new JLabel();
notifyline1.setText(line1);
notifyline1.setFont(new Font("Minecraft",1 , 16));
notifyline1.setForeground(new Color(242, 238, 17));
main.add(notifyline1);
JLabel notifyline2 = new JLabel();
notifyline2.setText(line2);
notifyline1.setFont(new Font("Minecraft",1 , 12));
notifyline1.setForeground(Color.black);
main.add(notifyline1);
notification.add(main);
notification.setExtendedState(JFrame.ICONIFIED);
notification.setSize(new Dimension(320,64));
notification.setLocation(swidth, 0);
notification.setUndecorated(true);
notification.setVisible(true);
}
ALSO, to kill 2 birds with one stone
is there a way to color a jlabel, tried
label1.setForegroundColor(new Color(100, 100, 100));
Java has TrayIcon class which can be used to minimize application to SystemTray.You can see the working example here.

Categories