i am trying to create a jtable using java swing and little bit i am able to do this but problem is this that i want to create that jtable on full jframe window how can i do this here is my code given below
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
public class ScrollableJTable{
public static void main(String[] args) {
new ScrollableJTable();
}
public ScrollableJTable(){
JFrame frame = new JFrame("Creating a Scrollable JTable!");
JPanel panel = new JPanel();
String data[][] = {{"001"},
};
String col[] = {"Roll"};
JTable table = new JTable(data,col);
JTableHeader header = table.getTableHeader();
header.setBackground(Color.yellow);
Toolkit tk = Toolkit.getDefaultToolkit();
int xSize = ((int) tk.getScreenSize().getWidth());
int ySize = ((int) tk.getScreenSize().getHeight());
JScrollPane pane = new JScrollPane(table);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
panel.add(pane);
frame.add(panel);
frame.setSize(xSize,ySize);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
How can i achieve my desired output?
hanks in advance
Simple, set the layout manager for panel as BorderLayout or simple add the JScrollPane (pane) directly to the frame, which uses a BorderLayout by default
Take a look at Laying Out Components Within a Container for more details
cool..you do not need to write so many lines of code to display a file.Just download the jar file rs2xml and add it to your project library.
Note: This will be useful if you are using netbeans
How to use it?
create the table in backend
download rs2xml jar file
Add it to the project library
drag the jtable from the swing controls to your jframe
Now write the following code as follows:
write ur query:
ps=con.prepareStatement("select * from register");
ResultSet r=ps.executeQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(r)); //display the register table
Related
I'm fairly new to working with graphics in Java, and have been trying to make a simple console to display text based games in a window. I have a test class, where I'm working on the console, but when I add a JTextArea to my console window, it either takes up the entire window or doesn't display at all.
Here is my code:
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.Event;
public class GUI {
public static void main(String[] args) {
JFrame frame = new JFrame("AoA");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(1020,760);
frame.setBackground(Color.LIGHT_GRAY);
frame.setResizable(false);
JTextArea jta = new JTextArea(100,100);
jta.setEditable(false);
jta.setBackground(Color.WHITE);
frame.add(jta);
}
}
I know that some of my imports aren't used in this file, but they will used in the final game. I am also aware that the JTextArea is set to size 100,100, which I am unsure whether it is too large or small. I could really use some help on this, though.
Your problem is the installed by default BorderLayout in your frame. The easiest way to solve your problem is to set another layout manager.
public static void main(String[] args) {
SwingUtilities.invokeLater(GUI::startUp);
}
private static void startUp() {
JFrame frame = new JFrame("AoA");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1020,760);
frame.setBackground(Color.LIGHT_GRAY);
frame.setResizable(false);
frame.setLayout(new FlowLayout()); // FlowLayout is required
JTextArea jta = new JTextArea(40,40);
jta.setEditable(false);
jta.setBackground(Color.WHITE);
// JScrollPane to get the scroll bars when required
frame.add(new JScrollPane(jta));
// setVisible should be last operation to get a correct painting
frame.setVisible(true);
}
Please take a look for layout managers in Swing
So I have this JFrame that contains a JPanel and in there I add JLabels with information I want but since I'll be adding labels all the time at some point the text is too long to appear so I want to add a scrollbar. Basically I want to make my JFrame with a JPanel in it scrollable. I have this code but my problem is that even though the scrollbar appears but it doesnt move and doesn't really work when the text is a lot, meaning the text still gets cut out and the scrollbar is there not moving. Does anyone know how to fix this?
import javax.swing.*;
import java.awt.*;
import java.util.*;
public class Bar {
JFrame info = new JFrame("Information");
JLabel ballinf = new JLabel();
JPanel contentPane = new JPanel();
JScrollPane scrolling = new JScrollPane();
public Bar(){
contentPane.setOpaque(true);
contentPane.setBackground(Color.WHITE);
contentPane.setLayout(null);
scrolling = new JScrollPane(contentPane,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
info.add(scrolling);
info.setSize(750, 600);
info.setLocationByPlatform(true);
info.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
info.setVisible(true);
}
public void adding(int pos){
ballinf = new JLabel("Something ",JLabel.CENTER);//assume the text will be bigger here and have more info
ballinf.setSize(700, 30);
ballinf.setForeground(Color.green);
ballinf.setLocation(5, 5+pos);
contentPane.add(ballinf);
info.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
info.setVisible(true);
}
public static void main(String[] args){
Bar stats = new Bar();
stats.adding(0);
stats.adding(20);//this will be done in a for loop for more than 2 times so the text ends up to be a lot
}
}
contentPane.setLayout(null);
Don't use a null layout!!!
You need to use an appropriate layout manager. Read the section from the Swing tutorial on Layout Managers for more information and working examples. The layout manager will then determine the preferred size of the panel as you add components to the panel.
The scrollpane will then display the scrollbars when necessary.
If you dynamically add components to the panel (after the GUI is visible) then the code should be something like:
panel.add(...);
panel.revalidate();
panel.repaint();
I have a JTable component I made in IntelliJ's GUI Designer. The class is bound to the GUI Form and compiles without problem, but the table stays empty. I've tried to use some other code as well, but im a bit new to java and really want to know the answer to this. please dont give me a half answer or point me to a page to go figure it out. I am here cause I did try that.
package com.company;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
/**
* Created by User on 3/8/2016.
*/
public class TableGUI {
private static JFrame frame;
private JTable table1;
private JPanel panel1;
public TableGUI() {
DefaultTableModel model = new DefaultTableModel();
table1.setAutoCreateRowSorter(true);
table1.setFillsViewportHeight(true);
table1.setPreferredScrollableViewportSize(new Dimension(550, 200));
model.addColumn("Id");
model.addColumn("First Name");
model.addColumn("Last Name");
model.addColumn("Company Name");
table1.setModel(model);
}
public static void main(String[] args) {
frame = new JFrame("TableGUI");
frame.setContentPane(new TableGUI().panel1);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setSize(new Dimension(600, 400));
}
}
I figured it out, the answer was I was lacking a JScrollPane. I had to drop in a component called JScrollPane via the IntelliJ Designer and drop the JTable inside it. This allowed for the column-names to show on the JTable.
Is it not possible to add jxbrowser contents to jpanel. When I add jxBrowser content to JFrame, it is seen very easily. But I am not able to add jxBrowser content to jpanel. I don't know if it is not possible or I am not able to add jxBrowser contents to jpanel.
Here's sample code that demonstrates how to embed JxBrowser Browser component into JPanel:
import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;
import javax.swing.*;
import java.awt.*;
/**
* This sample demonstrates how to create Browser instance,
* embed it into Swing BrowserView container, display it in JFrame and
* navigate to the "www.google.com" web site.
*/
public class BrowserSample {
public static void main(String[] args) {
Browser browser = new Browser();
BrowserView view = new BrowserView(browser);
JPanel panel = new JPanel(new BorderLayout());
panel.add(view, BorderLayout.CENTER);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(panel, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
browser.loadURL("https://google.com");
}
}
More samples you can find at https://jxbrowser.support.teamdev.com/support/solutions/9000049010
Actually jxbrowser is able to added into Jpanel.
I have met the same problem. It is just because I did not specify BorderLayout when initilize JPanel. The code blow is alright.
JPanel panel = new JPanel(new BorderLayout());
I want to create a Window with an image and a text so far i've got this:
public void ShowPng1() {
ImageIcon theImage = new ImageIcon("Icon_Entry_21.gif");
panel.setSize(270, 270);
JLabel label = new JLabel("Hello, World!");
JLabel imageLabel = new JLabel(theImage);
imageLabel.setOpaque(true);
panel.add(imageLabel);
panel.add(label);
panel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setVisible(true);
}
My panel:
private JFrame panel = new JFrame();
For some reason it won't load nor image nor text, it just pops up as a white window. What can be the problem? I've also tried changing the format to .png, didn't work.
UPDATE
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Img {
private JFrame panel = new JFrame();
public Img(){
ShowPng1();
}
public void ShowPng1() {
ImageIcon theImage = new ImageIcon("Icon_Entry_21.gif");
panel.setSize(300, 300);
panel.setResizable(false);
JLabel label = new JLabel("Hello, World!");
JLabel imageLabel = new JLabel(theImage);
imageLabel.setOpaque(true);
panel.add(imageLabel);
panel.add(label, BorderLayout.PAGE_END);
panel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setVisible(true);
}
public static void main(String[] args) {
new Img();
}
}
I've managed to get this working, which is ridiculous because I can't figure out how to make it work with my program. Reimeus gave me an idea on creating this script separately, the fix and that worked. I will have to look through my entire program to see if I'm missing anything. Creating it in a separate class should work as well.
it just pops up as a white window
Sounds like you're blocking the EDT on startup. You may need to use one of Swing's concurrency mechanisms to solve it. Post a Minimal, Complete, Tested and Readable example so we can determine this for sure.
In the meantime...
You're displacing the component containing the theImage component in the BorderLayout.CENTER location
panel.add(label);
You could organize your labels so that they can appear simultaneously (placing the components at 2 different BorderLayout locations will do)
panel.add(imageLabel);
panel.add(label, BorderLayout.PAGE_END);
You should make a JPanel and add it to the frame, and then add the labels to the panel
Something like
private JPanel panel = new JPanel;
and then add it to the frame in your method calling
frame.add(panel);