I want to add test2 to my DFrametest class, so that the table of test2 shows up in my window. I get an empty window and can't find the mistake.
public class test2 extends JPanel {
JTable tbl;
DefaultTableModel dt;
public test2(){
JLabel label = new JLabel("Course Lookup GUI");
this.add( label );
tbl = new JTable();
dt = new DefaultTableModel();
dt.addColumn("ID");
dt.addColumn("Name");
tbl.setModel(dt);
try{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/lagerverwaltungwin", "root", "");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT ArtNr, Beschreibung FROM artikel");
}catch(Exception e){
e.printStackTrace();
}
JScrollPane jp = new JScrollPane();
jp.getViewport().add(tbl);
add(jp);
}
}
This is my Frameclass which should have the table from test2:
public class DFrametest extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DFrametest frame = new DFrametest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public DFrametest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
test2 t = new test2();
this.add(t);
this.setVisible(true);
}
}
Never Use null Layout. Also donot use setBounds(100, 100, 450, 300);
use pack();
Change your DFrametest class like this
contentPane.setLayout(new BorderLayout());
contentPane.add(t, BorderLayout.CENTER);
Full Code of DFrametest class:
public class DFrametest extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DFrametest frame = new DFrametest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public DFrametest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
test2 t = new test2();
contentPane.setLayout(new BorderLayout());
contentPane.add(t, BorderLayout.CENTER);
pack();
}
}
Try to use setBounds() method since you haven't mentioned any layout (setLayout is null).
Add setBounds method before adding the jpanel to the jframe
setBounds(x-axis,y-axis,width,height);
EX:-
setBounds(10,20,200,400);
Note:
Related
I use Eclipse Window Builder. When I click the button, something will be written on the screen. But since my prints are long, I want to use a scroll pane.
public class uyg2 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
uyg2 frame = new uyg2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public uyg2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("New button");
btnNewButton.setBounds(32, 29, 89, 23);
contentPane.add(btnNewButton);
JTextArea textArea = new JTextArea();
textArea.setBounds(10, 63, 233, 173);
contentPane.add(textArea);
ScrollPane scrollPane = new ScrollPane();
scrollPane.setBounds(249, 10, 173, 118);
contentPane.add(scrollPane);
}
So, based on...
public class uyg1 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
uyg1 frame = new uyg1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public uyg1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JTextArea textArea = new JTextArea("Test");
textArea.setSize(400, 400);
JScrollPane scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
frame.getContentPane().add(scroll);
frame.setVisible(true);
}
}
textArea.setSize(400, 400); is irrelevant as the layout manager will deal with it. You can provide sizing hints via the JTextArea(String, int, int) constructer, but remember, this is the number of characters in width/height, not pixels.
The following are giving you issues...
frame.getContentPane().add(scroll);
frame.setVisible(true);
because frame is undefined. Since the class extends from JFrame, they are pointless, it should just be
getContentPane().add(scroll);
setVisible(true);
but, I'd add...
pack();
setLocationRelativeTo(null);
before it, as it will give you a generally better experience
You need to add the TextArea to the ScrollPane. Don't add the textarea tho the contentpane.
On Eclipse, using JFrames, I'm using a mainFrame as the main user interface and in that frame i got a contentPanel (JPanel) containing a small panel (JPanel as well), under that panel there's a buttong, and i have another class which i named 'clients' (another frame) containting a contentPanel too (JPanel) with other compenents, i want to get the the second class contentPanel to show in the first's panel (the small panel i mentionned).
here's what i did, but it's not working!! any help?
MainF.java
public class mainF extends JFrame {
private JPanel contentPane;
public JPanel panel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mainF frame = new mainF();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public mainF() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 404);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
panel = new JPanel();
panel.setBounds(10, 11, 414, 256);
contentPane.add(panel);
panel.setLayout(null);
JLabel Panel = new JLabel("Main Panel");
Panel.setHorizontalAlignment(SwingConstants.CENTER);
Panel.setBounds(10, 11, 69, 33);
panel.add(Panel);
JButton btnClients = new JButton("Clients");
btnClients.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel = new Clients().getContentPane();
panel.setVisible(true);
}
});
btnClients.setBounds(162, 296, 89, 23);
contentPane.add(btnClients);
}
}
Clients
public class Clients extends JFrame {
private JPanel contentPane;
public JPanel getContentPane() {
return contentPane;
}
public Clients() {
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblClients = new JLabel("Clients");
lblClients.setHorizontalAlignment(SwingConstants.CENTER);
lblClients.setFont(new Font("Times New Roman", Font.BOLD, 18));
lblClients.setBounds(10, 11, 85, 37);
contentPane.add(lblClients);
}
}
Thanks Everyone!!
here's an image to explain the situation:
See the image here
You are using one JPanel reference panel, and using it to create 2 different panel objects. So first remove the panel from contentPane then add it back with new initialization.
public void actionPerformed(ActionEvent e) {
contentPane.remove(panel);
panel = new Clients().getContentPane();
panel.setVisible(true);
contentPane.add(panel);
contentPane.repaint();
contentPane.revalidate();
}
});
Also you require contentpane to revalidate() and repaint()
Hi guys im trying to set a string value on my main frame with a value generated in an other frame, but i dont quite know how to do it this is my code so far.
This is my mainframe opens the query frame where i input the data
public class InfoCd extends JFrame {
private JPanel contentPane;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
InfoCd frame = new InfoCd();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
Connection conn=null;
private JTextField verConsulta;
/**
* Create the frame.
*/
public InfoCd() {
conn=sqliteConnection.dbConnector();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 753, 477);
contentPane = new JPanel();
contentPane.setBackground(SystemColor.activeCaption);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnHacerConsulta = new JButton("Hacer Consulta");
btnHacerConsulta.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Query q = new Query();
q.setVisible(true);
String userQuery = Query.getqueryField();//here is the problem im sending the value before filling it in the second frame
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery(userQuery);
table.setModel(DbUtils.resultSetToTableModel(rs));
//insert delete update
} catch (Exception e2) {
e2.printStackTrace();
}
}
});
btnHacerConsulta.setBounds(20, 388, 169, 39);
contentPane.add(btnHacerConsulta);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 0, 737, 367);
contentPane.add(scrollPane);
table = new JTable();
scrollPane.setViewportView(table);
verConsulta = new JTextField();
verConsulta.setBounds(260, 388, 359, 29);
contentPane.add(verConsulta);
verConsulta.setColumns(10);
}
}
here is the query field
public class Query extends JFrame {
private JPanel contentPane;
private static JTextField queryField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Query frame = new Query();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Query() {
setBackground(SystemColor.activeCaption);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 641, 146);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnAceptar = new JButton("Aceptar");
btnAceptar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
getqueryField();
dispose();
}
});
btnAceptar.setBounds(226, 52, 155, 34);
contentPane.add(btnAceptar);
JLabel lblConsulta = new JLabel("Consulta:");
lblConsulta.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 13));
lblConsulta.setBounds(69, 11, 101, 39);
contentPane.add(lblConsulta);
queryField = new JTextField();
queryField.setBounds(135, 21, 480, 20);
contentPane.add(queryField);
queryField.setColumns(10);
}
public static String getqueryField() {
return queryField.getText();
}
}
How do I switch between JPanels of different classes? I do not wish to use a Card Layout.
I have 2 classes - MainPage & MenuPage. For instance, I would like to clear the contentPane (a JPanel) # MainPage and replace it with the content pane # MenuPage. For testing purposes, I included a button # MenuPage.
Please see my following attempt - it gives an error:
java.lang.IllegalArgumentException: adding a window to a container
MainPage
public class MainPage extends JFrame {
private static JPanel contentPane;
private JLabel imgBackground;
private JLabel lblTitle;
private JLabel imgLogo;
private Dimension dim;
//timer
private final static int interval = 40;
private int i;
private Timer t;
//private JButton start;
//private JLabel lblLoading;
private JProgressBar pbar;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainPage frame = new MainPage();
frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MainPage() {
dim = Toolkit.getDefaultToolkit().getScreenSize();
System.out.println(dim);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
contentPane.setBounds(0,0,dim.width,dim.height);
setContentPane(contentPane);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
t = new Timer (interval, new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (i == 20){
t.stop();
//start.setEnabled(true);
//refresh + load next page ???
contentPane.removeAll();
MenuPage loadpanel2 = new MenuPage();
setContentPane(loadpanel2);
revalidate();
repaint();
}
else{
i++;
pbar.setValue(i);
}
}
});
t.start();
MenuPage
public class MenuPage extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MenuPage frame = new MenuPage();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MenuPage() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
JButton btnSadfsafsa = new JButton("sadfsafsa");
btnSadfsafsa.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnSadfsafsa.setBounds(10, 52, 89, 23);
btnSadfsafsa.setEnabled(true);
btnSadfsafsa.setVisible(true);
contentPane.add(btnSadfsafsa);
}
}
java.lang.IllegalArgumentException: adding a window to a container
That is pretty straightforward. Both GUIs extend JFrame and are therefore top level containers. We cannot add one top level container to another.
Instead of extending frame, both GUIs might extend JPanel. A JPanel (or more than one) can then be added to a JFrame instantiated in the main(String[]) or a showGUI() method.
I have a simple JFrame and a JPanel inside it.
public class TestPanel extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestPanel frame = new TestPanel();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TestPanel() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
}
}
And I have an SWT widget (Nebula Gantt Chart). -->
http://hexapixel.com/software/ganttwidget
I would like to add this SWT widget to the JPanel, actually embed it.
What are the possible ways to do it? Thank you for help! :-)